SQL fundamentals learning path Level 3 This level · 3 of 6
Hard sql sql-server

Can a simple SELECT cause a deadlock?

The 30 second answer

Yes. A SELECT that seeks a nonclustered index and then does a Key Lookup into the clustered index locks those two structures in one order. A concurrent UPDATE typically locks the clustered row first and then has to maintain the nonclustered index, which is the opposite order, so a cycle can form. I prevent it with a covering index, or I turn on READ_COMMITTED_SNAPSHOT so the SELECT no longer takes shared locks on the data. NOLOCK is the wrong fix, because it allows dirty reads.

Could you say this in an interview?

Yes. The SELECT does not need to be a multi-statement transaction, and it does not need to write. If a nonclustered index does not cover the query, the plan seeks that index and then performs a Key Lookup into the clustered index. The SELECT can hold a shared lock on the nonclustered key while it waits for a shared lock on the clustered key. An UPDATE of the same row typically takes an exclusive lock on the clustered key first, then has to update the nonclustered index and waits for an exclusive lock there. Those two lock orders are a cycle.

The clean fix is a covering index, so the SELECT can be answered from the nonclustered index and never visits, or locks, the clustered key for that lookup. If readers and writers collide often, I also consider READ_COMMITTED_SNAPSHOT. With that database option on, READ COMMITTED uses row versions: the statement sees a transactionally consistent snapshot of committed data, and the read does not acquire shared locks on the rows. On SQL Server that option defaults to off; on Azure SQL Database it defaults to on. Changing it with ALTER DATABASE requires that no other connection is in the database until the statement finishes.

I do not use NOLOCK or READ UNCOMMITTED as a deadlock shortcut. Those hints skip shared locks, but they allow dirty reads of data that may never be committed, and they can also miss rows or read the same row twice. Microsoft's recommended way to reduce reader/writer locking without dirty reads is row-versioned READ COMMITTED or SNAPSHOT isolation, not NOLOCK.

SELECT Name, Email
FROM Users
WHERE Email = @email;
-- if IX_Users_Email does not include Name, this seeks the nonclustered index
-- and then does a Key Lookup into the clustered index

CREATE NONCLUSTERED INDEX IX_Users_Email
ON Users (Email)
INCLUDE (Name);
-- covering index: the SELECT no longer needs the Key Lookup

ALTER DATABASE AppDb SET READ_COMMITTED_SNAPSHOT ON;
-- readers use row versions and do not take shared locks on the data

Alternate interview scenarios

"Why is NOLOCK the wrong way to stop a SELECT from deadlocking?"

Because NOLOCK is READ UNCOMMITTED. The read no longer takes shared locks, so it may not collide with a writer, but it can return uncommitted values that later roll back, and a scan can skip rows or see a row twice. I use a covering index to remove the Key Lookup, or I enable READ_COMMITTED_SNAPSHOT so the SELECT reads a committed row version without a shared lock on the data. 123

Sources (5)
  1. Microsoft Learn — Deadlock Troubleshooting, Part 3learn.microsoft.com
  2. Microsoft Learn — Transaction Locking and Row Versioning Guidelearn.microsoft.com
  3. Microsoft Learn — Table Hints (NOLOCK and READUNCOMMITTED)learn.microsoft.com
  4. Microsoft Learn — SET TRANSACTION ISOLATION LEVELlearn.microsoft.com
  5. Microsoft Learn — Deadlocks Guidelearn.microsoft.com
Previous
Next