SQL SERVER – Index Seek Vs. Index Scan (Table Scan)
Index Scan retrieves all the rows from the table. Index Seek retrieves selective rows from the table.
Index Scan:
Since a scan touches every row in the table whether or not it qualifies, the cost is proportional to the total number of rows in the table. Thus, a scan is an efficient strategy if the table is small or if most of the rows qualify for the predicate.
Since a scan touches every row in the table whether or not it qualifies, the cost is proportional to the total number of rows in the table. Thus, a scan is an efficient strategy if the table is small or if most of the rows qualify for the predicate.
Index Seek:
Since a seek only touches rows that qualify and pages that contain these qualifying rows, the cost is proportional to the number of qualifying rows and pages rather than to the total number of rows in the table.
Since a seek only touches rows that qualify and pages that contain these qualifying rows, the cost is proportional to the number of qualifying rows and pages rather than to the total number of rows in the table.
Index Scan is nothing but scanning on the
data pages from the first page to the last page. If there is an index
on a table, and if the query is touching a larger amount of data, which
means the query is retrieving more than 50 percent or 90 percent of the
data, and then optimizer would just scan all the data pages to retrieve
the data rows. If there is no index, then you might see a Table Scan
(Index Scan) in the execution plan.
Index seeks are generally preferred for
the highly selective queries. What that means is that the query is just
requesting a fewer number of rows or just retrieving the other 10 (some
documents says 15 percent) of the rows of the table.
In general query optimizer tries to use
an Index Seek which means that optimizer has found a useful index to
retrieve recordset. But if it is not able to do so either because there
is no index or no useful indexes on the table then SQL Server has to
scan all the records that satisfy query condition.
down vote
accepted
|
An index scan is where SQL server reads the whole of the
index looking for matches - the time this takes is proportional to the
size of the index.
An index seek is where SQL server uses the b-tree structure of the index to seek directly to matching records (see http://mattfleming.com/node/192 for an idea on how this works) - time taken is only proportional to the number of matching records.
|
The basic rule to follow is Scans are bad, Seeks are good.
Index Scan When SQL Server does a scan it loads the 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. Index Seek When SQL Server does a seek it knows where in the index that the data is going to be, so it loads up the index from disk, goes directly to the part of the index that it needs and reads to where the data that it needs ends. This is obsouly a must more efficient operation than a scan, as SQL already knows where the data is that it is looking for. |
No comments:
Post a Comment