Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Bookmark Lookup in Sql Server?

I'm in the process of trying to optimize a query that looks up historical data. I'm using the query analyzer to lookup the Execution Plan and have found that the majority of my query cost is on something called a "Bookmark Lookup". I've never seen this node in an execution plan before and don't know what it means.

Is this a good thing or a bad thing in a query?

like image 827
Gavin Miller Avatar asked May 04 '09 16:05

Gavin Miller


People also ask

What is lookup SQL Server?

Lookups are an intuitive table linking syntax provided to simplify data integration and SQL queries. They represent foreign key relationships between tables, and once established, can be used to "expose" columns from the "target" of the lookup in the source table or query.

What is key lookup and rid lookup?

A key lookup occurs when data is found in a non-clustered index, but additional data is needed from the clustered index to satisfy the query and therefore a lookup occurs. If the table does not have a clustered index then a RID Lookup occurs instead.

How do I delete a bookmark in SQL Server?

To add or remove a bookmark, place the insertion point on the desired line in the Editor, and then click Toggle a bookmark.


3 Answers

A bookmark lookup is the process of finding the actual data in the SQL table, based on an entry found in a non-clustered index.

When you search for a value in a non-clustered index, and your query needs more fields than are part of the index leaf node (all the index fields, plus any possible INCLUDE columns), then SQL Server needs to go retrieve the actual data page(s) - that's what's called a bookmark lookup.

In some cases, that's really the only way to go - only if your query would require just one more field (not a whole bunch of 'em), it might be a good idea to INCLUDE that field in the non-clustered index. In that case, the leaf-level node of the non-clustered index would contain all fields needed to satisfy your query (a "covering" index), and thus a bookmark lookup wouldn't be necessary anymore.

Marc

like image 100
marc_s Avatar answered Oct 20 '22 22:10

marc_s


It's a NESTED LOOP which joins a non-clustered index with the table itself on a row pointer.

Happens for the queries like this:

SELECT  col1
FROM    table
WHERE   col2 BETWEEN 1 AND 10

, if you have an index on col2.

The index on col2 contains pointers to the indexed rows.

So, in order to retrieve the value of col1, the engine needs to scan the index on col2 for the key values from 1 to 10, and for each index leaf, refer to the table itself using the pointer contained in the leaf, to find out the value of col1.

This article points out that a Bookmark Lookup is SQL Server 2000's term, which is replaced by NESTED LOOP's between the index and the table in SQL Server 2005 and above

like image 39
Quassnoi Avatar answered Oct 20 '22 22:10

Quassnoi


From MSDN regarding Bookmark Lookups:

The Bookmark Lookup operator uses a bookmark (row ID or clustering key) to look up the corresponding row in the table or clustered index. The Argument column contains the bookmark label used to look up the row in the table or clustered index. The Argument column also contains the name of the table or clustered index in which the row is looked up. If the WITH PREFETCH clause appears in the Argument column, the query processor has determined that it is optimal to use asynchronous prefetching (read-ahead) when looking up bookmarks in the table or clustered index.

like image 35
Gavin Miller Avatar answered Oct 20 '22 20:10

Gavin Miller