Instead of index scan vs index seek , we should say scan vs seek and this covers all
Before going with difference we should know how to Identify them in execution plan then the difference and then code to see how it works
CODE :
create table employee (id int , name varchar (20) , age int)
GO
insert into employee values (1 , 'saurabh', 25)
insert into employee values (2 , 'Ravi', 35)
insert into employee values (3 , 'Anil', 25)
insert into employee values (4 , 'Suresh', 45)
GO
select* from employee
---> Table scan
Create clustered index pk_employee_id on employee(id)
Select * from employee
---> Clusterd index scan
Select * from employee where id= 1
---> Clustered index seek
Create nonclustered index ix_employee_age on employee(age)
Select age from employee where age = 35
---> Non Clustered index seek
You might like to read: What is the Difference Between Rebuild and Reorganize indexes ; Interview Question
Before going with difference we should know how to Identify them in execution plan then the difference and then code to see how it works
DIFFERENCES
Scan | Seek | |
1 | When search definition can not point close on single row/ Range of rows to satisfy search predicates. It called as scan | When index definition can point close on single row/ Range of rows to satisfy search predicates. It called as Seek |
2 | SQL Server has to scan multiple pages to find range of rows which satisfy search predicates. | SQL Server knows which page / Range of rows to read |
3 | Scan is good if you have to retrieves all the rows from the table. | Seek is used if we need selected rows from the table |
4 | It reads every row in the table whether or not it qualifies | It reads rows that qualify and pages that contain these qualifying rows |
5 | Scan cost is proportional to the total number of rows in the table | The cost is proportional to the number of qualifying rows and pages rather than to the total number of rows in the table. |
6 | If Scan is table scan means table doesn’t have index | Seek is always on index only |
7 | If table is having very small no. of rows and its not very frequently used, scan and seek will be almost same | If table is having huge no. of records then seek will give huge performance impact. |
8 | When SQL Server does a scan it loads the complete object which it wants to read from disk into memory, then reads through that object from top to bottom looking for the records that it needs. | It knows where in the index data is going to be, so goes directly to the part of the index that it needs and load to memory |
9 | You never want to convert seek to scan but if you just want it, then drop index | By addin column in where clause and select option in index, we can convert a scan into seek operation. |
10 | A scan means whole data irrespective of data you want | A seek means that you are looking for specific value(s) and the index provides you with the best way to do it |
11 | Scan can be possible with index or table | Seek is always with index , Db engine search data in B tree |
12 | Scan is of 3 types clustered index scan , non clustered index scan , table scan | Seek is of two types clustered index seek and nonclustered index seek |
13 | An Non Clustered index scan is a complete scan of all the leaf pages in B tree to to find index key of cluster index | An Non clustered index seek is a seek through the B-tree structure of a non-clustered index to find index key of clusterindex leaf page, from the root down to the leaf |
14 | A clustered index scan is a complete scan of all data pages at leaf in a clustered index | A clustered index seek is a seek through the b-tree structure of a clustered index, from the root down to the leaf |
15 | Nonclustered index scan means lot fewer pages then in clustered index scan | Nonclustered index seek means only pages required for data address, where as in clustered index seek it only reads data pages. |
16 | Order of data doesnt impact much | Search is fast beacause data is stored in order based on the clustered index key |
17 | Fragmented data affects scan most as whole data need to be read from disk | fragmented data affects but not as compare to scan, as sql engine reed minimal required data. |
18 | Scan is genreally not good , some queries it will use a table scan even when a useful index is present - this is usually because the data in the table is so small that it is more hassle to traverse the indexes (if this is the case you would expect the plan to change as the data grows, assuming the selectivity measure of the index is good). | seek is generally much better, but a great many seeks (caused by bad query design with nasty correlated sub-queries for instance, or because you are making many queries in a cursor operation or other loop) can be worse than a scan, especially if your query may end up returning data from most of the rows in the affected table. |
19 | ||
20 | Table scan only appears for a heap ie table without a clustered index. The first page in the heap is located based on info in the system tables, and then the pages are read one by one, using the next and, if necessary, previous pointers in the page headers. This is generally an expensive operation and should be avoided where ever possible | Seek is not possible without index |
21 | Clustered index scan is similar to table scan, just on a table that has clustered index. This operation reads the leaf pages of the clustered index, using the next and previous page pointers. Like with the table scan, this can be an expensive operation and should, wherever possible be avoided | Clustered index seek uses the clustered index’s b-tree structure. The seek starts at the root of the tree and navigates down the levels of the index until it reached the leaf page(s) with the desired data. This operation also appears when a partial scan of the table is done, when the index’s tree is used to locate a page, and the index is scanned from that point until another point in the table (possibly the end). |
22 | Index scan means reading all the leaf pages of a non-clustered index using the next and previous page pointers to navigate. Because non-clustered indexes generally have fewer pages in the leaf than a clustered index, this operation is usually cheaper than a clustered index scan | Non-clustered index seek is the same as the clustered index seek, just using a non-clustered index |
23 | This some times comes with lookups (Row \ Bookmark) , apart of scanning , Another idex is used to over result of scan | In a seak not all of the index is considered. Only one used index is enough |
26 | We can see scan is horizontal arrow | Seek is vertical arrow explain its nature of work |
CODE :
create table employee (id int , name varchar (20) , age int)
GO
insert into employee values (1 , 'saurabh', 25)
insert into employee values (2 , 'Ravi', 35)
insert into employee values (3 , 'Anil', 25)
insert into employee values (4 , 'Suresh', 45)
GO
select* from employee
---> Table scan
Create clustered index pk_employee_id on employee(id)
Select * from employee
---> Clusterd index scan
Select * from employee where id= 1
---> Clustered index seek
Create nonclustered index ix_employee_age on employee(age)
Select age from employee where age = 35
---> Non Clustered index seek
You might like to read: What is the Difference Between Rebuild and Reorganize indexes ; Interview Question
Thank you sir :)
ReplyDeleteNice Article !
ReplyDeleteReally this will help to people of Database Community.
I have also prepared small note on this, What is difference between Table Scan, Index Scan and Index Seek?.
http://www.dbrnd.com/2016/04/database-theory-table-scan-vs-index-scan-vs-index-seek/
SQL - Index Scan Vs Table Scan
ReplyDeleteIn absence of the indexes, A table scan will work on the data pages and this scanning starts from the first page to the last page for the data and in this way, scanned table stands on a heap and these data rows have their own pages. In the table scan, every row of data goes into the data scanning. If we have the huge data in the table and no index is not there then the query execution cost will increase due to high volume table scan.
SQL - Index Scan Vs Table Scan