الانتقال إلى المحتوى
قواعد البياناتSQL Server· 7 دقائق للقراءة

NOLOCK Is Not a Free Speed Boost: Dirty Reads, Missed Rows and Better Options

The hint doesn't just risk an early peek at uncommitted data. It can count rows twice, skip them, and still wait on a schema lock. Row versioning gives readers the same freedom with correct answers.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • SQL Server 2025
محتويات المقال

The finance dashboard showed 70,000.00 for yesterday's revenue, while the accounting export for the same day said 50,000.00. The dashboard query ends with WITH (NOLOCK), added years ago "so the dashboard never blocks checkout". When it refreshed, a 20,000.00 order was halfway through its transaction; the payment failed and the order was rolled back. In a test on SQL Server 2022 with two sessions, session A inserted that order and waited before rolling back. Session B's NOLOCK sum returned 70,000.00 instantly. The same sum without the hint waited about 2 seconds for A to finish and returned 50,000.00.

The common misconception

The belief: WITH (NOLOCK) just makes reads faster, and the worst case is occasionally seeing data that's about to be committed anyway.

You can see why it spreads. Add the hint to a slow, blocked query and it returns at once, and the name promises exactly that: no locks. A dirty read sounds like a timing detail, a value that's a few milliseconds early. Many codebases put the hint on every table, and for years nothing looks wrong. The hint really does stop a read from waiting on row locks. The costs are just harder to see than the benefit.

What actually happens

NOLOCK is READ UNCOMMITTED for one table

Microsoft's table hints reference says NOLOCK is equivalent to READUNCOMMITTED: no shared locks are taken, and exclusive locks held by other transactions don't block the read. It lists the price plainly: it "might generate errors for your transaction, present users with data that was never committed, or cause users to see records twice (or not at all)." That's three problems, and only the first one is the one people expect.

A dirty read isn't an early read

A returns a change that hasn't been committed, and nothing says it ever will be. The order in the hook was rolled back, so 70,000.00 was never true at any moment. The same applies to an import that fails halfway, or a transfer that has debited one account and not yet credited the other: a NOLOCK report can show the money missing.

Rows go missing or appear twice

This is the part most teams don't know about. Microsoft's locking guide explains that a READ UNCOMMITTED read using an allocation order scan, which follows the order pages sit in the file rather than the index order, might miss rows when another transaction causes a . Rows move to a new page, and the scan may already be past it or may still be on its way to it.

In the test, a table held exactly 100,000 rows, and nothing inserted or deleted any. A second session updated random rows to longer values, which split pages. The count below ran 15 times with each version of the query:

SQL nolock-count-test.sql
-- dbo.Events: 100,000 rows, no inserts or deletes during the test.-- Another session kept growing random rows, splitting pages.SELECT COUNT(*) FROM dbo.Events WITH (NOLOCK);-- Test, 15 runs: 15 different answers, from 99,742 to 100,262SELECT COUNT(*) FROM dbo.Events;-- Test, 15 runs: 100,000 every time

The NOLOCK count never once returned the right answer: it came out as much as 258 rows short and 262 rows over. A sum or a report built on such a scan inherits the same error. The docs add that such a read can also fail with error 601, to be handled like a deadlock: retry the statement.

It still waits sometimes

NOLOCK only skips data locks. Every query takes a schema stability (Sch-S) lock, and a schema change holds a Sch-M lock that blocks it. In the test, session A started a transaction with ALTER TABLE ... ADD and held it for 3 seconds. A NOLOCK count started a second later waited 2,015 ms, until A rolled back, and sys.dm_exec_requests showed it waiting on LCK_M_SCH_S. So the hint doesn't even guarantee a dashboard that never waits.

What readers actually want: row versioning

The real goal behind most NOLOCK hints is readers that don't wait for writers. SQL Server has had that since 2005: with (RCSI), each READ COMMITTED statement reads the data as last committed when the statement started, and the locking guide says such reads don't take shared locks, so they don't block writers either.

SQL
-- "So the dashboard never blocks checkout"SELECT SUM(Total) AS RevenueFROM dbo.Orders WITH (NOLOCK)WHERE OrderDate = '2026-11-02';-- Test: 70000.00 while a 20000.00 order was open;-- it rolled back, the committed total was 50000.00

With RCSI on, the plain count also returned 100,000 in all 15 runs while pages split. One detail matters: a NOLOCK hint overrides RCSI. In the same test, the hinted sum still returned 70,000.00, so turning RCSI on doesn't fix queries until you remove the hints.

Row versioning isn't free. Changes keep the previous versions of rows in a version store, in tempdb, or in the database itself when accelerated database recovery (ADR) is on, and a long-running transaction keeps those versions from being cleaned up. Reads at the default level stop waiting for writers, so code that relied on that blocking to read and then update needs a real fix, such as the atomic UPDATE or UPDLOCK from the lost updates article. In SQL Server 2025, optimized locking also reduces the locks writers hold; it's off by default, requires ADR, and its lock after qualification part only works with RCSI on.

OptionUncommitted dataWaits for writersMissed or double rowsMain cost
NOLOCKYes (tested)No, but waits on schema locks (tested)Yes (tested)Wrong answers
READ COMMITTED, lockingNoYes (tested)Only if a key changes mid-scanBlocking
RCSINoNo (tested)No: one snapshot per statementVersion store
SNAPSHOTNoNoNo: one snapshot per transactionVersion store, update conflicts

How to apply it in your project

  1. Inventory the hintsList every NOLOCK, READUNCOMMITTED and READ UNCOMMITTED in procedures, views and application SQL, and note which ones feed money, stock or anything users act on.
  2. Find the blocking each one was hidingWhile the workload runs, look at blocking_session_id and wait_type in sys.dm_exec_requests. Long transactions and scans caused by missing indexes are the usual causes, and fixing them helps every query, not only the hinted one.
  3. Turn on RCSI in a test environment firstMake sure tempdb or the database has room for the version store, then run your tests. Look for code that read a row and then updated it, relying on blocking; fix it with an atomic update or UPDLOCK.
  4. Remove the hints, then consider optimized lockingDrop the hints query by query and compare the results. On SQL Server 2025, enable ADR and optimized locking together with RCSI to reduce writer blocking too.

Key takeaways

  • NOLOCK equals READ UNCOMMITTED: in the test, a sum included a 20,000.00 order that was later rolled back.
  • It can also skip rows or read them twice during page splits: 15 counts of a 100,000-row table gave 15 wrong answers.
  • It doesn't skip schema locks: a hinted count waited about 2 seconds behind an uncommitted ALTER TABLE.
  • RCSI gives readers that don't block on writers with committed, consistent results; its cost is the version store.
  • A NOLOCK hint overrides RCSI, so turning RCSI on only helps once the hints are gone.

📚 المصادر

  1. Table hints (Transact-SQL) — SQL Server, Microsoft Learn
  2. Transaction locking and row versioning guide — SQL Server, Microsoft Learn
  3. ALTER DATABASE SET options (Transact-SQL) — SQL Server, Microsoft Learn
  4. Optimized locking — SQL Server 2025, Microsoft Learn

التعليقات (0)