views:

2133

answers:

6

In a SQL Server Execution plan plans what is the difference between an Index Scan and an Index Seek

I'm on sql server 2005

+1  A: 

A scan touches every row in the table even if its what you are after or not

A scan looks only at the rows that are what you are looking for.

Seeks are always better to have than scans as they are more efficient in the way it looks data up.

A good explanation can be found here

AutomatedTester
+1  A: 

With an Index Scan, all rows in the index are being scanned to find a matching row. This can be efficient for small tables. With an Index Seek, it only needs to touch the rows that actually meet the criteria and so is generally more performant

AdaTheDev
A: 

An Index Scan happens when the index definition cannot find on a single row to satisfy search predicates. In this case SQL Server has to scan multiple pages to find a range of rows which satisfy the search predicates.

In the case of a Index Seek, SQL Server finds a single row matching search predicates using index definition.

Index Seeks are better and more effective.

kevchadders
A: 

Index seek is used with equal and in operator (=, in) in the WHERE condition clause

select * from people where pid=7735

Index scan is used with greater, less and between operator (>,<, >=, <=, between)

select * from people where pid<7735
Fima
I'm afraid this is incorrect - SQL server can still do index seeks for > and < operators.
Kragen
+8  A: 

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.

  • In general an index seek is preferable to an index scan (when the number of matching records is proprtionally much lower than the total number of records), as the time taken to perform an index seek is constant regardless of the toal number of records in your table.
  • Note however that in certain situations an index scan can actually be faster than an index seek - usually when the table is very small, or when a large percentage of the records match the predicate.
Kragen
+4  A: 

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.


How can I modify an Execution Plan to use a Seek instead of a Scan?

When SQL Server is looking for your data probably one of the largest things which will make SQL Server switch from a seek to a scan is when some of the columns are you looking for are not included in the index you want it to use. Most often this will have SQL Server fall back to doing a clustered index scan, since the Clustered index contains all the columns in the table. This is one of the biggest reasons (in my opinion at least) that we now have the ability to INCUDE columns in an index, without adding those columns to the indexed columns of the index. By including the additional columns in the index we increase the size of the index, but we allow SQL Server to read the index, without having togo back to the clustered index, or to the table it self to get these values.

References

For information regarding the specifics of each of these operators within a SQL Server Execution plan see....

John Sansom