lock escalation

Sam 1,476 Reputation points
2023-09-05T10:35:03.9733333+00:00

Hi All, 

I am trying to understand the lock escalation behavior in Microsoft SQL Server 2017 (RTM-CU31).
As per my knowledge, lock escalation is done to minimize the lock memory when it is exceeding 5000 locks.

Here, in this example, I have create a table with 100 million records and I am trying to update 1 row (i.e. pk = 3).
Since, there is no index on pk column, I understand that SQL Server has to do a table scan. The point, I am not able to understand is, I tried to capture the trace during the update operation to see how locks are being acquired. I see that locks are being acquired and getting released on each row. if that's the case, why should SQL has to hold all these 5000 locks? Am I, missing anything here? If so, please help me understand the locking behavior. Another observation is, if I create a non-clustered idx on pk column, then there is no lock escalation as it is doing index seek and no table scan.

Here is my demo code.

use master 
go 
create database testdb 
go 

use testdb
go
CREATE TABLE dbo.Sample 
(
    pk integer IDENTITY NOT NULL, 
    i bigint NOT NULL, 
    lob nvarchar(max) NOT NULL
);

-- Add data
INSERT dbo.Sample 
    (i, lob)
SELECT
    i = ROW_NUMBER() OVER (ORDER BY @@SPID),
    lob = CONVERT(nvarchar(max), N'big string')
FROM sys.all_columns AS AC1
CROSS JOIN sys.all_columns AS AC2
go 

select count(*) from Sample; --( 106502400 rows affected)  --100 million rows
go 


select name, lock_escalation_desc from sys.tables where name = 'sample'
go
/*
Sample	TABLE
*/

select count(*) from Sample
where pk=3;
go 
--1 row

BEGIN TRAN
update sample set lob='test string' where pk=3;
--rollback;


NOTE: For some reason, I am not able to attach the .zip file. so, I had to rename .zip to .log. 
Once you are able to download the locktrace.log , rename it to locktrace.zip and unzip it. you should find the trace file inside.

Regards,
Sam 

Locktrace.log
SQL Server | Other
{count} votes

Answer accepted by question author
  1. Erland Sommarskog 129.6K Reputation points MVP Volunteer Moderator
    2023-09-05T21:44:24.77+00:00

    Not sure why you posted all those screen shots as an Answer. Did they actually answer anything? I converted them to a comment.

    Your question was difficult to read since it was a single long line. If you want to get help, you may want to make an effort to make your posts readable.

    Anyway, I ran this:

    BEGIN TRAN
    update Sample set lob='test string' where pk=3
    
    SELECT resource_type, request_mode, resource_associated_entity_id AS id,
           object_name(try_convert(int, resource_associated_entity_id)), 
           COUNT(*) AS [Count]
    FROM   sys.dm_tran_locks
    WHERE  request_session_id = @@spid
    GROUP  BY resource_type, request_mode, resource_associated_entity_id,
              object_name(try_convert(int, resource_associated_entity_id)) 
    
    
    

    And this is what I see:

    User's image

    I don't lock escalation is going on here, but I don't sure. There are 24 locks on object level. The reason that there is more than one is that with a table of this size, there will be parallelism and lock partitions. Lock partitions is a concept that is difficult to grasp, and to be honest, it may be above your own level. (Hint: I have difficulties to grasp the concept myself. And they call me an expert.) Also, because the table is that big, SQL Server may have taken these locks already from the start.

    There is also one intent lock on page and one RID lock, that is a row lock, which is a little puzzling. But that must be for the row begin updated.

    I would recommend that you redo the exercise for a table of a more moderate size. One million rows should do.


1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.