WELCOME  TO  THE  NIGHTMARE  OF  
LOCKING,  BLOCKING  
AND  ISOLATION  LEVELS!
Boris  Hristov
PASS  Virtual  Chapter  07/12/2013
So  Who  Am  I?
—  SQL  Server  DBA  at  HP
—  Trainer  and  Speaker
—  MCITP,  MCSE,  MCT
—  brshristov@live.com 

—  www.borishristov.com
       @BorisHristov
Agenda…
Locks.  What  is  there  for  us?
TroubleshooYng  locking  problems
TransacYon  IsolaYon  Levels
Locks.  What  is  there  for  us?
Methods  of  Concurrency  Control
Two  main  concurrency  control  types:
1.  PessimisYc
2.  OpYmisYc
      
  
                                                    –  SQL  Server  uses  locks,  causes  blocks  and  who  said  deadlocks?  
                                                  –  SQL  Server  generates  versions  for  everyone,  but  the  updates…
What  Are  Locks  and  what  is  locking?
Lock  –  internal  memory  structure  that  “tells”  us  what  we  all  do  with  the  resources  
inside  the  system
Locking  –  mechanism  to  protect  the  resources  and  guarantee  consistent  data
  
  
Common  lock  types
Intent  
  
Used	
  for:	
  	
  Preven&ng	
  incompa&ble	
  locks	
  
Dura0on:	
  End	
  of	
  the	
  transac&on	
  
Shared	
  (S)	
  
	
  
Used	
  for:	
  Reading	
  
Dura0on:	
  Released	
  almost	
  immediately	
  
(depends	
  on	
  the	
  isola&on	
  level)
Update	
  (U)	
  
	
  
Used	
  for:	
  Preparing	
  to	
  modify	
  
Dura0on:	
  End	
  of	
  the	
  transac&on	
  or	
  un&l	
  
converted	
  to	
  exclusive	
  (X)
Exclusive	
  (X)	
  
	
  
Used	
  for:	
  Modifying	
  
Dura0on:	
  End	
  of	
  the	
  transac&on
Lock  CompaYbility
	
   Not  all  locks  are  compaYble  with  other  locks.  

Lock
 Shared
 Update
 Exclusive
Shared  (S)
P	
   P	
   X	
  
Update  (U)

 P	
   X	
   X	
  
Exclusive  (X)
X	
   X	
   X	
  
Lock  Hierarchy
Database
Table
Page
Row
Let’s  update  a  row.  What  do  we  need?
USE  AdventureWorks2012
GO
UPDATE  [Person].[Address]
SET  AddressLine1='Microsoi,  Bulgaria'  
WHERE  AddressID=2

S
IX
Header	
  
Row	
  
Row	
  
Row	
  
Row	
  
Row	
  
IX
X
A  query!  J
Methods  to  View  Locking  InformaYon
Dynamic  
Management  
Views
SQL  Server  Profiler  
or  Extended  
Events
Performance  
monitor  or  AcYvity  
Monitor
TroubleshooYng  locking  problems
Locking  and  blocking
Locking  and  blocking  are  oien  confused!
  
Locking
• The  acYon  of  taking  and  potenYally  holding  locks
• Used  to  implement  concurrency  control  

Blocking  is  result  of  locking!
• One  process  needs  to  wait  for  another  process  to  release  locked  resources
• In  a  mulYuser  environment,  there  is  always,  always  blocking!
• Only  a  problem  if  it  lasts  too  long
SQL  Server  decides  (during  compilaYon)  the  granularity  of  locks  to  be  used:
•  Row
•  Page
Lock  granularity  can  be  controlled  via  LOCK  HINTS  (PAGLOCK,  TABLOCKX,  etc…)
EscalaYon  always  happens  this  way:
Row  -­‐>  table  lock  (or  parYYon  lock  if  possible)
Page  -­‐>  table  lock  (or  parYYon  lock  if  possible)
Lock  escalaYon  can  be  disabled:
•  Trace  flag  1211  –  disables  lock  escalaYon  on  server  level
•  Trace  flag  1224  –  disables  lock  escalaYon  on  server  level  unYl  40%  of  the  memory  used  is  
consumed
Lock  granularity  and  escalaYon
Lock  escalaYon
S
S
X
>=  5000
IX
X
X
X
X
IX
Switch  the  escalaYon  level  (per  table)





AUTO  –  ParYYon-­‐level  escalaYon  if  the  table  is  parYYoned
TABLE  –  Always  table-­‐level  escalaYon
DISABLE  –  Do  not  escalate  unYl  absolutely  necessary    
Controlling  Lock  escalaYon
SELECT  lock_escalaYon_desc
FROM  sys.tables
WHERE  name  =  'Person.Address'

ALTER  TABLE  Person.Address  SET  (LOCK_ESCALATION  =  {AUTO  |  TABLE  |  DISABLE}
What  Are  Deadlocks?
Task  A
Task  B
Resource  1
Resource  2
Who  is  vicYm?
•  Cost  for  Rollback
•  Deadlock  priority  –  SET  DEADLOCK_PRIOIRTY
Resolve  blocking  a.k.a  live  locking
How  to  resolve  blocking  problems:
1.  Keep  the  transacYons  as  short  as  possible
2.  No  user  interacYons  required  in  the  middle  of  the  
transacYon
3.  Use  indexes
4.  Consider  a  server  to  offload  some  of  the  workloads
5.  Choose  isolaYon  level
DEMO    
Monitor  for  locks  with  xEvents
Lock  escalaYon  –  both  to  table  and  parYYon
Deadlock  and  the  SET  DEADLOCK_PRIORITY  opYon
TransacYon  isolaYon  levels
SET	
  TRANSACTION	
  ISOLATION	
  LEVEL	
  READ	
  UNCOMMITTED	
  (NOLOCK?)	
  	
  
Transaction 1
Transaction 2
	
   Sugges0on:	
  BePer	
  offload	
  the	
  reads	
  or	
  go	
  with	
  op&mis&c	
  level	
  concurrency!	
  
Select
Update
eXclusive lock
Read  Uncommixed  
(pessimisYc  concurrency  control)
Dirty read
SET	
  TRANSACTION	
  ISOLATION	
  LEVEL	
  REPEATABLE	
  READ	
  
Transaction 1 S(hared) lock
select
	
   No	
  non-­‐repeatable	
  reads	
  possible	
  (updates	
  during	
  Transac&on	
  1)	
  
	
   Phantom	
  records	
  s&ll	
  possible	
  (inserts	
  during	
  Transac&on	
  1)	
  	
  
Update
Transaction 2
Repeatable  Read  
(pessimisYc  concurrency  control)
Transaction 1 S(hared) lock
select
	
   Even	
  phantom	
  records	
  are	
  not	
  possible!	
  
	
   Highest	
  pessimis&c	
  level	
  of	
  isola&on,	
  lowest	
  level	
  of	
  concurrency	
  
	
  
Insert
Transaction 2
Serializable  
(pessimisYc  concurrency  control)
SET	
  TRANSACTION	
  ISOLATION	
  LEVEL	
  SERIALIZABLE	
  
  Based	
  on	
  Row	
  versioning	
  (stored	
  inside	
  tempdb’s	
  version	
  store	
  area)	
  	
  
◦  No	
  dirty,	
  non-­‐repeatable	
  reads	
  or	
  phantom	
  records	
  
◦  Every	
  single	
  modifica&on	
  is	
  versioned	
  even	
  if	
  not	
  used	
  
◦  Adds	
  14	
  bytes	
  per	
  row	
  
Readers	
  do	
  not	
  block	
  writers	
  and	
  writers	
  do	
  not	
  block	
  readers	
  
Writers	
  can	
  and	
  will	
  block	
  writers,	
  this	
  can	
  cause	
  conflicts	
  
OpYmisYc  Concurrency
  RCSI	
  –	
  Read	
  CommiHed	
  Snapshot	
  Isola0on	
  Level	
  
◦  Statement	
  level	
  versioning	
  	
  
◦  Requires	
  ALTER	
  DATABASE	
  SET	
  READ_COMMITTED_SNAPSHOT	
  ON	
  
	
   Snapshot	
  Isola0on	
  Level	
  
◦  Transac&on	
  level	
  versioning	
  
◦  Requires	
  ALTER	
  DATABASE	
  SET	
  ALLOW_SNAPSHOT_ISOLATION	
  ON	
  
◦  Requires	
  SET	
  TRANSACTION	
  ISOLATION	
  LEVEL	
  SNAPSHOT	
  
RCSI  and  SI  
(opYmisYc  concurrency  control)
DEMO    
Playing  around  with  the  IsolaYon  levels
Summary
	
   Blocking	
  is	
  something	
  normal	
  when	
  it’s	
  not	
  for	
  long	
  
	
   There	
  are	
  hundreds	
  of	
  thousands	
  of	
  ways	
  to	
  monitor	
  locking	
  and	
  blocking	
  
	
   Be	
  extremely	
  careful	
  for	
  lock	
  escala&ons	
  
	
   Choosing	
  the	
  Isola&on	
  level	
  is	
  also	
  a	
  business	
  decision!	
  
	
  
Resources
MCM  Readiness  videos  on  locking  lecture  and  demo
MCM  Readiness  video  on  Snapshot  IsolaYon  Level
hxp://blogs.msdn.com/b/bartd/archive/tags/sql+locking/
hxp://www.sqlskills.com/blogs/paul/category/locking/
Lock  hints  -­‐  
hxp://www.techrepublic.com/arYcle/control-­‐sql-­‐server-­‐
locking-­‐with-­‐hints/5181472
Thank  you.
…  and  don’t  forget  for  the  Google  Hangouts!
?

The nightmare of locking, blocking and isolation levels

  • 1.
    WELCOME  TO  THE NIGHTMARE  OF   LOCKING,  BLOCKING   AND  ISOLATION  LEVELS! Boris  Hristov PASS  Virtual  Chapter  07/12/2013
  • 2.
    So  Who  Am I? —  SQL  Server  DBA  at  HP —  Trainer  and  Speaker —  MCITP,  MCSE,  MCT —  brshristov@live.com —  www.borishristov.com      @BorisHristov
  • 3.
    Agenda… Locks.  What  is there  for  us? TroubleshooYng  locking  problems TransacYon  IsolaYon  Levels
  • 4.
    Locks.  What  is there  for  us?
  • 5.
    Methods  of  Concurrency Control Two  main  concurrency  control  types: 1.  PessimisYc 2.  OpYmisYc                                                            –  SQL  Server  uses  locks,  causes  blocks  and  who  said  deadlocks?                                                    –  SQL  Server  generates  versions  for  everyone,  but  the  updates…
  • 6.
    What  Are  Locks and  what  is  locking? Lock  –  internal  memory  structure  that  “tells”  us  what  we  all  do  with  the  resources   inside  the  system Locking  –  mechanism  to  protect  the  resources  and  guarantee  consistent  data    
  • 7.
    Common  lock  types Intent    Used  for:    Preven&ng  incompa&ble  locks   Dura0on:  End  of  the  transac&on   Shared  (S)     Used  for:  Reading   Dura0on:  Released  almost  immediately   (depends  on  the  isola&on  level) Update  (U)     Used  for:  Preparing  to  modify   Dura0on:  End  of  the  transac&on  or  un&l   converted  to  exclusive  (X) Exclusive  (X)     Used  for:  Modifying   Dura0on:  End  of  the  transac&on
  • 8.
    Lock  CompaYbility   Not all  locks  are  compaYble  with  other  locks.   Lock Shared Update Exclusive Shared  (S) P   P   X   Update  (U) P   X   X   Exclusive  (X) X   X   X  
  • 9.
  • 10.
    Let’s  update  a row.  What  do  we  need? USE  AdventureWorks2012 GO UPDATE  [Person].[Address] SET  AddressLine1='Microsoi,  Bulgaria'   WHERE  AddressID=2 S IX Header   Row   Row   Row   Row   Row   IX X A  query!  J
  • 11.
    Methods  to  View Locking  InformaYon Dynamic   Management   Views SQL  Server  Profiler   or  Extended   Events Performance   monitor  or  AcYvity   Monitor
  • 12.
  • 13.
    Locking  and  blocking Locking and  blocking  are  oien  confused!   Locking • The  acYon  of  taking  and  potenYally  holding  locks • Used  to  implement  concurrency  control   Blocking  is  result  of  locking! • One  process  needs  to  wait  for  another  process  to  release  locked  resources • In  a  mulYuser  environment,  there  is  always,  always  blocking! • Only  a  problem  if  it  lasts  too  long
  • 14.
    SQL  Server  decides (during  compilaYon)  the  granularity  of  locks  to  be  used: •  Row •  Page Lock  granularity  can  be  controlled  via  LOCK  HINTS  (PAGLOCK,  TABLOCKX,  etc…) EscalaYon  always  happens  this  way: Row  -­‐>  table  lock  (or  parYYon  lock  if  possible) Page  -­‐>  table  lock  (or  parYYon  lock  if  possible) Lock  escalaYon  can  be  disabled: •  Trace  flag  1211  –  disables  lock  escalaYon  on  server  level •  Trace  flag  1224  –  disables  lock  escalaYon  on  server  level  unYl  40%  of  the  memory  used  is   consumed Lock  granularity  and  escalaYon
  • 15.
  • 16.
    Switch  the  escalaYon level  (per  table) AUTO  –  ParYYon-­‐level  escalaYon  if  the  table  is  parYYoned TABLE  –  Always  table-­‐level  escalaYon DISABLE  –  Do  not  escalate  unYl  absolutely  necessary     Controlling  Lock  escalaYon SELECT  lock_escalaYon_desc FROM  sys.tables WHERE  name  =  'Person.Address' ALTER  TABLE  Person.Address  SET  (LOCK_ESCALATION  =  {AUTO  |  TABLE  |  DISABLE}
  • 17.
    What  Are  Deadlocks? Task A Task  B Resource  1 Resource  2 Who  is  vicYm? •  Cost  for  Rollback •  Deadlock  priority  –  SET  DEADLOCK_PRIOIRTY
  • 18.
    Resolve  blocking  a.k.a live  locking How  to  resolve  blocking  problems: 1.  Keep  the  transacYons  as  short  as  possible 2.  No  user  interacYons  required  in  the  middle  of  the   transacYon 3.  Use  indexes 4.  Consider  a  server  to  offload  some  of  the  workloads 5.  Choose  isolaYon  level
  • 19.
    DEMO     Monitor for  locks  with  xEvents Lock  escalaYon  –  both  to  table  and  parYYon Deadlock  and  the  SET  DEADLOCK_PRIORITY  opYon
  • 20.
  • 21.
    SET  TRANSACTION  ISOLATION  LEVEL  READ  UNCOMMITTED  (NOLOCK?)     Transaction 1 Transaction 2   Sugges0on:  BePer  offload  the  reads  or  go  with  op&mis&c  level  concurrency!   Select Update eXclusive lock Read  Uncommixed   (pessimisYc  concurrency  control) Dirty read
  • 22.
    SET  TRANSACTION  ISOLATION  LEVEL  REPEATABLE  READ   Transaction 1 S(hared) lock select   No  non-­‐repeatable  reads  possible  (updates  during  Transac&on  1)     Phantom  records  s&ll  possible  (inserts  during  Transac&on  1)     Update Transaction 2 Repeatable  Read   (pessimisYc  concurrency  control)
  • 23.
    Transaction 1 S(hared)lock select   Even  phantom  records  are  not  possible!     Highest  pessimis&c  level  of  isola&on,  lowest  level  of  concurrency     Insert Transaction 2 Serializable   (pessimisYc  concurrency  control) SET  TRANSACTION  ISOLATION  LEVEL  SERIALIZABLE  
  • 24.
      Based  on  Row  versioning  (stored  inside  tempdb’s  version  store  area)     ◦  No  dirty,  non-­‐repeatable  reads  or  phantom  records   ◦  Every  single  modifica&on  is  versioned  even  if  not  used   ◦  Adds  14  bytes  per  row   Readers  do  not  block  writers  and  writers  do  not  block  readers   Writers  can  and  will  block  writers,  this  can  cause  conflicts   OpYmisYc  Concurrency
  • 25.
      RCSI  –  Read  CommiHed  Snapshot  Isola0on  Level   ◦  Statement  level  versioning     ◦  Requires  ALTER  DATABASE  SET  READ_COMMITTED_SNAPSHOT  ON     Snapshot  Isola0on  Level   ◦  Transac&on  level  versioning   ◦  Requires  ALTER  DATABASE  SET  ALLOW_SNAPSHOT_ISOLATION  ON   ◦  Requires  SET  TRANSACTION  ISOLATION  LEVEL  SNAPSHOT   RCSI  and  SI   (opYmisYc  concurrency  control)
  • 26.
    DEMO     Playing around  with  the  IsolaYon  levels
  • 27.
    Summary   Blocking  is  something  normal  when  it’s  not  for  long     There  are  hundreds  of  thousands  of  ways  to  monitor  locking  and  blocking     Be  extremely  careful  for  lock  escala&ons     Choosing  the  Isola&on  level  is  also  a  business  decision!    
  • 28.
    Resources MCM  Readiness  videos on  locking  lecture  and  demo MCM  Readiness  video  on  Snapshot  IsolaYon  Level hxp://blogs.msdn.com/b/bartd/archive/tags/sql+locking/ hxp://www.sqlskills.com/blogs/paul/category/locking/ Lock  hints  -­‐   hxp://www.techrepublic.com/arYcle/control-­‐sql-­‐server-­‐ locking-­‐with-­‐hints/5181472
  • 29.
    Thank  you. …  and don’t  forget  for  the  Google  Hangouts! ?