What even is Optimized Locking?
If you've spent any time tuning busy OLTP workloads on SQL Server, you've made peace with locks. LCK_M_X waits in your DMVs, blocking chains three levels deep, that one rogue UPDATE holding an exclusive row lock while twelve other sessions twiddle their thumbs. Classic.
SQL Server 2025 ships with Optimized Locking, a fundamental rethink of how the engine acquires and holds row-level locks. The pitch: dramatically fewer locks in memory, far fewer lock escalations, less blocking, and better throughput on concurrent workloads, mostly without changing a line of application code (see the behavior change gotcha below).
CorrectedOptimized Locking first shipped in Azure SQL Database, where it's always on. It's also always on in SQL database in Microsoft Fabric and in Azure SQL Managed Instance on the Always-up-to-date or SQL Server 2025 update policy. It was never in SQL Server 2022. In SQL Server 2025 it's off by default, and you turn it on per database. Let's dig in.
Required settings: the full checklist
CorrectedOptimized Locking builds on other database features. ADR is the only hard requirement, but you want RCSI as well to get the full benefit:
ALTER DATABASE StackOverflow SET ACCELERATED_DATABASE_RECOVERY = ON;ALTER DATABASE StackOverflow SET READ_COMMITTED_SNAPSHOT ON;ALTER DATABASE StackOverflow SET OPTIMIZED_LOCKING = ON;master, msdb, tempdb, and model are excluded regardless of settings. NewIt isn't used for temporary tables either.CorrectedThe original checklist listed compatibility level 160+ as a requirement. It isn't one. (Thanks, Dimitri Furman.)
WITH ROLLBACK AFTER 5 is your friend). ADR uses the Persistent Version Store (PVS) inside the user database, so size your data files accordingly on busy systems.
Here's the full setup script to run against a fresh Stack Overflow database copy:
-- Step 1: Enable RCSI (needed for the LAQ half of Optimized Locking) ALTER DATABASE StackOverflow SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK AFTER 5; -- give active connections 5 seconds -- Step 2: Enable Accelerated Database Recovery (required) ALTER DATABASE StackOverflow SET ACCELERATED_DATABASE_RECOVERY = ON; -- Step 3: Enable Optimized Locking (off by default in SQL Server 2025) ALTER DATABASE StackOverflow SET OPTIMIZED_LOCKING = ON; -- Step 4: Verify all three settings at once SELECT name, DATABASEPROPERTYEX(name, 'IsOptimizedLockingOn') AS optimized_locking_on, DATABASEPROPERTYEX(name, 'IsReadCommittedSnapshotOn') AS rcsi_on, DATABASEPROPERTYEX(name, 'IsAcceleratedDatabaseRecoveryOn') AS adr_on FROM sys.databases WHERE name = 'StackOverflow';
CorrectedYou should see 1 in all three columns. If optimized_locking_on is 0, Step 3 didn't run or ADR isn't on. If it's NULL, Optimized Locking isn't available on that instance (anything before SQL Server 2025).
How it actually works
Part 1: Transaction ID (TID) Locking
CorrectedTraditionally, when you update a row, SQL Server takes an exclusive (X) lock on that row's RID or key and holds it until the transaction commits or rolls back. Update 1,000 rows and you're holding 1,000 X locks. Bigger updates don't just keep piling up row locks, though. Once a single statement holds about 5,000 locks on one table, the engine tries to escalate to one table lock. Your 500,000-row update usually ends up holding an exclusive lock on the whole table, and everyone else who needs that table waits.
With TID locking, every row carries the Transaction ID of the last transaction that modified it. Row and page locks are still taken while each row is modified, but each one is released as soon as that row is done. The only lock held until commit is a single X lock on the transaction's own TID. Lock memory stays small however many rows you touch, and lock escalation becomes much less likely. That's the real headline benefit.
When another writer reaches a row you've modified, it takes a shared lock on your TID, so it waits until your transaction finishes. Readers under RCSI don't take locks at all. They read the last committed version of the row, exactly as they did before Optimized Locking.
Thanks to Uwe Ricken and Dimitri Furman for pointing out that the original "500,000 row locks" example ignored lock escalation.
NewIn SQL Server 2025 there's a further refinement called skip index locks. When nothing running under REPEATABLE READ, SERIALIZABLE or similar locking hints is reading the row, many UPDATEs (and INSERTs into heaps) can skip the short-lived row and page locks completely and rely on a page latch plus the TID lock.
CorrectedThe mental model
Old way: put a "DO NOT TOUCH" sticky note on every single row you're modifying and leave them all there until you're done. When you run out of sticky notes, lock the whole room.
New way: put one badge on yourself that says "I'm in transaction #8472." Anyone who wants to change a row you touched sees your badge and waits for you to finish. Anyone who only wants to read it uses the last committed copy and doesn't wait at all. One badge instead of thousands of sticky notes, and no locked rooms.
Part 2: Lock After Qualification (LAQ)
CorrectedLAQ attacks a different problem: unnecessary locking during row scans. In the old model, an UPDATE takes an update (U) lock on each row it scans before evaluating the WHERE clause. Rows that don't qualify get their U lock released; rows that do are converted to an X lock that's held until commit. So if you're running:
UPDATE dbo.Posts SET Score = Score + 1 WHERE OwnerUserId = 22656 -- Jon Skeet, obviously AND PostTypeId = 1;
Corrected...the old engine would take a U lock on every row it touched during the scan, even rows that didn't match OwnerUserId = 22656. Lock, evaluate, release. For every non-qualifying row. On a table scan of Posts looking for one user's posts, that's the difference between millions of transient lock acquisitions and a few thousand. Worse, a U lock conflicts with another writer's X lock, so your scan blocks on rows it was never going to update.
With LAQ (which needs RCSI), the engine checks the predicate against the latest committed version of the row without taking any lock, and only takes an X lock if the row qualifies. That X lock is released as soon as the row is updated. Non-qualifying rows are never locked, so two writers working on different rows don't block each other. If a qualifying row is being changed by another open transaction, the engine waits on that transaction's TID, then rechecks the predicate before it updates the row.
Without Optimized Locking
With Optimized Locking
Demos with the Stack Overflow database
Enough theory. Let's get our hands dirty. These demos assume you've run the setup script above and you're on the Brent Ozar Stack Overflow restore.
ALTER DATABASE StackOverflow SET OPTIMIZED_LOCKING = OFF;
Demo 1: Observe lock counts before and after
Open three query windows. Window 1 starts a transaction that updates Posts and leaves it open. Window 2 counts the locks. Window 3 proves readers don't block.
USE StackOverflow; BEGIN TRANSACTION; -- Small update just to generate locks; it's rolled back at the end UPDATE dbo.Posts SET Score = Score + 1 WHERE OwnerUserId = 22656; -- DON'T COMMIT YET, leave this window open -- ROLLBACK TRANSACTION; <-- run this when done
USE StackOverflow; SELECT tl.request_mode, tl.resource_type, COUNT(*) AS lock_count FROM sys.dm_tran_locks tl JOIN sys.dm_exec_sessions es ON tl.request_session_id = es.session_id WHERE es.is_user_process = 1 AND tl.request_status = 'GRANT' GROUP BY tl.request_mode, tl.resource_type ORDER BY lock_count DESC; -- With Optimized Locking: DATABASE and OBJECT intent locks -- plus one X lock on an XACT (TID) resource. -- Without it: Jon has tens of thousands of posts, so the update -- will most likely have escalated to a single OBJECT X lock. -- If escalation couldn't get the table lock, you'll see -- thousands of KEY X locks instead.
USE StackOverflow; -- Runs freely even while Window 1's transaction is open. -- That's RCSI, not Optimized Locking: readers see the last -- committed version and don't wait for the writer to finish. -- (It works the same with Optimized Locking turned off.) SELECT TOP 10 p.Id, p.Title, p.Score, p.CreationDate FROM dbo.Posts p WHERE p.OwnerUserId = 22656 ORDER BY p.Score DESC; -- Returns immediately. No blocking. Beautiful.
Demo 2: Viewing TID locks directly
SQL Server 2025 exposes TID locks as a new XACT resource type in sys.dm_tran_locks. Here's how to see them while an open transaction is running:
USE StackOverflow; -- Window 1 must still have its open transaction from Demo 1 SELECT tl.resource_type, -- look for 'XACT' rows tl.resource_description, tl.request_mode, tl.request_status, es.session_id FROM sys.dm_tran_locks tl JOIN sys.dm_exec_sessions es ON tl.request_session_id = es.session_id WHERE tl.resource_type = 'XACT' AND es.is_user_process = 1; -- One XACT row per open transaction, not one per row modified. -- That's the whole point.
Demo 3: LAQ lock counts on a selective update
CorrectedUse Extended Events to count the locks taken during a selective update, broken down by resource type. The difference shows up clearly here because the Votes table is large and the predicate is narrow. The original version of this demo filtered on a hard-coded resource type number and read a ring buffer that stops counting once it's full. This version uses a histogram target, so every lock is counted and labelled.
There's no index on Votes.UserId, so this scans the whole table and fires an event for every lock. Use one of the smaller Stack Overflow copies, such as StackOverflow2010, rather than the full-size database.
USE StackOverflow; -- Count every lock acquired in this database, bucketed by resource type CREATE EVENT SESSION [LockCountDemo] ON SERVER ADD EVENT sqlserver.lock_acquired( WHERE sqlserver.database_name = N'StackOverflow' ) ADD TARGET package0.histogram( SET source = N'resource_type', source_type = 0 ); ALTER EVENT SESSION [LockCountDemo] ON SERVER STATE = START; GO BEGIN TRANSACTION; UPDATE dbo.Votes SET BountyAmount = 50 WHERE UserId = 22656 AND VoteTypeId = 9; ROLLBACK; GO -- Read the histogram: how many locks of each type were acquired? SELECT COALESCE(mv.map_value, h.slot_value) AS resource_type, h.lock_count FROM ( SELECT sl.n.value('(value)[1]', 'nvarchar(60)') AS slot_value, sl.n.value('@count', 'bigint') AS lock_count FROM ( SELECT CAST(t.target_data AS XML) AS target_data FROM sys.dm_xe_sessions s JOIN sys.dm_xe_session_targets t ON s.address = t.event_session_address WHERE s.name = N'LockCountDemo' AND t.target_name = N'histogram' ) x CROSS APPLY x.target_data.nodes('HistogramTarget/Slot') sl(n) ) h LEFT JOIN sys.dm_xe_map_values mv ON mv.name = N'lock_resource_type' AND CAST(mv.map_key AS nvarchar(60)) = h.slot_value ORDER BY h.lock_count DESC; DROP EVENT SESSION [LockCountDemo] ON SERVER; -- With LAQ: non-qualifying rows are never locked, so you'll see very -- few KEY locks (possibly none, thanks to skip index locks). -- Without: a U lock on every row the scan reads, so the KEY count -- is roughly the number of rows in Votes.
Demo 4: Two concurrent writers, same table
CorrectedThe classic OLTP nightmare: two sessions updating the same table at the same time. The original version of this demo depended on whether any of Jon's posts happened to match both sessions' filters, so you couldn't be sure what you'd see. This version runs two second writers: one that touches different rows and one that touches the same rows.
USE StackOverflow; BEGIN TRANSACTION; -- Jon's questions only UPDATE dbo.Posts SET Score = Score + 1 WHERE OwnerUserId = 22656 AND PostTypeId = 1; -- Leave open, mimics a slow transaction -- ROLLBACK TRANSACTION; <-- run this when done
USE StackOverflow; -- Jon's answers: same user, but none of the rows Window A changed. -- Without Optimized Locking: the scan takes a U lock on every row, -- hits Window A's X-locked questions and waits. -- With Optimized Locking + LAQ: Window A's rows are checked against -- their last committed version, don't qualify (PostTypeId = 1), -- and are skipped. Finishes straight away. BEGIN TRANSACTION; UPDATE dbo.Posts SET ViewCount = ViewCount + 100 WHERE OwnerUserId = 22656 AND PostTypeId = 2; ROLLBACK;
USE StackOverflow; -- Jon's questions again: a true conflict with Window A. -- This blocks with or without Optimized Locking, as it should. -- With Optimized Locking it waits on Window A's TID (XACT) lock -- rather than a KEY lock. BEGIN TRANSACTION; UPDATE dbo.Posts SET ViewCount = ViewCount + 100 WHERE OwnerUserId = 22656 AND PostTypeId = 1; ROLLBACK;
USE StackOverflow; SELECT s.session_id AS blocked_spid, r.blocking_session_id, SUBSTRING(st.text, (r.statement_start_offset/2)+1, ((CASE WHEN r.statement_end_offset = -1 THEN DATALENGTH(st.text) ELSE r.statement_end_offset END - r.statement_start_offset)/2)+1) AS current_statement, r.wait_type, r.wait_resource, r.wait_time AS wait_ms FROM sys.dm_exec_requests r JOIN sys.dm_exec_sessions s ON r.session_id = s.session_id CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) st WHERE r.blocking_session_id > 0; -- While B1 runs: -- With Optimized Locking: 0 rows, B1 has already finished. -- Without: B1 blocked on Window A, wait_type LCK_M_U. -- While B2 runs: -- With Optimized Locking: B2 blocked, wait_type LCK_M_S_XACT_MODIFY -- and an XACT wait_resource. -- Without: B2 blocked, wait_type LCK_M_U.
Verifying it's active and monitoring
Trust but verify. Here's your all-in-one health check:
SELECT d.name AS database_name, d.is_accelerated_database_recovery_on AS adr_enabled, d.is_read_committed_snapshot_on AS rcsi_enabled, d.is_optimized_locking_on AS optimized_locking, CASE WHEN d.is_optimized_locking_on = 1 AND d.is_read_committed_snapshot_on = 1 THEN 'Full benefit: TID locking + LAQ' WHEN d.is_optimized_locking_on = 1 THEN 'TID locking only: enable RCSI for LAQ' WHEN d.is_accelerated_database_recovery_on = 1 THEN 'Ready: run SET OPTIMIZED_LOCKING = ON' ELSE 'Enable ADR first' END AS summary FROM sys.databases d WHERE d.database_id > 4; -- skip system databases
Keep an eye on the Persistent Version Store too. Since ADR is a prerequisite, long-running transactions can cause PVS to grow:
SELECT pvss.persistent_version_store_size_kb / 1024.0 AS pvs_size_mb, pvss.online_index_version_store_size_kb / 1024.0 AS online_ix_ver_mb, pvss.current_aborted_transaction_count, pvss.aborted_version_cleaner_start_time, pvss.aborted_version_cleaner_end_time, dt.database_transaction_begin_time AS oldest_active_txn FROM sys.dm_tran_persistent_version_store_stats pvss LEFT JOIN sys.dm_tran_database_transactions dt ON pvss.oldest_active_transaction_id = dt.transaction_id AND pvss.database_id = dt.database_id WHERE pvss.database_id = DB_ID('StackOverflow');
NewA few diagnostic additions come with Optimized Locking:
- New wait types for writers waiting on a TID:
LCK_M_S_XACT_MODIFY,LCK_M_S_XACT_READand, rarely,LCK_M_S_XACT. If you have blocking alerts that only look forLCK_M_XorLCK_M_U, add these. - The
lock_after_qual_stmt_abortExtended Event fires when a statement has to be restarted without LAQ because another transaction changed a row underneath it. - The
locking_statsExtended Event reports every few minutes per database: lock escalations, whether TID locking and LAQ are on, and how many queries couldn't use LAQ and why.
Gotchas and things to know
Isolation level interactions
CorrectedLAQ only applies under READ COMMITTED with RCSI turned on. It isn't used under plain locking READ COMMITTED (RCSI off), REPEATABLE READ, SERIALIZABLE or SNAPSHOT. Those queries still get TID locking, just not LAQ. Your serializable hints aren't broken; they just don't get the LAQ benefit.
The original post had a note here saying LAQ still applies under SNAPSHOT isolation. It doesn't, and I've removed the note.
CorrectedHints reduce the benefit
Locking hints don't switch Optimized Locking off. They're still honored, but hints such as UPDLOCK, READCOMMITTEDLOCK, XLOCK and HOLDLOCK make the engine take row or page locks and possibly hold them until the end of the transaction, and they stop LAQ being used. A hint only affects the table it's on, so the other tables in the same statement still get the full benefit.
Legacy code that defensively scatters WITH (UPDLOCK) everywhere will continue to work correctly. It just gets less out of Optimized Locking. Some patterns genuinely need the hint (read a row with UPDLOCK, then update it later), but it's worth auditing to remove the ones that don't.
The original list also said ROWLOCK, PAGLOCK and TABLOCK opt out of Optimized Locking. I couldn't find a source for that, so I've taken it out.
NewOther cases where LAQ isn't used
Besides hints and isolation levels, LAQ is skipped when:
- The table being modified has a columnstore index.
- The statement is a
MERGE. - The statement assigns to a variable, or has an
OUTPUTclause that returns rows or writes to a table variable. - The statement reads the rows it modifies through more than one index seek or scan.
- LAQ's own feedback mechanism has switched it off for that plan or database, because statements kept having to restart.
NewResults can change under RCSI
Because LAQ qualifies rows against the last committed version instead of waiting, an UPDATE can skip a row that it would previously have waited for and then updated. Take a row where b = 1. Session 1 runs UPDATE ... SET b = 2 and hasn't committed. Session 2 runs UPDATE ... SET b = 3 WHERE b = 2. Without LAQ, Session 2 waits, sees b = 2 after the commit, and the row ends up as 3. With LAQ, Session 2 sees the committed b = 1, skips the row, and it ends up as 2.
Code that relies on that kind of strict ordering under RCSI was never guaranteed it. If yours does, use REPEATABLE READ or SERIALIZABLE for those statements, or a READCOMMITTEDLOCK hint.
CorrectedLock escalation becomes rare
This is the big one. Without Optimized Locking, a statement that piles up about 5,000 locks on one table tries to escalate to a table lock, and big batch updates block everything else on that table. With Optimized Locking, row and page locks are released as each row is modified, so statements rarely get near that threshold. Escalation can still happen where locks are held to the end of the transaction, for example under locking hints or REPEATABLE READ and SERIALIZABLE. The locking_stats event counts escalations, so you can see the difference for yourself.
The original section said Optimized Locking "doesn't disable lock escalation" and played down this benefit. Thanks to Dimitri Furman for pointing out that avoiding escalation is the main point.
ADR cleanup and PVS growth
Because ADR stores row versions in the PVS, long-running transactions prevent version cleanup and cause PVS to grow. Monitor sys.dm_tran_persistent_version_store_stats and keep an eye on your longest open transactions. The aborted_version_cleaner fields tell you if cleanup is falling behind.
Deadlock detection changes subtly
With TID locking, some deadlock patterns that existed before will disappear. However, TID-on-TID deadlocks can still occur when writer A is waiting for writer B's TID to commit while writer B is waiting for writer A. CorrectedThese show up in the deadlock report as <xactlock> elements under <resource-list> (not "tidlock", as the original said), so your existing deadlock XE sessions will capture them. See Microsoft's deadlocks guide for an example.
"Optimized Locking is the concurrency upgrade SQL Server has needed for a decade. Turn on ADR, enable RCSI, switch on Optimized Locking, and let the engine work for you."
Requirements: ADR = ON, then OPTIMIZED_LOCKING = ON. RCSI = ON for LAQ. User databases only.NewFurther reading: Optimized Locking on Microsoft Learn. Thanks again to Dimitri Furman and Uwe Ricken for the corrections on LinkedIn.