SQL fundamentals learning path Level 3 This level · 1 of 6
Medium sql database

What is a Key Lookup?

When a nonclustered index finds the matching rows but does not contain every column the query needs, SQL Server retrieves those columns from the base table. On a clustered table, this operation is called a Key Lookup; on a heap, it is a RID Lookup. A high number of Key Lookups relative to table size can indicate that a covering index would help.

A Key Lookup can also create a deadlock. The SELECT first takes a shared lock on the nonclustered index entry, then needs a lock on the clustered key. A concurrent UPDATE typically takes an exclusive lock on the clustered key first, then has to maintain the nonclustered index. Those two lock orders form a cycle. A covering index removes the Key Lookup, so the SELECT never has to lock the clustered key for that lookup.

SELECT order_id, customer_id, total
FROM Orders
WHERE customer_id = 42;
-- if IX_Orders_Customer does not include total, each hit needs a Key Lookup
Sources (2)
  1. Microsoft Learn — Key Lookup and RID Lookup showplan operatorslearn.microsoft.com
  2. Microsoft Learn — Deadlock Troubleshooting, Part 3learn.microsoft.com
Previous
Next