Welcome to the nightmare of 
locking, blocking and isolation levels!
With thanks to our sponsors
So who am I? 
So who am I? 
@BorisHristov
Agenda… 
Locks. What is there for us? 
Troubleshooting locking problems 
Transaction Isolation Levels
Locks. What is there for us?
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…
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 
Dura-on: 
End 
of 
the 
transac-on 
Shared 
(S) 
Used 
for: 
Reading 
Dura-on: 
Released 
almost 
immediately 
(depends 
on 
the 
isola-on 
level) 
Update 
(U) 
Used 
for: 
Preparing 
to 
modify 
Dura-on: 
End 
of 
the 
transac-on 
or 
un-l 
converted 
to 
exclusive 
(X) 
Exclusive 
(X) 
Used 
for: 
Modifying 
Dura-on: 
End 
of 
the 
transac-on
Lock Compatibility 
Not all locks are compatible 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=’Dublin, Ireland' 
WHERE AddressID=2 
S 
IX 
IX 
Header 
Row 
Row 
Row 
Row 
Row 
X
Methods to View Locking Information 
Dynamic 
Management 
Views 
SQL 
Server 
Profiler 
or 
Extended 
Events 
Performance 
monitor 
or 
Ac-vity 
Monitor
Troubleshooting locking problems
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
Lock escalation 
S 
S 
X 
>= 5000 
IX 
Header 
Row 
Row 
Row 
Row 
Row 
IX 
X 
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
What Are Deadlocks? 
Task A 
Task B 
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
DEMO 
Monitor for locks with xEvents 
Lock escalation – both to table and partition 
Deadlock and the SET DEADLOCK_PRIORITY option
Transaction isolation levels
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!
Repeatable Read 
(pessimistic concurrency control) 
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ 
Transaction 1 S(hared) lock 
select 
Update 
Transaction 2 
No non-repeatable reads possible (updates during Transaction 1) 
Phantom records still possible (inserts during Transaction 1)
(pessimistic concurrency control) 
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE 
Transaction 1 S(hared) lock 
select 
Serializable 
Insert 
Transaction 2 
Even phantom records are not possible! 
Highest pessimistic level of isolation, lowest level of concurrency
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
RCSI and SI 
(optimistic concurrency control) 
Transaction 1 
V1 V2 
Select Select in RCSI 
Transaction 2 
Select in SI 
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
DEMO 
Playing around with the Isolation levels
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!
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
Feedback 
Your feedback is important to us 
& there will be a feedback prize draw @ 
http://www.sql.ie/feedback
10:10 - Next Session … 
§ Lady Windermere (Charlemont #1) 
§ Carmel Gunn & Bob Duffy – The Irish Economic Crisis Visualized with Power BI 
§ Dorian Gray (Charlemont #2) 
§ Mladen Prajdic – Digging into the .NET SQLClient 
§ Intentions (Charlemont #3) 
§ Chris Webb – Power Query: Beyond the Basics 
§ Ideal Husband (Charlemont #4) 
§ Ewan Fairweather – Shared technical learning for building highly scalable & 
available cloud apps 
§ Lord Arthur Saville (Charlemont #5) 
§ William Durkin – Replication Troubleshooting & Monitoring
Thank you!

More Related Content

PPTX
The Nightmare of Locking, Blocking and Isolation Levels!
PPTX
Welcome to the nightmare of locking, blocking and isolation levels!
PPTX
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona
PDF
The nightmare of locking, blocking and isolation levels
PPTX
The Nightmare of Locking, Blocking and Isolation Levels!
PDF
The nightmare of locking, blocking and isolation levels!
PPTX
The Nightmare of Locking, Blocking and Isolation Levels!
PPTX
The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!
Welcome to the nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona
The nightmare of locking, blocking and isolation levels
The Nightmare of Locking, Blocking and Isolation Levels!
The nightmare of locking, blocking and isolation levels!
The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!

What's hot (16)

PPTX
The nightmare of locking, blocking and isolation levels!
PDF
The nightmare of locking, blocking and isolation levels
PPTX
Welcome to the nightmare of locking, blocking and isolation levels!
PPTX
Deep Into Isolation Levels
PDF
Replay your workload as it is your actual one!
PPTX
Welcome to the nightmare of locking, blocking and isolation levels!
PPTX
Database Transactions and SQL Server Concurrency
PPT
SQL Server Transaction Management
PDF
Distributed Systems Theory for Mere Mortals - GeeCON Krakow May 2017
PDF
Distributed Systems Theory for Mere Mortals - Java Day Istanbul May 2017
PDF
Concurrency in SQL Server (SQL Night #24)
PPTX
Concurrency
PDF
Top Mistakes When Writing Reactive Applications - Scala by the Bay 2016
PDF
Distributed Systems Theory for Mere Mortals
PPT
11. transaction sql
PDF
SSL, X.509, HTTPS - How to configure your HTTPS server
The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels
Welcome to the nightmare of locking, blocking and isolation levels!
Deep Into Isolation Levels
Replay your workload as it is your actual one!
Welcome to the nightmare of locking, blocking and isolation levels!
Database Transactions and SQL Server Concurrency
SQL Server Transaction Management
Distributed Systems Theory for Mere Mortals - GeeCON Krakow May 2017
Distributed Systems Theory for Mere Mortals - Java Day Istanbul May 2017
Concurrency in SQL Server (SQL Night #24)
Concurrency
Top Mistakes When Writing Reactive Applications - Scala by the Bay 2016
Distributed Systems Theory for Mere Mortals
11. transaction sql
SSL, X.509, HTTPS - How to configure your HTTPS server
Ad

Similar to The Nightmare of Locking, Blocking and Isolation Levels! (20)

PPTX
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
PDF
Database concurrency and transactions - Tal Olier
PDF
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
PDF
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
PPTX
SQL Server Blocking Analysis
PDF
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
PDF
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
PDF
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
PPTX
Ibm db2 case study
PPTX
The Nightmare of Locking, Blocking and Isolation Levels
PPTX
Choosing A Concurrency Model, Optimistic Or Pessimistic
PPT
Locking And Concurrency
PPT
Locking unit 1 topic 3
PPT
Troubleshooting Deadlocks in SQL Server 2000
PPT
Choosing a concurrency model, optimistic or pessimistic
PPTX
Sql server concurrency
PPT
SQL Server Transaction Management
PPT
DB2UDB_the_Basics Day 4
PPTX
Managing Memory & Locks - Series 2 Transactions & Lock management
PPT
Intro to tsql unit 12
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
Database concurrency and transactions - Tal Olier
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
SQL Server Blocking Analysis
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Ibm db2 case study
The Nightmare of Locking, Blocking and Isolation Levels
Choosing A Concurrency Model, Optimistic Or Pessimistic
Locking And Concurrency
Locking unit 1 topic 3
Troubleshooting Deadlocks in SQL Server 2000
Choosing a concurrency model, optimistic or pessimistic
Sql server concurrency
SQL Server Transaction Management
DB2UDB_the_Basics Day 4
Managing Memory & Locks - Series 2 Transactions & Lock management
Intro to tsql unit 12
Ad

More from Boris Hristov (18)

PDF
The Secret to Engaging Presentations
PDF
Presentation Design Fundamentals
PPTX
The World of Business Intelligence
PPTX
The 5 Hidden Performance Gems of SQL Server 2014
PPTX
Securing SQL Azure DB? How?
PDF
How to Deliver Technical Presentations: The Right Way!
PPTX
Securing SQL Azure DB? How?
PPTX
Top 5 T-SQL Improvements in SQL Server 2014
PPTX
Presentation Skills: The Next Level
PPTX
SQL Server 2014: Ready. Steady. Go!
PPTX
BI PoC for the Telco Industry
PPTX
Presentation Design Basics
PPTX
Top 5 T-SQL Improvements in SQL Server 2014
PPTX
Database Performance
PPTX
You want rules? You need Policy-Based Management!
PPTX
First Steps with Microsoft SQL Server
PDF
Top 5 TSQL Improvements in SQL Server 2014
PPTX
Replay your workload as it is your actual one!
The Secret to Engaging Presentations
Presentation Design Fundamentals
The World of Business Intelligence
The 5 Hidden Performance Gems of SQL Server 2014
Securing SQL Azure DB? How?
How to Deliver Technical Presentations: The Right Way!
Securing SQL Azure DB? How?
Top 5 T-SQL Improvements in SQL Server 2014
Presentation Skills: The Next Level
SQL Server 2014: Ready. Steady. Go!
BI PoC for the Telco Industry
Presentation Design Basics
Top 5 T-SQL Improvements in SQL Server 2014
Database Performance
You want rules? You need Policy-Based Management!
First Steps with Microsoft SQL Server
Top 5 TSQL Improvements in SQL Server 2014
Replay your workload as it is your actual one!

Recently uploaded (20)

PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPTX
Modernising the Digital Integration Hub
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
Architecture types and enterprise applications.pdf
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PPT
What is a Computer? Input Devices /output devices
PDF
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
PDF
Getting started with AI Agents and Multi-Agent Systems
PPTX
Chapter 5: Probability Theory and Statistics
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Comparative analysis of machine learning models for fake news detection in so...
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PPTX
Configure Apache Mutual Authentication
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PPT
Module 1.ppt Iot fundamentals and Architecture
Credit Without Borders: AI and Financial Inclusion in Bangladesh
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
A review of recent deep learning applications in wood surface defect identifi...
Modernising the Digital Integration Hub
Enhancing plagiarism detection using data pre-processing and machine learning...
1 - Historical Antecedents, Social Consideration.pdf
Final SEM Unit 1 for mit wpu at pune .pptx
Architecture types and enterprise applications.pdf
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
What is a Computer? Input Devices /output devices
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
Getting started with AI Agents and Multi-Agent Systems
Chapter 5: Probability Theory and Statistics
Zenith AI: Advanced Artificial Intelligence
Taming the Chaos: How to Turn Unstructured Data into Decisions
Comparative analysis of machine learning models for fake news detection in so...
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Configure Apache Mutual Authentication
Custom Battery Pack Design Considerations for Performance and Safety
Module 1.ppt Iot fundamentals and Architecture

The Nightmare of Locking, Blocking and Isolation Levels!

  • 1. Welcome to the nightmare of locking, blocking and isolation levels!
  • 2. With thanks to our sponsors
  • 3. So who am I? So who am I? @BorisHristov
  • 4. Agenda… Locks. What is there for us? Troubleshooting locking problems Transaction Isolation Levels
  • 5. Locks. What is there for us?
  • 6. 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…
  • 7. 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
  • 8. Common lock types Intent Used for: Preven-ng incompa-ble locks Dura-on: End of the transac-on Shared (S) Used for: Reading Dura-on: Released almost immediately (depends on the isola-on level) Update (U) Used for: Preparing to modify Dura-on: End of the transac-on or un-l converted to exclusive (X) Exclusive (X) Used for: Modifying Dura-on: End of the transac-on
  • 9. Lock Compatibility Not all locks are compatible with other locks. Lock Shared Update Exclusive Shared (S) P P X Update (U) P X X Exclusive (X) X X X
  • 10. Lock Hierarchy Database Table Page Row
  • 11. Let’s update a row! What do we need? USE AdventureWorks2012 GO UPDATE [Person].[Address] SET AddressLine1=’Dublin, Ireland' WHERE AddressID=2 S IX IX Header Row Row Row Row Row X
  • 12. Methods to View Locking Information Dynamic Management Views SQL Server Profiler or Extended Events Performance monitor or Ac-vity Monitor
  • 14. 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
  • 15. Lock escalation S S X >= 5000 IX Header Row Row Row Row Row IX X X X X
  • 16. 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
  • 17. What Are Deadlocks? Task A Task B Resource 1 Resource 2 Who is victim? • Cost for Rollback • Deadlock priority – SET DEADLOCK_PRIORITY
  • 18. 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
  • 19. DEMO Monitor for locks with xEvents Lock escalation – both to table and partition Deadlock and the SET DEADLOCK_PRIORITY option
  • 21. 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!
  • 22. Repeatable Read (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL REPEATABLE READ Transaction 1 S(hared) lock select Update Transaction 2 No non-repeatable reads possible (updates during Transaction 1) Phantom records still possible (inserts during Transaction 1)
  • 23. (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL SERIALIZABLE Transaction 1 S(hared) lock select Serializable Insert Transaction 2 Even phantom records are not possible! Highest pessimistic level of isolation, lowest level of concurrency
  • 24. 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
  • 25. RCSI and SI (optimistic concurrency control) Transaction 1 V1 V2 Select Select in RCSI Transaction 2 Select in SI 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
  • 26. DEMO Playing around with the Isolation levels
  • 27. 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!
  • 28. 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
  • 29. Feedback Your feedback is important to us & there will be a feedback prize draw @ http://www.sql.ie/feedback
  • 30. 10:10 - Next Session … § Lady Windermere (Charlemont #1) § Carmel Gunn & Bob Duffy – The Irish Economic Crisis Visualized with Power BI § Dorian Gray (Charlemont #2) § Mladen Prajdic – Digging into the .NET SQLClient § Intentions (Charlemont #3) § Chris Webb – Power Query: Beyond the Basics § Ideal Husband (Charlemont #4) § Ewan Fairweather – Shared technical learning for building highly scalable & available cloud apps § Lord Arthur Saville (Charlemont #5) § William Durkin – Replication Troubleshooting & Monitoring