Almost every SQL Server codebase I review has WITH (NOLOCK) somewhere. Usually everywhere. Ask why, and the answer is always the same: "so reports don't block the application, and in the worst case we read a row that gets rolled back".
That worst case is not the worst case. With NOLOCK, a query can read the same row twice, or skip rows that were there all along. Totals come out wrong, exports contain duplicate keys, and nobody can reproduce it the next day.
What NOLOCK really changes
NOLOCK is the same as READ UNCOMMITTED for that table. Besides not taking shared locks, it allows SQL Server to read a table in a different way.
Normally a scan follows the index in key order, from one page to the next. Under READ UNCOMMITTED, for larger tables the engine can choose an allocation-order scan: it reads pages in the order they are allocated in the file, which is often faster.
Now add a concurrent insert or update that makes a row bigger. The page is full, so SQL Server splits it: half of the rows move to a new page.
- If the new page is after the point the scan has reached, the moved rows are read again. Counted twice.
- If the new page is before it, the moved rows are never read. Missing.
Even in key-order scans, a row whose key is updated can jump ahead of or behind the scan. Under READ UNCOMMITTED nothing stops that. Sometimes the movement is so bad that the query fails with error 601, "Could not continue scan with NOLOCK due to data movement".
See it happen
On a test database, create a table where rows keep growing and shrinking. The number of rows never changes:
CREATE TABLE dbo.SplitDemo (
Id uniqueidentifier NOT NULL DEFAULT NEWID() PRIMARY KEY CLUSTERED,
Pad varchar(4000) NOT NULL DEFAULT ''
);
GO
INSERT dbo.SplitDemo DEFAULT VALUES;
GO 50000
In session 1, keep making random rows bigger and smaller, so pages split:
WHILE 1 = 1
BEGIN
UPDATE TOP (500) dbo.SplitDemo
SET Pad = CASE WHEN Pad = '' THEN REPLICATE('x', 3000) ELSE '' END
WHERE Id > NEWID();
END
In session 2, count the rows with NOLOCK. The right answer is always 50,000:
DECLARE @i int = 0, @c int;
WHILE @i < 200
BEGIN
SELECT @c = COUNT(*) FROM dbo.SplitDemo WITH (NOLOCK);
IF @c <> 50000 PRINT CONCAT('Wrong count: ', @c);
SET @i += 1;
END
On most machines you see wrong counts within seconds, both above and below 50,000. Now imagine that table is Payments and the query is the month-end total.
Find it in your code
SELECT OBJECT_SCHEMA_NAME(m.object_id) AS schema_name,
OBJECT_NAME(m.object_id) AS object_name
FROM sys.sql_modules AS m
WHERE m.definition LIKE '%NOLOCK%'
OR m.definition LIKE '%READ UNCOMMITTED%'
ORDER BY schema_name, object_name;
That covers procedures, views and functions. Don't forget queries in the application code, in SSRS reports and in ETL packages.
What to use instead: Read Committed Snapshot
The real goal behind NOLOCK is "readers should not block writers". SQL Server has had a proper answer since 2005: Read Committed Snapshot Isolation (RCSI). Readers see the last committed version of each row, they don't take shared locks, and they never see half-moved data.
-- is it already on?
SELECT name, is_read_committed_snapshot_on
FROM sys.databases;
-- switch it on (needs a moment with no other connections)
ALTER DATABASE Sales SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
It is not free, so plan it like any change:
- Row versions are kept in tempdb (or in the database itself if Accelerated Database Recovery is on). Watch the version store, and watch long-running transactions that keep old versions alive.
- Modified rows get 14 extra bytes for versioning information.
- Code that relies on readers being blocked, for example "read a row from a queue table, then update it", must be reviewed. Use
UPDLOCKandREADPASTwhere you really need that behaviour.
Once RCSI is on, the NOLOCK hints are no longer needed. Remove them gradually, starting with the queries that feed money-related reports.
Is NOLOCK ever fine?
Yes, for queries where approximate is good enough and nobody makes decisions on the result: a quick look at a monitoring table, a rough row count during a migration. Not for anything that ends up in a report, an export or an invoice.
If you are not sure how much of your code depends on NOLOCK, or whether RCSI is safe for your workload, that is exactly the kind of question I answer in a health check.
