SlideShare a Scribd company logo
#tarabica15
#tarabica15
Boris Hristov
SQL Server Consultant and Trainer
Welcome to the nightmare of locking,
blocking and isolation levels!
#tarabica15
#tarabica15
@BorisHristov
So who am I?
#tarabica15
Agenda…
Locks. What is there for us?
Troubleshooting locking problems
Transaction Isolation Levels
#tarabica15
Locks. What is there for us?
#tarabica15
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…
#tarabica15
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
#tarabica15
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
Common Lock Types
#tarabica15
Lock Compatibility
Not all locks are compatible with other locks.
Lock Shared Update Exclusive
Shared (S)
  X
Update (U)
 X X
Exclusive (X)
X X X
#tarabica15
Lock Hierarchy
Database
Table
Page
Row
#tarabica15
Let’s update a row!
What do we need?
USE AdventureWorks2012
GO
UPDATE [Person].[Address]
SET AddressLine1=’Belgrade, Serbia'
WHERE AddressID=2
S
IX
Header
Row
Row
Row
Row
Row
IX
X
#tarabica15
Methods to View Locking Information
Dynamic
Management
Views
SQL Server
Profiler or
Extended
Events
Performance
Monitor or
Activity Monitor
#tarabica15
Troubleshooting locking problems
#tarabica15
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
#tarabica15
Lock escalation
S
S
X
>= 5000
IX
Header
Row
Row
Row
Row
Row
X
X
X
IX
X
#tarabica15
Controlling Lock Escalation
1. Switch the escalation level (per table)
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
SELECT lock_escalation_desc
FROM sys.tables
WHERE name = 'Person.Address'
ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE |
DISABLE}
#tarabica15
What Are Deadlocks?
Task A
Task B
Resource 1
Resource 2
Who is victim?
• Cost for Rollback
• Deadlock priority – SET DEADLOCK_PRIORITY
#tarabica15
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
Resolving blocking a.k.a live locking
#tarabica15
DEMO
Monitor for locks with xEvents
Lock escalation – both to table and partition
Deadlock and the SET DEADLOCK_PRIORITY option
#tarabica15
Transaction isolation levels
#tarabica15
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?)
Transaction 1
Transaction 2
Suggestion: Better offload the reads or go with optimistic level concurrency!
Select
Update
eXclusive lock
Read Uncommitted
(pessimistic concurrency control)
Dirty read
#tarabica15
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)
Update
Transaction 2
Repeatable Read
(pessimistic concurrency control)
#tarabica15
Transaction 1 S(hared) lock
select
Even phantom records are not possible!
Highest pessimistic level of isolation, lowest level of concurrency
Insert
Transaction 2
Serializable
(pessimistic concurrency control)
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
#tarabica15
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
Optimistic Concurrency
#tarabica15
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
RCSI and SI
(optimistic concurrency control)
V1 V2
Transaction 1
Transaction 2
Select in RCSISelect
Select in SI
#tarabica15
DEMO
Playing around with the Isolation levels
#tarabica15
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!
#tarabica15
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
#tarabica15
Thank you!
Boris Hristov
brshristov@live.com
www.borishristov.com

More Related Content

PPTX
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!
PPTX
Welcome to the nightmare of locking, blocking and isolation levels!
PPTX
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona
PPTX
Deep Into Isolation Levels
PDF
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!
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
Deep Into Isolation Levels
The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!

What's hot (20)

PDF
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
Welcome to the nightmare of locking, blocking and isolation levels!
PDF
Replay your workload as it is your actual one!
PDF
The Nightmare of Locking, Blocking and Isolation Levels!
PPTX
Replay your workload as it is your actual one!
PPT
SQL Server Transaction Management
PPT
11. transaction sql
PDF
How to cook Rabbit on Production - Serhiy Nazarov | Ruby Meditation 28
PPSX
LMAX Disruptor - High Performance Inter-Thread Messaging Library
PDF
[Altibase] 6 what is the mvcc
PPTX
Concurrency
PDF
Drizzle Talk
PDF
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
PPTX
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
PDF
Distributed Systems Theory for Mere Mortals - GeeCON Krakow May 2017
PPTX
Replay your workload as it is your actual one!
PPTX
PDF
Dynamo Amazon’s Highly Available Key-value Store
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!
Welcome to the nightmare of locking, blocking and isolation levels!
Replay your workload as it is your actual one!
The Nightmare of Locking, Blocking and Isolation Levels!
Replay your workload as it is your actual one!
SQL Server Transaction Management
11. transaction sql
How to cook Rabbit on Production - Serhiy Nazarov | Ruby Meditation 28
LMAX Disruptor - High Performance Inter-Thread Messaging Library
[Altibase] 6 what is the mvcc
Concurrency
Drizzle Talk
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Distributed Systems Theory for Mere Mortals - GeeCON Krakow May 2017
Replay your workload as it is your actual one!
Dynamo Amazon’s Highly Available Key-value Store
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
PPTX
The Nightmare of Locking, Blocking and Isolation Levels
PPTX
SQL Server Blocking Analysis
PPTX
Choosing A Concurrency Model, Optimistic Or Pessimistic
PPT
Troubleshooting Deadlocks in SQL Server 2000
PDF
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
PDF
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
PPTX
Database Transactions and SQL Server Concurrency
PDF
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
PDF
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
PPTX
Managing Memory & Locks - Series 2 Transactions & Lock management
PPT
Choosing a concurrency model, optimistic or pessimistic
PDF
Concurrency in SQL Server (SQL Night #24)
PDF
Database concurrency and transactions - Tal Olier
PPTX
Ibm db2 case study
PPTX
SqlSaturday199 - Deadlocks
PPT
Locking unit 1 topic 3
PPT
Locking And Concurrency
PPTX
Sql server concurrency
PPTX
Deadlocks: Lets Do One, Understand It, and Fix It
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
The Nightmare of Locking, Blocking and Isolation Levels
SQL Server Blocking Analysis
Choosing A Concurrency Model, Optimistic Or Pessimistic
Troubleshooting Deadlocks in SQL Server 2000
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
Database Transactions and SQL Server Concurrency
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Managing Memory & Locks - Series 2 Transactions & Lock management
Choosing a concurrency model, optimistic or pessimistic
Concurrency in SQL Server (SQL Night #24)
Database concurrency and transactions - Tal Olier
Ibm db2 case study
SqlSaturday199 - Deadlocks
Locking unit 1 topic 3
Locking And Concurrency
Sql server concurrency
Deadlocks: Lets Do One, Understand It, and Fix It
Ad

More from Boris Hristov (17)

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
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

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
Tartificialntelligence_presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
project resource management chapter-09.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Getting Started with Data Integration: FME Form 101
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
NewMind AI Weekly Chronicles - August'25-Week II
Zenith AI: Advanced Artificial Intelligence
Web App vs Mobile App What Should You Build First.pdf
Approach and Philosophy of On baking technology
Tartificialntelligence_presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
project resource management chapter-09.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
OMC Textile Division Presentation 2021.pptx
Unlocking AI with Model Context Protocol (MCP)
SOPHOS-XG Firewall Administrator PPT.pptx
Hindi spoken digit analysis for native and non-native speakers
Building Integrated photovoltaic BIPV_UPV.pdf
A comparative study of natural language inference in Swahili using monolingua...
Assigned Numbers - 2025 - Bluetooth® Document
Getting Started with Data Integration: FME Form 101
Univ-Connecticut-ChatGPT-Presentaion.pdf
Hybrid model detection and classification of lung cancer
DP Operators-handbook-extract for the Mautical Institute
From MVP to Full-Scale Product A Startup’s Software Journey.pdf

The Nightmare of Locking, Blocking and Isolation Levels!

  • 2. #tarabica15 Boris Hristov SQL Server Consultant and Trainer Welcome to the nightmare of locking, blocking and isolation levels!
  • 5. #tarabica15 Agenda… Locks. What is there for us? Troubleshooting locking problems Transaction Isolation Levels
  • 7. #tarabica15 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…
  • 8. #tarabica15 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
  • 9. #tarabica15 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 Common Lock Types
  • 10. #tarabica15 Lock Compatibility Not all locks are compatible with other locks. Lock Shared Update Exclusive Shared (S)   X Update (U)  X X Exclusive (X) X X X
  • 12. #tarabica15 Let’s update a row! What do we need? USE AdventureWorks2012 GO UPDATE [Person].[Address] SET AddressLine1=’Belgrade, Serbia' WHERE AddressID=2 S IX Header Row Row Row Row Row IX X
  • 13. #tarabica15 Methods to View Locking Information Dynamic Management Views SQL Server Profiler or Extended Events Performance Monitor or Activity Monitor
  • 15. #tarabica15 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
  • 17. #tarabica15 Controlling Lock Escalation 1. Switch the escalation level (per table) 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 SELECT lock_escalation_desc FROM sys.tables WHERE name = 'Person.Address' ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE | DISABLE}
  • 18. #tarabica15 What Are Deadlocks? Task A Task B Resource 1 Resource 2 Who is victim? • Cost for Rollback • Deadlock priority – SET DEADLOCK_PRIORITY
  • 19. #tarabica15 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 Resolving blocking a.k.a live locking
  • 20. #tarabica15 DEMO Monitor for locks with xEvents Lock escalation – both to table and partition Deadlock and the SET DEADLOCK_PRIORITY option
  • 22. #tarabica15 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?) Transaction 1 Transaction 2 Suggestion: Better offload the reads or go with optimistic level concurrency! Select Update eXclusive lock Read Uncommitted (pessimistic concurrency control) Dirty read
  • 23. #tarabica15 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) Update Transaction 2 Repeatable Read (pessimistic concurrency control)
  • 24. #tarabica15 Transaction 1 S(hared) lock select Even phantom records are not possible! Highest pessimistic level of isolation, lowest level of concurrency Insert Transaction 2 Serializable (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  • 25. #tarabica15 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 Optimistic Concurrency
  • 26. #tarabica15 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 RCSI and SI (optimistic concurrency control) V1 V2 Transaction 1 Transaction 2 Select in RCSISelect Select in SI
  • 27. #tarabica15 DEMO Playing around with the Isolation levels
  • 28. #tarabica15 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!
  • 29. #tarabica15 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