Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better: Bookmark/Key Lookup or Index Scan

I know that an index seek is better than an index scan, but which is preferable in SQL Server explain plans: Index seek or Key Lookup (Bookmark in SQL Server 2000)?

Please tell me they didn't change the name again for SQL Server 2008...

like image 833
OMG Ponies Avatar asked Sep 08 '09 17:09

OMG Ponies


People also ask

Which is better index seek or index scan?

Index scan means it retrieves all the rows from the table and index seek means it retrieves selective rows from the table. INDEX SCAN: Index Scan touches every row in the table it is qualified or not, the cost is proportional to the total number of rows in the table.

Is index scan better than table scan?

3) index scan is faster than a table scan because they look at sorted data and query optimizers know when to stop and look for another range. 4) index seek is the fastest way to retrieve data and it comes into the picture when your search criterion is very specific.

What is the difference between table scan and index scan?

The key difference between Table Scan and Index Scan is that data is stored in the index tree, the query processor knows it when reaches the end of the current it is looking for. Then it can send the query or move on to the next range of data.

What is an index scan?

An index scan occurs when the database manager accesses an index to narrow the set of qualifying rows (by scanning the rows in a specified range of the index) before accessing the base table; to order the output; or to retrieve the requested column data directly ( index-only access ).


2 Answers

Index seek, every time.

Lookups are expensive, so this is covering indexes and especially the INCLUDE clause was added to make them better.

Saying that if you expect exactly one row, for example, a seek followed a lookup can be better than trying to cover a query. We rely on this to avoid yet another index in certain situations.

Edit: Simple talk article: Using Covering Indexes to Improve Query Performance

Edit, Aug 2012

Lookups happen per row which is why they scale badly. Eventually, the optimiser will choose a clustered index scan instead of a seek+lookup because it's more efficient than many lookups.

like image 76
gbn Avatar answered Oct 07 '22 20:10

gbn


Key lookup is very similar to a clustered index seek (pre 2005 SP2 was named 'seek with lookup'). I think the only difference is that the Key Lookup may specify an additional PRE-FETCH argument instructing the execution engine to prefetch more keys in the cluster (ie. do a clustered index seek followed by scan).

Seeing a Key Lookup should not scare you. Is the normal operator used in Nested Loops, and Nested Loops is the run-of-the-mill join operator. If you want to improve a plan, try improving on the join and see if it can use a merge join instead (ie. both sides of join can provide rows on the same key order, fastest join) or a hash-join (have enough memory for the QO to consider a hash join, or reduce the cardinality by filtering rows before the join rather than after).

like image 36
Remus Rusanu Avatar answered Oct 07 '22 20:10

Remus Rusanu