Read Committed Isolation Level

Published Mar 23 2019 04:57 AM 1,047 Views
Microsoft
First published on MSDN on Apr 25, 2007

SQL Server 2000 supports four different isolation levels: read uncommitted (or nolock), read committed, repeatable read, and serializable.  SQL Server 2005 adds two new isolation levels: read committed snapshot and snapshot.  These isolation levels determine what locks SQL Server takes when accessing data and, therefore, by extension they determine the level of concurrency and consistency that statements and transactions experience.  All of these isolation levels are described in Books Online .


In this post, I'm going to take a closer look at the default isolation level of read committed.  When SQL Server executes a statement at the read committed isolation level, it acquires short lived share locks on a row by row basis.  The duration of these share locks is just long enough to read and process each row; the server generally releases each lock before proceeding to the next row.  Thus, if you run a simple select statement under read committed and check for locks (e.g., with sys.dm_tran_locks), you will typically see at most a single row lock at a time.  The sole purpose of these locks is to ensure that the statement only reads and returns committed data.  The locks work because updates always acquire an exclusive lock which blocks any readers trying to acquire a share lock.


Now, let's suppose that we scan an entire table at read committed isolation level.  Since the scan locks only one row at a time, there is nothing to prevent a concurrent update from moving a row before or after our scan reaches it.  The following graphic illustrates this point:



Let's try an experiment to see this effect in action.  We'll need two server sessions for this experiment.  First, create a simple table with three rows:


create table t (a int primary key, b int)
insert t values (1, 1)
insert t values (2, 2)
insert t values (3, 3)


Next, in session 1 lock the second row:


begin tran
update t set b = 12 where a = 2


Now, in session 2 run a simple scan of the table:


select * from t


This scan will read the first row and then block waiting for session 1 to release the lock it holds on the second row.  While the scan is blocked, in session 1 we can swap the first and third rows and then commit the transaction and release the exclusive lock blocking session 2:


update t set a = 4 where a = 1
update t set a = 0 where a = 3
select * from t
commit tran


Here are the new contents of the table following these updates:


a           b
----------- -----------
0           3
2           2
4           1


Finally, here is the result of the scan from session 2:


a           b
----------- -----------
1           1
2           2
4           1


Notice that in this output the first row was scanned prior to the updates while the third row was scanned following the updates.  In fact, these two rows are really the same row from before and after the update.  Moreover, the original third row that had the value (3, 3) is not output at all.  (We could claim that changing the primary key effectively deleted one row and created a new row, but we could also achieve the same effect on a non-clustered index.)


Finally, try repeating this experiment, but add a unique column to the table:


create table t (a int primary key, b int, c int unique)
insert t values (1, 1, 1)
insert t values (2, 2, 2)
insert t values (3, 3, 3)


You'll get the same result, but you'll see "duplicates" in the unique column.


If the above results are not acceptable, you can either enable read committed snapshot for your database or you can run at a higher isolation level (albeit with somewhat lower concurrency).



CLARIFICATION 8/26/2008: The above example works as I originally described if it is executed in tempdb.  However, the SELECT statement in session 2 may not block as described if the example is executed in other databases due to an optimization where SQL Server avoids acquiring read committed locks when it knows that no data has changed on a page.  If you encounter this problem, either run this example in tempdb or change the UPDATE statement in session 1 so that it actually changes the value of column b.  For example, try "update t set b = 12 where a = 2".


UPDATE 2/17/2011:  I changed the example from "update t set b = 2 where a = 2" to "update t set b = 12 where a = 2" to avoid the issue described in the above clarification.

%3CLINGO-SUB%20id%3D%22lingo-sub-383258%22%20slang%3D%22en-US%22%3ERead%20Committed%20Isolation%20Level%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383258%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%20Apr%2025%2C%202007%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3ESQL%20Server%202000%20supports%20four%20different%20isolation%20levels%3A%20read%20uncommitted%20(or%20nolock)%2C%20read%20committed%2C%20repeatable%20read%2C%20and%20serializable.%26nbsp%3B%20SQL%20Server%202005%20adds%20two%20new%20isolation%20levels%3A%20read%20committed%20snapshot%20and%20snapshot.%26nbsp%3B%20These%20isolation%20levels%20determine%20what%20locks%20SQL%20Server%20takes%20when%20accessing%20data%20and%2C%20therefore%2C%20by%20extension%20they%20determine%20the%20level%20of%20concurrency%20and%20consistency%20that%20statements%20and%20transactions%20experience.%26nbsp%3B%20All%20of%20these%20isolation%20levels%20are%20described%20in%20%3CA%20href%3D%22http%3A%2F%2Fmsdn2.microsoft.com%2Fen-us%2Flibrary%2Fms173763.aspx%22%20title%3D%22SET%20TRANSACTION%20ISOLATION%20LEVEL%20(Transact-SQL)%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20Books%20Online%20%3C%2FA%3E%20.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EIn%20this%20post%2C%20I'm%20going%20to%20take%20a%20closer%20look%20at%20the%20default%20isolation%20level%20of%20read%20committed.%26nbsp%3B%20When%20SQL%20Server%20executes%20a%20statement%20at%20the%20read%20committed%20isolation%20level%2C%20it%20acquires%20short%20lived%20share%20locks%20on%20a%20row%20by%20row%20basis.%26nbsp%3B%20The%20duration%20of%20these%20share%20locks%20is%20just%20long%20enough%20to%20read%20and%20process%20each%20row%3B%20the%20server%20generally%20releases%20each%20lock%20before%20proceeding%20to%20the%20next%20row.%26nbsp%3B%20Thus%2C%20if%20you%20run%20a%20simple%20select%20statement%20under%20read%20committed%20and%20check%20for%20locks%20(e.g.%2C%20with%20sys.dm_tran_locks)%2C%20you%20will%20typically%20see%20at%20most%20a%20single%20row%20lock%20at%20a%20time.%26nbsp%3B%20The%20sole%20purpose%20of%20these%20locks%20is%20to%20ensure%20that%20the%20statement%20only%20reads%20and%20returns%20committed%20data.%26nbsp%3B%20The%20locks%20work%20because%20updates%20always%20acquire%20an%20exclusive%20lock%20which%20blocks%20any%20readers%20trying%20to%20acquire%20a%20share%20lock.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENow%2C%20let's%20suppose%20that%20we%20scan%20an%20entire%20table%20at%20read%20committed%20isolation%20level.%26nbsp%3B%20Since%20the%20scan%20locks%20only%20one%20row%20at%20a%20time%2C%20there%20is%20nothing%20to%20prevent%20a%20concurrent%20update%20from%20moving%20a%20row%20before%20or%20after%20our%20scan%20reaches%20it.%26nbsp%3B%20The%20following%20graphic%20illustrates%20this%20point%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F97250i1CAE43D082E5C5DF%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ELet's%20try%20an%20experiment%20to%20see%20this%20effect%20in%20action.%26nbsp%3B%20We'll%20need%20two%20server%20sessions%20for%20this%20experiment.%26nbsp%3B%20First%2C%20create%20a%20simple%20table%20with%20three%20rows%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ecreate%20table%20t%20(a%20int%20primary%20key%2C%20b%20int)%20%3CBR%20%2F%3E%20insert%20t%20values%20(1%2C%201)%20%3CBR%20%2F%3E%20insert%20t%20values%20(2%2C%202)%20%3CBR%20%2F%3E%20insert%20t%20values%20(3%2C%203)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENext%2C%20in%20session%201%20lock%20the%20second%20row%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ebegin%20tran%20%3CBR%20%2F%3E%20update%20t%20set%20b%20%3D%2012%20where%20a%20%3D%202%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENow%2C%20in%20session%202%20run%20a%20simple%20scan%20of%20the%20table%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eselect%20*%20from%20t%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThis%20scan%20will%20read%20the%20first%20row%20and%20then%20block%20waiting%20for%20session%201%20to%20release%20the%20lock%20it%20holds%20on%20the%20second%20row.%26nbsp%3B%20While%20the%20scan%20is%20blocked%2C%20in%20session%201%20we%20can%20swap%20the%20first%20and%20third%20rows%20and%20then%20commit%20the%20transaction%20and%20release%20the%20exclusive%20lock%20blocking%20session%202%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eupdate%20t%20set%20a%20%3D%204%20where%20a%20%3D%201%20%3CBR%20%2F%3E%20update%20t%20set%20a%20%3D%200%20where%20a%20%3D%203%20%3CBR%20%2F%3E%20select%20*%20from%20t%20%3CBR%20%2F%3E%20commit%20tran%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EHere%20are%20the%20new%20contents%20of%20the%20table%20following%20these%20updates%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ea%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20b%20%3CBR%20%2F%3E%20-----------%20-----------%20%3CBR%20%2F%3E%200%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%203%20%3CBR%20%2F%3E%202%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%202%20%3CBR%20%2F%3E%204%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EFinally%2C%20here%20is%20the%20result%20of%20the%20scan%20from%20session%202%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ea%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20b%20%3CBR%20%2F%3E%20-----------%20-----------%20%3CBR%20%2F%3E%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B1%20%3CBR%20%2F%3E%202%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%202%20%3CBR%20%2F%3E%204%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENotice%20that%20in%20this%20output%20the%20first%20row%20was%20scanned%20prior%20to%20the%20updates%20while%20the%20third%20row%20was%20scanned%20following%20the%20updates.%26nbsp%3B%20In%20fact%2C%20these%20two%20rows%20are%20really%20the%20same%20row%20from%20before%20and%20after%20the%20update.%26nbsp%3B%20Moreover%2C%20the%20original%20third%20row%20that%20had%20the%20value%20(3%2C%203)%20is%20not%20output%20at%20all.%26nbsp%3B%20(We%20could%20claim%20that%20changing%20the%20primary%20key%20effectively%20deleted%20one%20row%20and%20created%20a%20new%20row%2C%20but%20we%20could%20also%20achieve%20the%20same%20effect%20on%20a%20non-clustered%20index.)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EFinally%2C%20try%20repeating%20this%20experiment%2C%20but%20add%20a%20unique%20column%20to%20the%20table%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ecreate%20table%20t%20(a%20int%20primary%20key%2C%20b%20int%2C%20c%20int%20unique)%20%3CBR%20%2F%3E%20insert%20t%20values%20(1%2C%201%2C%201)%20%3CBR%20%2F%3E%20insert%20t%20values%20(2%2C%202%2C%202)%20%3CBR%20%2F%3E%20insert%20t%20values%20(3%2C%203%2C%203)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EYou'll%20get%20the%20same%20result%2C%20but%20you'll%20see%20%22duplicates%22%20in%20the%20unique%20column.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EIf%20the%20above%20results%20are%20not%20acceptable%2C%20you%20can%20either%20enable%20read%20committed%20snapshot%20for%20your%20database%20or%20you%20can%20run%20at%20a%20higher%20isolation%20level%20(albeit%20with%20somewhat%20lower%20concurrency).%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CEM%3E%20%3C%2FEM%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CEM%3E%20CLARIFICATION%208%2F26%2F2008%3A%20The%20above%20example%20works%20as%20I%20originally%20described%20if%20it%20is%20executed%20in%20tempdb.%26nbsp%3B%20However%2C%20the%20SELECT%20statement%20in%20session%202%20may%20not%20block%20as%20described%20if%20the%20example%20is%20executed%20in%20other%20databases%20due%20to%20an%20optimization%20where%20SQL%20Server%20avoids%20acquiring%20read%20committed%20locks%20when%20it%20knows%20that%20no%20data%20has%20changed%20on%20a%20page.%26nbsp%3B%20If%20you%20encounter%20this%20problem%2C%20either%20run%20this%20example%20in%20tempdb%20or%20change%20the%20UPDATE%20statement%20in%20session%201%20so%20that%20it%20actually%20changes%20the%20value%20of%20column%20b.%26nbsp%3B%20For%20example%2C%20try%20%22update%20t%20set%20b%20%3D%2012%20where%20a%20%3D%202%22.%20%3C%2FEM%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CEM%3E%20UPDATE%202%2F17%2F2011%3A%26nbsp%3B%20I%20changed%20the%20example%20from%20%22update%20t%20set%20b%20%3D%202%20where%20a%20%3D%202%22%20to%20%22update%20t%20set%20b%20%3D%2012%20where%20a%20%3D%202%22%20to%20avoid%20the%20issue%20described%20in%20the%20above%20clarification.%20%3C%2FEM%3E%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383258%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Apr%2025%2C%202007%20SQL%20Server%202000%20supports%20four%20different%20isolation%20levels%3A%20read%20uncommitted%20(or%20nolock)%2C%20read%20committed%2C%20repeatable%20read%2C%20and%20serializable.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383258%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 04:57 AM
Updated by: