
NOLOCK Is Not a Free Speed Boost: Dirty Reads, Missed Rows and Better Options
- كاتب موثّق
- SQL Server 2025
محتويات المقال
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
WITH (NOLOCK) just makes reads faster, and the worst case is occasionally seeing data that's about to be committed anyway.
What actually happens
NOLOCK is READ UNCOMMITTED for one table
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
NOLOCK report can show the money missing.
Rows go missing or appear twice
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.
-- 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 timeNOLOCK 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
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.
-- "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-- Once per database; needs no other active connectionsALTER DATABASE Shop SET READ_COMMITTED_SNAPSHOT ON;-- No hint: the last committed version, no shared locksSELECT SUM(Total) AS RevenueFROM dbo.OrdersWHERE OrderDate = '2026-11-02';-- Test: 50000.00 in 0 ms while the same order was openNOLOCK 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.
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.
NOLOCK | ||||
READ COMMITTED, locking | ||||
SNAPSHOT |
How to apply it in your project
Inventory the hints List every NOLOCK,READUNCOMMITTEDandREAD UNCOMMITTEDin procedures, views and application SQL, and note which ones feed money, stock or anything users act on.Find the blocking each one was hiding While the workload runs, look at blocking_session_idandwait_typeinsys.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.Turn on RCSI in a test environment first Make sure tempdbor 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 orUPDLOCK.Remove the hints, then consider optimized locking Drop 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
NOLOCKequalsREAD 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 NOLOCKhint overrides RCSI, so turning RCSI on only helps once the hints are gone.
التعليقات (0)