Table Scan and Index Scan in SQL

0 votes
What distinguishes a table scan from an index scan in SQL, and when would each be used?
Aug 15, 2022 in Database by Kithuzzz
• 38,000 points
815 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Most query engines have a query optimizer, which tries to come up with a good plan for executing queries. The query optimizer will run an index scan or index seek, or a table scan, if there are any accessible indexes, which can speed up a query.

Example:

SELECT * FROM tbl WHERE category_id = 5;

If the category id does not have an index, a table scan—in which every entry in the table is checked for the correct category id—will be carried out.

However, things get more complicated if the category id is indexed. It's likely that an index seek will be used if the table is really large. However, if the table is tiny, the optimizer may decide that a table scan is still quicker because accessing an index incurs some overhead. Scanning the table may be quicker even for large tables if the category id is not discriminating enough, such as if there are only two categories.

Typically, tree structures are used to organize indexes. An O(log n) operation is required to locate an item in a tree. It takes O(n) operations to scan a table. The number of disc accesses necessary to complete the query mostly determines the speed. For tiny tables, accessing the index first and the table for the discovered entries second can result in additional disc accesses.

Let us have a look at another query:

SELECT category_id FROM tbl WHERE category_id BETWEEN 10 AND 100;

Another solution is available in this situation. In this case, an index seek (as opposed to an index scan) might not be quicker than a table scan, but since we are only retrieving category ids, it might be. Instead of utilizing the tree structure, an index scan reads each entry of the index table (what the index seek does). However, since the requested data is all contained in the index, there will be no need to access the data table. Similar to a table scan, an index scan also uses an O(n) operation, however, because an index is typically smaller than a table, it uses fewer disc accesses.

answered Aug 15, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In Database

0 votes
1 answer

Difference between clustered and non clustered index in SQL

The differences between the clustered and non ...READ MORE

answered Sep 28, 2018 in Database by Sahiti
• 6,370 points
1,969 views
0 votes
0 answers

What's the difference between a temp table and table variable in SQL Server?

We can build temp tables in SQL ...READ MORE

Feb 25, 2022 in Database by Vaani
• 7,070 points
706 views
0 votes
0 answers

What is the difference between drop table and delete table in SQL Server?

What is the distinction between the following ...READ MORE

Aug 9, 2022 in Database by Kithuzzz
• 38,000 points
1,021 views
0 votes
0 answers

Merge 2 tables in SQL and save into 1 new table

I have two tables with the same columns. Table ...READ MORE

Aug 26, 2022 in Database by Kithuzzz
• 38,000 points
501 views
0 votes
0 answers

Check if table exists and if it doesn't exist, create it in SQL Server 2008

I'm using SQL Server 2008 to create ...READ MORE

Sep 2, 2022 in Database by Kithuzzz
• 38,000 points
1,033 views
0 votes
1 answer

Difference between single and double quotes in SQL

Single quotes are used to indicate the ...READ MORE

answered Sep 11, 2018 in Database by CodingByHeart77
• 3,750 points
29,987 views
0 votes
1 answer

Which query to use for better performance, join in SQL or using Dataset API?

DataFrames and SparkSQL performed almost about the ...READ MORE

answered Apr 19, 2018 in Apache Spark by kurt_cobain
• 9,350 points
2,098 views
0 votes
1 answer

How to connect Java program to the MySQL database?

You can connect your Java code with ...READ MORE

answered May 11, 2018 in Java by Parth
• 4,640 points
2,058 views
0 votes
1 answer
0 votes
1 answer

Automating Oracle script with nolio

Depending upon the details of your script ...READ MORE

answered Jul 17, 2018 in Other DevOps Questions by ajs3033
• 7,300 points
1,242 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP