Welcome to the nightmare of 
locking, blocking and isolation levels! 
SQLSaturday 323 – Paris 2014
Sponsors Gold 
SQLSaturday 323 – Paris 2014
Sponsors Silver et Bronze 
SQLSaturday 323 – Paris 2014
So who am I? 
So who am I? 
@BorisHristov 
SQLSaturday 323 – Paris 2014
Agenda… 
Locks. What is there for us? 
Troubleshooting locking problems 
Transaction Isolation Levels 
SQLSaturday 323 – Paris 2014
Locks. What is there for us? 
SQLSaturday 323 – Paris 2014
Methods of Concurrency Control 
1. Pessimistic 
– SQL Server uses locks, causes blocks and who said deadlocks? 
2. Optimistic 
– SQL Server generates versions for everyone, but the updates… 
SQLSaturday 323 – Paris 2014
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 
SQLSaturday 323 – Paris 2014
Common lock types 
SQLSaturday 323 – Paris 2014 
Intent 
Used for: Preventing incompatible locks 
Duration: End of the transaction 
Shared (S) 
Used for: Reading 
Duration: Released almost immediately 
(depends on the isolation level) 
Update (U) 
Used for: Preparing to modify 
Duration: End of the transaction or until 
converted to exclusive (X) 
Exclusive (X) 
Used for: Modifying 
Duration: End of the transaction
Lock Compatibility 
Not all locks are compatible with other locks. 
Lock Shared Update Exclusive 
Shared 
  X 
(S) 
Update 
(U) 
 X X 
Exclusive 
(X) X X X 
SQLSaturday 323 – Paris 2014
Lock Hierarchy 
SQLSaturday 323 – Paris 2014 
Database 
Table 
Page 
Row
Let’s update a row! 
What do we need? 
USE AdventureWorks2012 
GO 
UPDATE [Person].[Address] 
SET AddressLine1=’Paris, France' 
WHERE AddressID=2 
SQLSaturday 323 – Paris 2014 
S 
IX 
Header 
Row 
Row 
Row 
Row 
Row 
IX 
X
Methods to View Locking Information 
Dynamic 
Management 
Views 
SQL Server 
Profiler or 
Extended Events 
SQLSaturday 323 – Paris 2014 
Performance 
monitor or 
Activity Monitor
Troubleshooting locking problems 
SQLSaturday 323 – Paris 2014
Locking and blocking 
Locking and blocking are often confused! 
Locking 
• The action of taking and potentially 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 multiuser environment, there is always, always blocking! 
• Only a problem if it lasts too long 
SQLSaturday 323 – Paris 2014
Lock escalation 
S 
IX 
X 
SQLSaturday 323 – Paris 2014 
S 
X 
>= 5000 
IX 
Header 
Row 
Row 
Row 
Row 
Row 
X 
X 
X
Controlling Lock escalation 
1. Switch the escalation level (per table) 
SELECT lock_escalation_desc 
FROM sys.tables 
WHERE name = 'Person.Address' 
ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE | DISABLE} 
AUTO – Partition-level escalation if the table is partitioned 
TABLE – Always table-level escalation 
DISABLE – Do not escalate until absolutely necessary 
2. Just disable it (that’s not Nike’s “Just do it!”) 
• Trace flag 1211 – disables lock escalation on server level 
• Trace flag 1224 – disables lock escalation if 40% of the memory used is consumed 
SQLSaturday 323 – Paris 2014
What Are Deadlocks? 
Task A 
Task B 
SQLSaturday 323 – Paris 2014 
Resource 1 
Resource 2 
Who is victim? 
• Cost for Rollback 
• Deadlock priority – SET DEADLOCK_PRIORITY
Resolve blocking a.k.a live locking 
1. Keep the transactions as short as possible 
2. No user interactions required in the middle of the transaction 
3. Use indexes (proper ones) 
4. Consider a server to offload some of the workloads 
5. Choose isolation level 
SQLSaturday 323 – Paris 2014
DEMO 
Monitor for locks with xEvents 
Lock escalation – both to table and partition 
Deadlock and the SET DEADLOCK_PRIORITY option 
SQLSaturday 323 – Paris 2014
Transaction isolation levels 
SQLSaturday 323 – Paris 2014
Read Uncommitted 
(pessimistic concurrency control) 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?) 
Transaction 1 
Select 
eXclusive lock 
Transaction 2 
Update 
Dirty read 
Suggestion: Better offload the reads or go with optimistic level concurrency! 
SQLSaturday 323 – Paris 2014
Repeatable Read 
(pessimistic concurrency control) 
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ 
Transaction 1 S(hared) lock 
select 
No non-repeatable reads possible (updates during Transaction 1) 
Phantom records still possible (inserts during Transaction 1) 
SQLSaturday 323 – Paris 2014 
Update 
Transaction 2
Transaction 1 S(hared) lock 
select 
Even phantom records are not possible! 
Highest pessimistic level of isolation, lowest level of concurrency 
SQLSaturday 323 – Paris 2014 
Insert 
Transaction 2 
Serializable 
(pessimistic concurrency control) 
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
Optimistic Concurrency 
Based on Row versioning (stored inside tempdb’s version store area) 
• No dirty, non-repeatable reads or phantom records 
• Every single modification 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 
SQLSaturday 323 – Paris 2014
RCSI and SI 
(optimistic concurrency control) 
Transaction 1 
V1 V2 
Select Select in RCSI 
Transaction 2 
RCSI – Read Committed Snapshot Isolation Level 
• Statement level versioning 
• Requires ALTER DATABASE SET READ_COMMITTED_SNAPSHOT ON 
Snapshot Isolation Level 
• Transaction level versioning 
• Requires ALTER DATABASE SET ALLOW_SNAPSHOT_ISOLATION ON 
• Requires SET TRANSACTION ISOLATION LEVEL SNAPSHOT 
SQLSaturday 323 – Paris 2014 
Select in SI
DEMO 
Playing around with the Isolation levels 
SQLSaturday 323 – Paris 2014
Summary 
1. Blocking is something normal when it’s not for long 
2. There are numerous of ways to monitor locking and blocking 
3. Be extremely careful for lock escalations 
4. Choosing the Isolation level is also a business decision! 
SQLSaturday 323 – Paris 2014
Resources 
MCM Readiness videos on locking lecture and demo 
MCM Readiness video on Snapshot Isolation Level 
http://blogs.msdn.com/b/bartd/archive/tags/sql+locking 
http://www.sqlskills.com/blogs/paul/category/locking/ 
Lock hints - 
http://www.techrepublic.com/article/control-sql-server-locking- 
with-hints/5181472 
SQLSaturday 323 – Paris 2014
Merci! 
SQLSaturday 323 – Paris 2014

The Nightmare of Locking, Blocking and Isolation Levels

  • 1.
    Welcome to thenightmare of locking, blocking and isolation levels! SQLSaturday 323 – Paris 2014
  • 2.
    Sponsors Gold SQLSaturday323 – Paris 2014
  • 3.
    Sponsors Silver etBronze SQLSaturday 323 – Paris 2014
  • 4.
    So who amI? So who am I? @BorisHristov SQLSaturday 323 – Paris 2014
  • 5.
    Agenda… Locks. Whatis there for us? Troubleshooting locking problems Transaction Isolation Levels SQLSaturday 323 – Paris 2014
  • 6.
    Locks. What isthere for us? SQLSaturday 323 – Paris 2014
  • 7.
    Methods of ConcurrencyControl 1. Pessimistic – SQL Server uses locks, causes blocks and who said deadlocks? 2. Optimistic – SQL Server generates versions for everyone, but the updates… SQLSaturday 323 – Paris 2014
  • 8.
    What Are Locksand 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 SQLSaturday 323 – Paris 2014
  • 9.
    Common lock types SQLSaturday 323 – Paris 2014 Intent Used for: Preventing incompatible locks Duration: End of the transaction Shared (S) Used for: Reading Duration: Released almost immediately (depends on the isolation level) Update (U) Used for: Preparing to modify Duration: End of the transaction or until converted to exclusive (X) Exclusive (X) Used for: Modifying Duration: End of the transaction
  • 10.
    Lock Compatibility Notall locks are compatible with other locks. Lock Shared Update Exclusive Shared   X (S) Update (U)  X X Exclusive (X) X X X SQLSaturday 323 – Paris 2014
  • 11.
    Lock Hierarchy SQLSaturday323 – Paris 2014 Database Table Page Row
  • 12.
    Let’s update arow! What do we need? USE AdventureWorks2012 GO UPDATE [Person].[Address] SET AddressLine1=’Paris, France' WHERE AddressID=2 SQLSaturday 323 – Paris 2014 S IX Header Row Row Row Row Row IX X
  • 13.
    Methods to ViewLocking Information Dynamic Management Views SQL Server Profiler or Extended Events SQLSaturday 323 – Paris 2014 Performance monitor or Activity Monitor
  • 14.
    Troubleshooting locking problems SQLSaturday 323 – Paris 2014
  • 15.
    Locking and blocking Locking and blocking are often confused! Locking • The action of taking and potentially 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 multiuser environment, there is always, always blocking! • Only a problem if it lasts too long SQLSaturday 323 – Paris 2014
  • 16.
    Lock escalation S IX X SQLSaturday 323 – Paris 2014 S X >= 5000 IX Header Row Row Row Row Row X X X
  • 17.
    Controlling Lock escalation 1. Switch the escalation level (per table) SELECT lock_escalation_desc FROM sys.tables WHERE name = 'Person.Address' ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE | DISABLE} AUTO – Partition-level escalation if the table is partitioned TABLE – Always table-level escalation DISABLE – Do not escalate until absolutely necessary 2. Just disable it (that’s not Nike’s “Just do it!”) • Trace flag 1211 – disables lock escalation on server level • Trace flag 1224 – disables lock escalation if 40% of the memory used is consumed SQLSaturday 323 – Paris 2014
  • 18.
    What Are Deadlocks? Task A Task B SQLSaturday 323 – Paris 2014 Resource 1 Resource 2 Who is victim? • Cost for Rollback • Deadlock priority – SET DEADLOCK_PRIORITY
  • 19.
    Resolve blocking a.k.alive locking 1. Keep the transactions as short as possible 2. No user interactions required in the middle of the transaction 3. Use indexes (proper ones) 4. Consider a server to offload some of the workloads 5. Choose isolation level SQLSaturday 323 – Paris 2014
  • 20.
    DEMO Monitor forlocks with xEvents Lock escalation – both to table and partition Deadlock and the SET DEADLOCK_PRIORITY option SQLSaturday 323 – Paris 2014
  • 21.
    Transaction isolation levels SQLSaturday 323 – Paris 2014
  • 22.
    Read Uncommitted (pessimisticconcurrency control) SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?) Transaction 1 Select eXclusive lock Transaction 2 Update Dirty read Suggestion: Better offload the reads or go with optimistic level concurrency! SQLSaturday 323 – Paris 2014
  • 23.
    Repeatable Read (pessimisticconcurrency control) SET TRANSACTION ISOLATION LEVEL REPEATABLE READ Transaction 1 S(hared) lock select No non-repeatable reads possible (updates during Transaction 1) Phantom records still possible (inserts during Transaction 1) SQLSaturday 323 – Paris 2014 Update Transaction 2
  • 24.
    Transaction 1 S(hared)lock select Even phantom records are not possible! Highest pessimistic level of isolation, lowest level of concurrency SQLSaturday 323 – Paris 2014 Insert Transaction 2 Serializable (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  • 25.
    Optimistic Concurrency Basedon Row versioning (stored inside tempdb’s version store area) • No dirty, non-repeatable reads or phantom records • Every single modification 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 SQLSaturday 323 – Paris 2014
  • 26.
    RCSI and SI (optimistic concurrency control) Transaction 1 V1 V2 Select Select in RCSI Transaction 2 RCSI – Read Committed Snapshot Isolation Level • Statement level versioning • Requires ALTER DATABASE SET READ_COMMITTED_SNAPSHOT ON Snapshot Isolation Level • Transaction level versioning • Requires ALTER DATABASE SET ALLOW_SNAPSHOT_ISOLATION ON • Requires SET TRANSACTION ISOLATION LEVEL SNAPSHOT SQLSaturday 323 – Paris 2014 Select in SI
  • 27.
    DEMO Playing aroundwith the Isolation levels SQLSaturday 323 – Paris 2014
  • 28.
    Summary 1. Blockingis something normal when it’s not for long 2. There are numerous of ways to monitor locking and blocking 3. Be extremely careful for lock escalations 4. Choosing the Isolation level is also a business decision! SQLSaturday 323 – Paris 2014
  • 29.
    Resources MCM Readinessvideos on locking lecture and demo MCM Readiness video on Snapshot Isolation Level http://blogs.msdn.com/b/bartd/archive/tags/sql+locking http://www.sqlskills.com/blogs/paul/category/locking/ Lock hints - http://www.techrepublic.com/article/control-sql-server-locking- with-hints/5181472 SQLSaturday 323 – Paris 2014
  • 30.
    Merci! SQLSaturday 323– Paris 2014