Read Committed and Large Objects

Published Mar 23 2019 05:02 AM 126 Views
Microsoft
First published on MSDN on May 31, 2007

In my last post , I explained that SQL Server holds read committed locks until the end of an update statement (instead of releasing the locks as soon as each row is released) if there is a blocking operator between the scan or seek of the rows to be updated and the update itself.  In this post, I'll take a look at a similar result involving large objects.


Normally, when SQL Server moves data through a blocking operator such as a sort, it makes a copy of the data.  Once SQL Server makes a copy, there is no need to preserve the original row or source of the data.  However, since large objects (e.g., varchar(max)) can store up to 2 Gbytes, it is generally not practical to make copies of large objects.  Instead, whenever possible, SQL Server uses "pointers" to the data instead of making copies of the data.  To ensure that the pointers remain valid, SQL Server does not release any locks on the rows that contain the large objects until the statement completes.


Let's observe this behavior.  Begin by creating the following table:



create table t (pk int primary key, i int, lob varchar(max))
insert t values (1, 1, 'abc')
insert t values (2, 2, 'def')
insert t values (3, 3, 'ghi')


In session 1 lock the third row:



begin tran
update t set i = i where pk = 3


Now, in session 2 check the spid (we'll use it later to look at the locks) and run this query which scans the table and reads each large object (using the default read committed isolation level):



select @@spid


select lob from t


This query uses a trivial plan which consists of a clustered index scan:


|--Clustered Index Scan(OBJECT:([t].[PK__t__2D27B809]))


Because session 1 has a lock on the third row of the table, the scan blocks.  While it is blocked, we can check which locks it holds by running the following query in session 1:



select resource_type, request_mode, request_type, request_status
from sys.dm_tran_locks
where request_session_id = <session_2_spid>

resource_type request_mode request_type request_status
------------- ------------ ------------ ---------------
DATABASE S LOCK GRANT
PAGE IS LOCK GRANT
KEY S LOCK WAIT
OBJECT IS LOCK GRANT

We see the the scan is not holding any key locks and is waiting for the one key lock held by session 1.


Next, kill the scan in session 2 and try the following query:



select lob from t order by i


The plan for this query includes a sort:


|--Sort(ORDER BY:([t].[i] ASC))
|--Clustered Index Scan(OBJECT:([t].[PK__t__2D27B809]))


As I explained above, when we check the locks held by this query, we find that due to the large object and the blocking sort, this query holds key locks on each row it touches:

resource_type request_mode request_type request_status
------------- ------------ ------------ ---------------
DATABASE S LOCK GRANT
KEY S LOCK GRANT
PAGE IS LOCK GRANT
KEY S LOCK WAIT
OBJECT IS LOCK GRANT
KEY S LOCK GRANT

If you have any doubt whether the extra key locks are due to the large object, repeat this experiment with the following nearly identical query which does not return the large object:



select i from t order by i


Finally, try one more query:



select i from t where lob > 'a'


The plan for this query uses an explicit filter operator to evaluate the predicate on the large object:


|--Filter(WHERE:([t].[lob]>[@1]))
|--Clustered Index Scan(OBJECT:([t].[PK__t__2D27B809]))


The filter is not a blocking operator and the query does return the large object.  Nevertheless, if you run this query, you will observe that SQL Server once again holds locks on each row touched by this query.  In this example, SQL Server is overly conservative and retains the locks even though they are technically unnecessary.

%3CLINGO-SUB%20id%3D%22lingo-sub-383286%22%20slang%3D%22en-US%22%3ERead%20Committed%20and%20Large%20Objects%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383286%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20May%2031%2C%202007%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EIn%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2007%2F05%2F22%2Fread-committed-and-updates.aspx%22%20mce_href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2007%2F05%2F22%2Fread-committed-and-updates.aspx%22%20title%3D%22Read%20Committed%20and%20Updates%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20my%20last%20post%20%3C%2FA%3E%20%2C%20I%20explained%20that%20SQL%20Server%20holds%20read%20committed%20locks%20until%20the%20end%20of%20an%20update%20statement%20(instead%20of%20releasing%20the%20locks%20as%20soon%20as%20each%20row%20is%20released)%20if%20there%20is%20a%20blocking%20operator%20between%20the%20scan%20or%20seek%20of%20the%20rows%20to%20be%20updated%20and%20the%20update%20itself.%26nbsp%3B%20In%20this%20post%2C%20I'll%20take%20a%20look%20at%20a%20similar%20result%20involving%20large%20objects.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENormally%2C%20when%20SQL%20Server%20moves%20data%20through%20a%20blocking%20operator%20such%20as%20a%20sort%2C%20it%20makes%20a%20copy%20of%20the%20data.%26nbsp%3B%20Once%20SQL%20Server%20makes%20a%20copy%2C%20there%20is%20no%20need%20to%20preserve%20the%20original%20row%20or%20source%20of%20the%20data.%26nbsp%3B%20However%2C%20since%20large%20objects%20(e.g.%2C%20varchar(max))%20can%20store%20up%20to%202%20Gbytes%2C%20it%20is%20generally%20not%20practical%20to%20make%20copies%20of%20large%20objects.%26nbsp%3B%20Instead%2C%20whenever%20possible%2C%20SQL%20Server%20uses%20%22pointers%22%20to%20the%20data%20instead%20of%20making%20copies%20of%20the%20data.%26nbsp%3B%20To%20ensure%20that%20the%20pointers%20remain%20valid%2C%20SQL%20Server%20does%20not%20release%20any%20locks%20on%20the%20rows%20that%20contain%20the%20large%20objects%20until%20the%20statement%20completes.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ELet's%20observe%20this%20behavior.%26nbsp%3B%20Begin%20by%20creating%20the%20following%20table%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Ecreate%20table%20t%20(pk%20int%20primary%20key%2C%20i%20int%2C%20lob%20varchar(max))%20%3CBR%20%2F%3E%20insert%20t%20values%20(1%2C%201%2C%20'abc')%20%3CBR%20%2F%3E%20insert%20t%20values%20(2%2C%202%2C%20'def')%20%3CBR%20%2F%3E%20insert%20t%20values%20(3%2C%203%2C%20'ghi')%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EIn%20session%201%20lock%20the%20third%20row%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Ebegin%20tran%20%3CBR%20%2F%3E%20update%20t%20set%20i%20%3D%20i%20where%20pk%20%3D%203%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ENow%2C%20in%20session%202%20check%20the%20spid%20(we'll%20use%20it%20later%20to%20look%20at%20the%20locks)%20and%20run%20this%20query%20which%20scans%20the%20table%20and%20reads%20each%20large%20object%20(using%20the%20default%20read%20committed%20isolation%20level)%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Eselect%20%40%40spid%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eselect%20lob%20from%20t%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EThis%20query%20uses%20a%20trivial%20plan%20which%20consists%20of%20a%20clustered%20index%20scan%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Bt%5D.%5BPK__t__2D27B809%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EBecause%20session%201%20has%20a%20lock%20on%20the%20third%20row%20of%20the%20table%2C%20the%20scan%20blocks.%26nbsp%3B%20While%20it%20is%20blocked%2C%20we%20can%20check%20which%20locks%20it%20holds%20by%20running%20the%20following%20query%20in%20session%201%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Eselect%20resource_type%2C%20request_mode%2C%20request_type%2C%20request_status%20%3CBR%20%2F%3E%20from%20sys.dm_tran_locks%20%3CBR%20%2F%3E%20where%20request_session_id%20%3D%20%3CI%3E%20%3CSESSION_2_SPID%3E%3C%2FSESSION_2_SPID%3E%3C%2FI%3E%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3Eresource_type%20request_mode%20request_type%20request_status%20%3CBR%20%2F%3E%20-------------%20------------%20------------%20---------------%20%3CBR%20%2F%3E%20DATABASE%20S%20LOCK%20GRANT%20%3CBR%20%2F%3E%20PAGE%20IS%20LOCK%20GRANT%20%3CBR%20%2F%3E%20KEY%20S%20LOCK%20WAIT%20%3CBR%20%2F%3E%20OBJECT%20IS%20LOCK%20GRANT%20%3CBR%20%2F%3E%3CP%3EWe%20see%20the%20the%20scan%20is%20not%20holding%20any%20key%20locks%20and%20is%20waiting%20for%20the%20one%20key%20lock%20held%20by%20session%201.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENext%2C%20kill%20the%20scan%20in%20session%202%20and%20try%20the%20following%20query%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Eselect%20lob%20from%20t%20order%20by%20i%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EThe%20plan%20for%20this%20query%20includes%20a%20sort%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Sort(ORDER%20BY%3A(%5Bt%5D.%5Bi%5D%20ASC))%20%3CBR%20%2F%3E%20%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Bt%5D.%5BPK__t__2D27B809%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EAs%20I%20explained%20above%2C%20when%20we%20check%20the%20locks%20held%20by%20this%20query%2C%20we%20find%20that%20due%20to%20the%20large%20object%20and%20the%20blocking%20sort%2C%20this%20query%20holds%20key%20locks%20on%20each%20row%20it%20touches%3A%3C%2FP%3Eresource_type%20request_mode%20request_type%20request_status%20%3CBR%20%2F%3E%20-------------%20------------%20------------%20---------------%20%3CBR%20%2F%3E%20DATABASE%20S%20LOCK%20GRANT%20%3CBR%20%2F%3E%20KEY%20S%20LOCK%20GRANT%20%3CBR%20%2F%3E%20PAGE%20IS%20LOCK%20GRANT%20%3CBR%20%2F%3E%20KEY%20S%20LOCK%20WAIT%20%3CBR%20%2F%3E%20OBJECT%20IS%20LOCK%20GRANT%20%3CBR%20%2F%3E%20KEY%20S%20LOCK%20GRANT%20%3CBR%20%2F%3E%3CP%3EIf%20you%20have%20any%20doubt%20whether%20the%20extra%20key%20locks%20are%20due%20to%20the%20large%20object%2C%20repeat%20this%20experiment%20with%20the%20following%20nearly%20identical%20query%20which%20does%20not%20return%20the%20large%20object%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Eselect%20i%20from%20t%20order%20by%20i%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EFinally%2C%20try%20one%20more%20query%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Eselect%20i%20from%20t%20where%20lob%20%26gt%3B%20'a'%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EThe%20plan%20for%20this%20query%20uses%20an%20explicit%20filter%20operator%20to%20evaluate%20the%20predicate%20on%20the%20large%20object%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Filter(WHERE%3A(%5Bt%5D.%5Blob%5D%26gt%3B%5B%401%5D))%20%3CBR%20%2F%3E%20%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Bt%5D.%5BPK__t__2D27B809%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThe%20filter%20is%20not%20a%20blocking%20operator%20and%20the%20query%20does%20return%20the%20large%20object.%26nbsp%3B%20Nevertheless%2C%20if%20you%20run%20this%20query%2C%20you%20will%20observe%20that%20SQL%20Server%20once%20again%20holds%20locks%20on%20each%20row%20touched%20by%20this%20query.%26nbsp%3B%20In%20this%20example%2C%20SQL%20Server%20is%20overly%20conservative%20and%20retains%20the%20locks%20even%20though%20they%20are%20technically%20unnecessary.%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383286%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20May%2031%2C%202007%20In%20my%20last%20post%2C%20I%20explained%20that%20SQL%20Server%20holds%20read%20committed%20locks%20until%20the%20end%20of%20an%20update%20statement%20(instead%20of%20releasing%20the%20locks%20as%20soon%20as%20each%20row%20is%20released)%20if%20there%20is%20a%20blocking%20operator%20between%20the%20scan%20or%20seek%20of%20the%20rows%20to%20be%20updated%20and%20the%20update%20itself.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383286%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerQueryProcessing%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 05:02 AM
Updated by: