skip to content

In SQL Server, why does an execution plan show a Key Lookup beside an Index Seek, and how does an INCLUDE column remove it?

level: middleimportance: must knowfreq 72%

answer

  1. what the index leaf does not hold
  2. Nested Loops back to the base table
  3. the heap version of the operator
  4. leaf-only, non-key columns

basics

~20 s

A Key Lookup means the nonclustered index lacks columns the query needs, so SQL Server fetches each matching row from the clustered index. Adding those columns with INCLUDE stores them in the index leaf, which removes the lookup.

solid answer

~50 s

A **Key Lookup** is SQL Server's bookmark lookup into the clustered index. The seek on the nonclustered index found the matching rows, but the query also needs columns that index doesn't store, so a Nested Loops join uses each row's clustering key to fetch the rest. On a heap the same step is a **RID Lookup**. Each lookup is a separate random read, so the cost grows with the number of rows, and past some point the optimizer prefers to scan the clustered index. `INCLUDE (col, ...)` stores extra columns at the leaf level only: the index then covers the query without a wider key, the included columns don't count toward the 900- or 1,700-byte key limit, and they can be types a key can't use. The clustering key is already in every nonclustered index, so you never need to include it.

code

sql · 10 lines
sql
-- Before: Index Seek on IX_Orders_CustomerId plus a Key Lookup per row
SELECT OrderId, Status, Total
FROM dbo.Orders
WHERE CustomerId = 42;

-- After: the index covers the query; OrderId comes with the clustering key
CREATE NONCLUSTERED INDEX IX_Orders_CustomerId
ON dbo.Orders (CustomerId)
INCLUDE (Status, Total)
WITH (DROP_EXISTING = ON);

go deeper

for a junior

Recognise Key Lookup and RID Lookup in a plan as extra trips to the table for columns the index doesn't hold.

for a middle

Explain the Nested Loops pattern, the per-row cost, and how INCLUDE covers a query without widening the key. Know the type rules and that the clustering key comes along for free.

for a senior

Decide which lookups are worth removing by how often they run, and weigh the write and storage cost of each included column against the read it saves.

for a principal

Set a team rule for covering indexes, for example which queries earn one, so nonclustered indexes don't quietly turn into copies of the table.

## What the lookup operators are When a query uses a nonclustered index but needs columns that index doesn't hold, SQL Server has to go back to the base table for them. The execution plan shows this as one of two **bookmark lookup** operators: - **Key Lookup**: used when the table has a clustered index (or a clustered columnstore index). The nonclustered row carries the clustering key, and the engine seeks the clustered index with it. With a clustered columnstore index, the operator's properties show a columnstore location (`ColStoreLoc`) instead of a clustering key. - **RID Lookup**: used when the table is a heap. The nonclustered row carries an 8-byte RID, and the engine reads that exact page and slot. Both are always driven by a **Nested Loops** operator: for each row the seek returns, one lookup runs. When the properties show `WITH PREFETCH`, the engine has decided to read ahead asynchronously for those lookups. ## Why it costs what it costs Each Key Lookup is a seek through the clustered index's B+ tree, from the root to a leaf page, often a different page for every row. For ten rows that's nothing. For a hundred thousand rows it is a hundred thousand random page reads, and the optimizer's estimate decides whether it accepts that or scans the clustered index instead. So a Key Lookup is also why a seemingly perfect index is sometimes ignored: once the lookups cost more than reading the whole table, the scan wins. The operator reference says the same thing from the other side: a Key Lookup with many executions relative to the table size may benefit from a covering index, while a lookup that runs only a few times is typically not a concern. ## INCLUDE: SQL Server's non-key columns `CREATE INDEX ... INCLUDE (...)` adds **non-key columns** to the leaf level of a nonclustered index. The rules: - Included columns exist only on **nonclustered** indexes, on tables or indexed views. - Any data type is allowed except `text`, `ntext` and `image`, so `varchar(max)` and `nvarchar(max)` can be included even though they can't be key columns. - A column can't appear in both the key list and the `INCLUDE` list, and can't be repeated in `INCLUDE`. - An index needs at least one key column and can have up to 1,023 included columns. - Included columns don't count toward the key limits: up to 32 key columns and 900 bytes for a clustered key or 1,700 bytes for a nonclustered key. | | Key column | Included column | | --- | --- | --- | | Stored at | Every level of the B+ tree | Leaf level only | | Sorted, usable for seeks | Yes | No | | Counts toward key size and column limits | Yes | No | | Allowed types | No LOB types in the key | Everything except `text`, `ntext`, `image` | Put a column in the key when the query filters, joins, groups or sorts on it. Put it in `INCLUDE` when the query only returns it. ## What you get for free and what you pay The clustering key is already in every nonclustered index: as part of the key for a non-unique nonclustered index, and as an included column for a unique one. A query that returns the clustering key is therefore covered without listing it anywhere. The costs are real, though: 1. Wider leaf rows mean fewer rows per page, more pages to read and more memory to cache them. 2. Included `(max)` columns are copied into the index leaf, so the data now lives in both the table and the index. 3. Every `INSERT`, `DELETE` and every `UPDATE` of an included column has to maintain the index as well as the table. ## A worked fix The query below seeks `IX_Orders_CustomerId` and then runs a Key Lookup per row to fetch `Status` and `Total`. Adding them as included columns, with `DROP_EXISTING = ON` to replace the index under the same name, leaves an Index Seek and no lookup. `OrderId` is the clustering key, so it is already in the index. Check the actual plan afterwards: the Key Lookup and its Nested Loops should be gone. ```sql CREATE NONCLUSTERED INDEX IX_Orders_CustomerId ON dbo.Orders (CustomerId) INCLUDE (Status, Total) WITH (DROP_EXISTING = ON); ``` What makes an index "covering" in general is engine-neutral material. On SQL Server the practical skill is reading the lookup operator, checking which columns it fetches and how often it runs, and deciding which of them are worth an `INCLUDE`.

  • Should you remove every Key Lookup you find in your plans?
    No. A lookup that runs a handful of times costs almost nothing. It matters when it runs many times compared with the table's size. Covering every query with INCLUDE lists turns nonclustered indexes into partial copies of the table that every write must maintain, so cover the hot, frequent queries and accept lookups on the rest.
  • Can a Key Lookup appear on a table stored as a clustered columnstore index?
    Yes. When a nonclustered B-tree index sits on a clustered columnstore table, the lookup is still shown as Key Lookup, but it locates the row by its columnstore location (ColStoreLoc) rather than by a clustering key, because a columnstore index has no key columns.

  • Included columns can be sought on, just like key columns.
  • INCLUDE columns count toward the 900-byte index key limit.
  • You must list the clustering key in INCLUDE to cover a query.
  • A Key Lookup means the index is corrupt and needs rebuilding.
  • Including a varchar(max) column costs nothing because it stays off-row.