A SQL Server report blocks checkout writes and the team proposes WITH (NOLOCK) everywhere; what can those reads return, and what should you do instead?
answer
- a table-level READ UNCOMMITTED
- rows that move while you scan
- page splits and allocation-order scans
- an error numbered 601
- versions instead of no locks
basics
~20 sNOLOCK equals READ UNCOMMITTED for that table: a report can read changes later rolled back, miss rows during page splits, read moved rows twice, and fail with error 601. READ_COMMITTED_SNAPSHOT removes the blocking without dirty reads.
solid answer
~50 s`WITH (NOLOCK)` is the `READUNCOMMITTED` table hint: the scan takes no shared locks and ignores other sessions' exclusive locks. So a report can count an order whose transaction later rolls back, and because nothing holds rows still, it can read a row twice or miss it when a concurrent update moves its index key, or miss untouched rows when it uses an allocation-order scan while another transaction splits pages. A scan can even fail with error 601, "Could not continue scan with NOLOCK due to data movement". It does not remove every wait either: the query still takes a schema stability lock and queues behind DDL. SQL Server's answer is row versioning: with `READ_COMMITTED_SNAPSHOT ON` the report reads the last committed version of each row without blocking or being blocked by checkout writers, and `SNAPSHOT` isolation covers a report that needs one consistent view across several statements.
go deeper
Recall that NOLOCK means READ UNCOMMITTED for that table, so the report can show data that was never committed.
Explain the anomalies beyond dirty reads: rows read twice or skipped as keys move, rows missed in allocation-order scans during page splits, and error 601.
Show that you would fix the blocking with row versioning, remove the hints afterwards, and still check the report's plan, rather than trade correct totals for speed.
Weigh a database-wide READ_COMMITTED_SNAPSHOT against SNAPSHOT for reports only, and set a team rule that NOLOCK never appears in code producing financial numbers.
## What the hint actually does In SQL Server, `NOLOCK` is a **table hint** and is documented as equivalent to `READUNCOMMITTED`. It changes the isolation of one table reference in one query: the scan issues **no shared (`S`) locks** to stop others modifying what it reads, and other sessions' **exclusive (`X`) locks don't block it**, so it reads rows whose changes are not yet committed. That is why it appears to cure the reporting problem. On a database where `READ_COMMITTED_SNAPSHOT` is `OFF`, which is the SQL Server default, a report at `READ COMMITTED` takes `S` locks as it reads and waits behind every checkout transaction holding `X` locks; checkout writers in turn wait for the report's `S` locks to clear. Removing the report's locks removes that contention, but it also removes the guarantees the locks provided. ## What a NOLOCK report can return A report that relies on the hint can return: 1. **Dirty data.** An order inserted or updated by a checkout transaction that later rolls back can be counted, so totals include money that was never taken. 2. **The same row twice, or not at all.** If a concurrent update changes the index key of a row the scan is walking, the row can move ahead of the scan and be read again, or behind it and be skipped. Lock-based `READ COMMITTED` releases row locks as it goes and can suffer this too; `SERIALIZABLE` or row versioning prevent it. 3. **Missing rows nobody touched.** When SQL Server reads a table in allocation order, following its IAM pages, a page split caused by another transaction can move rows into a part of the table the scan has already passed. The documentation lists this as a `READ UNCOMMITTED` risk that does not occur at `READ COMMITTED`. 4. **An error instead of a result.** A scan can fail with **error 601**, "Could not continue scan with NOLOCK due to data movement". The documented handling is the same as for a deadlock: retry the statement. ## What NOLOCK does not fix - **Schema locks.** `NOLOCK` applies to data locks only. Every query still takes a schema stability (`Sch-S`) lock to compile and run, so it queues behind a schema modification (`Sch-M`) lock held by DDL such as an index rebuild or `ALTER TABLE`, and its own `Sch-S` blocks that DDL in turn. - **Writes.** The optimizer ignores `NOLOCK` and `READUNCOMMITTED` on the target table of an `UPDATE` or `DELETE`, and support for the hint in that position is slated for removal. A write always locks what it changes. - **Writer-against-writer blocking.** Two checkouts updating the same rows still wait for each other. ## What to do instead | Option | Reader blocks writers? | Dirty reads? | What it costs | |---|---|---|---| | `WITH (NOLOCK)` on the report | No | Yes, plus double and missed rows | Wrong numbers, error 601 | | `READ_COMMITTED_SNAPSHOT ON` | No | No, reads the last committed version per statement | Versioning for all writes, a database-wide behaviour change | | `SNAPSHOT` for the report session | No | No, one view for the whole transaction | `ALLOW_SNAPSHOT_ISOLATION ON` plus versioning | | A better index or a narrower query | Less often | No | Index maintenance on writes | For the scenario, the usual answer is row versioning: - Turn on `READ_COMMITTED_SNAPSHOT` if the application can accept non-blocking reads everywhere; existing reports then stop blocking checkout with no query change, and the `NOLOCK` hints should be **removed**, because a table hint still overrides the session's isolation and would keep the dirty reads. - Or enable `ALLOW_SNAPSHOT_ISOLATION` and run only the report at `SNAPSHOT`, which leaves every other reader on the lock-based behaviour. - Fix the report's plan as well: a report that scans the whole orders table at the busiest hour is a candidate for an index whatever the isolation level. ## Where the hint is tolerable `NOLOCK` remains defensible only where a wrong, duplicated or missing row is harmless: an approximate row count on a monitoring screen, or an exploratory query a DBA runs and reads by eye. Anywhere a total, a balance or an export is involved, the anomalies above are bugs that cannot be reproduced on demand, which is the worst kind to have in a financial report.
- Does putting NOLOCK on the target table of an UPDATE or DELETE in SQL Server stop that statement from locking?No. SQL Server's optimizer ignores `NOLOCK` and `READUNCOMMITTED` on the target table of an `UPDATE` or `DELETE`, so the modification takes its normal locks, and Microsoft documents that support for the hint in that position will be removed in a future version. A write always locks the rows it changes.
- After READ_COMMITTED_SNAPSHOT is turned on in SQL Server, what does an existing WITH (NOLOCK) hint on a report do?It still applies to that table. A table hint overrides the session's isolation level, so the scan keeps reading uncommitted rows instead of the committed versions RCSI would give it. Once RCSI is on, the hints buy nothing but the anomalies, so remove them.
- NOLOCK only risks seeing data that is later rolled back.
- A query with NOLOCK on every table can never be blocked.
- NOLOCK on an UPDATE's target table makes the update lock-free.
- READ_COMMITTED_SNAPSHOT also reads uncommitted rows, so it is NOLOCK under another name.
- Under NOLOCK every row is still read exactly once per scan.