SlideShare a Scribd company logo
1 of 29
13.12.2014 
Welcome to the nightmare of 
locking, blocking and isolation levels! 
Boris Hristov
Thank you to all our SPONORS! 
13.12.2014
So who am I? 
13.12.2014 
So who am I? 
@BorisHristov
Agenda… 
Locks. What is there for us? 
Troubleshooting locking problems 
Transaction Isolation Levels 
13.12.2014
Locks. What is there for us? 
13.12.2014
13.12.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…
13.12.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
13.12.2014 
Common lock types 
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
13.12.2014 
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
13.12.2014 
Lock Hierarchy 
Database 
Table 
Page 
Row
Let’s update a row! 
What do we need? 
USE AdventureWorks2012 
GO 
UPDATE [Person].[Address] 
SET AddressLine1=’Ljubljana, Slovenia' 
WHERE AddressID=2 
13.12.2014 
S 
IX 
Header 
Row 
Row 
Row 
Row 
Row 
IX 
X
13.12.2014 
Methods to View Locking Information 
Dynamic 
Management 
Views 
SQL Server 
Profiler or 
Extended Events 
Performance 
monitor or 
Activity Monitor
Troubleshooting locking problems 
13.12.2014
13.12.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
13.12.2014 
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!”) 
13.12.2014 
• Trace flag 1211 – disables lock escalation on server level 
• Trace flag 1224 – disables lock escalation if 40% of the memory used is consumed
13.12.2014 
What Are Deadlocks? 
Task A 
Task B 
Resource 1 
Resource 2 
Who is victim? 
• Cost for Rollback 
• Deadlock priority – SET DEADLOCK_PRIORITY
13.12.2014 
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
13.12.2014 
DEMO 
Monitor for locks with xEvents 
Lock escalation – both to table and partition 
Deadlock and the SET DEADLOCK_PRIORITY option
13.12.2014 
Transaction isolation levels
Read Uncommitted 
(pessimistic concurrency control) 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?) 
Transaction 1 
13.12.2014 
Select 
eXclusive lock 
Transaction 2 
Update 
Dirty read 
Suggestion: Better offload the reads or go with optimistic level concurrency!
13.12.2014 
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 
13.12.2014 
select 
Serializable 
Insert 
Transaction 2 
Even phantom records are not possible! 
Highest pessimistic level of isolation, lowest level of 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 
13.12.2014 
Optimistic Concurrency
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 
13.12.2014 
• 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
13.12.2014 
DEMO 
Playing around with the Isolation levels
13.12.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!
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- 
13.12.2014 
with-hints/5181472
Hvala! 
Contacts: 
brshristov@live.com 
@BorisHristov 
www.borishristov.com 
13.12.2014

More Related Content

What's hot

Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)
Antonios Chatzipavlis
 

What's hot (20)

Welcome to 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!
Welcome to 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!Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!
 
The nightmare of locking, blocking and isolation levels
The nightmare of locking, blocking and isolation levelsThe nightmare of locking, blocking and isolation levels
The nightmare of locking, blocking and isolation levels
 
Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Replay your workload as it is your actual one!
Replay your workload as it is your actual one!
 
Deep Into Isolation Levels
Deep Into Isolation LevelsDeep Into Isolation Levels
Deep Into 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!
 
Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
 
The Nightmare of Locking, Blocking and Isolation Levels
The Nightmare of Locking, Blocking and Isolation LevelsThe Nightmare of Locking, Blocking and Isolation Levels
The Nightmare of Locking, Blocking and Isolation Levels
 
How to cook Rabbit on Production - Serhiy Nazarov | Ruby Meditation 28
How to cook Rabbit on Production - Serhiy Nazarov | Ruby Meditation 28How to cook Rabbit on Production - Serhiy Nazarov | Ruby Meditation 28
How to cook Rabbit on Production - Serhiy Nazarov | Ruby Meditation 28
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction Management
 
2 years into drinking the Microservice kool-aid (Fact and Fiction)
2 years into drinking the Microservice kool-aid (Fact and Fiction)2 years into drinking the Microservice kool-aid (Fact and Fiction)
2 years into drinking the Microservice kool-aid (Fact and Fiction)
 
Dynamo Amazon’s Highly Available Key-value Store
Dynamo Amazon’s Highly Available Key-value StoreDynamo Amazon’s Highly Available Key-value Store
Dynamo Amazon’s Highly Available Key-value Store
 
MariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale:  an Intelligent Database ProxyMariaDB MaxScale:  an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database Proxy
 
LMAX Disruptor - High Performance Inter-Thread Messaging Library
LMAX Disruptor - High Performance Inter-Thread Messaging LibraryLMAX Disruptor - High Performance Inter-Thread Messaging Library
LMAX Disruptor - High Performance Inter-Thread Messaging Library
 
Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)
 
TransactionScope
TransactionScopeTransactionScope
TransactionScope
 
Redis as a Main Database, Scaling and HA
Redis as a Main Database, Scaling and HARedis as a Main Database, Scaling and HA
Redis as a Main Database, Scaling and HA
 
strangeloop 2012 apache cassandra anti patterns
strangeloop 2012 apache cassandra anti patternsstrangeloop 2012 apache cassandra anti patterns
strangeloop 2012 apache cassandra anti patterns
 
MariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database ProxyMariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database Proxy
 
Distributed Systems Theory for Mere Mortals - GeeCON Krakow May 2017
Distributed Systems Theory for Mere Mortals -  GeeCON Krakow May 2017Distributed Systems Theory for Mere Mortals -  GeeCON Krakow May 2017
Distributed Systems Theory for Mere Mortals - GeeCON Krakow May 2017
 

Viewers also liked

The World of Business Intelligence
The World of Business IntelligenceThe World of Business Intelligence
The World of Business Intelligence
Boris Hristov
 

Viewers also liked (8)

Auto start
Auto startAuto start
Auto start
 
Securing SQL Azure DB? How?
Securing SQL Azure DB? How?Securing SQL Azure DB? How?
Securing SQL Azure DB? How?
 
Layout and mock ups of my magazine.
Layout and mock ups of my magazine.Layout and mock ups of my magazine.
Layout and mock ups of my magazine.
 
Database Performance
Database PerformanceDatabase Performance
Database Performance
 
The 5 Hidden Performance Gems of SQL Server 2014
The 5 Hidden Performance Gems of SQL Server 2014The 5 Hidden Performance Gems of SQL Server 2014
The 5 Hidden Performance Gems of SQL Server 2014
 
The World of Business Intelligence
The World of Business IntelligenceThe World of Business Intelligence
The World of Business Intelligence
 
Top 10 tips for Oracle performance
Top 10 tips for Oracle performanceTop 10 tips for Oracle performance
Top 10 tips for Oracle performance
 
Database performance tuning and query optimization
Database performance tuning and query optimizationDatabase performance tuning and query optimization
Database performance tuning and query optimization
 

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

Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Olier
sqlserver.co.il
 
Troubleshooting Deadlocks in SQL Server 2000
Troubleshooting Deadlocks in SQL Server 2000Troubleshooting Deadlocks in SQL Server 2000
Troubleshooting Deadlocks in SQL Server 2000
elliando dias
 
How To Deploy Globally
How To Deploy GloballyHow To Deploy Globally
How To Deploy Globally
Aras
 
High Availbilty In Sql Server
High Availbilty In Sql ServerHigh Availbilty In Sql Server
High Availbilty In Sql Server
Rishikesh Tiwari
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12
Syed Asrarali
 

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

Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Olier
 
Troubleshooting Deadlocks in SQL Server 2000
Troubleshooting Deadlocks in SQL Server 2000Troubleshooting Deadlocks in SQL Server 2000
Troubleshooting Deadlocks in SQL Server 2000
 
Ibm db2 case study
Ibm db2 case studyIbm db2 case study
Ibm db2 case study
 
MNPHP Scalable Architecture 101 - Feb 3 2011
MNPHP Scalable Architecture 101 - Feb 3 2011MNPHP Scalable Architecture 101 - Feb 3 2011
MNPHP Scalable Architecture 101 - Feb 3 2011
 
Managing Memory & Locks - Series 2 Transactions & Lock management
Managing  Memory & Locks - Series 2 Transactions & Lock managementManaging  Memory & Locks - Series 2 Transactions & Lock management
Managing Memory & Locks - Series 2 Transactions & Lock management
 
How To Deploy Globally
How To Deploy GloballyHow To Deploy Globally
How To Deploy Globally
 
OVH Lab - Enterprise Cloud Databases
OVH Lab - Enterprise Cloud DatabasesOVH Lab - Enterprise Cloud Databases
OVH Lab - Enterprise Cloud Databases
 
High Availbilty In Sql Server
High Availbilty In Sql ServerHigh Availbilty In Sql Server
High Availbilty In Sql Server
 
Perforce Server: The Next Generation
Perforce Server: The Next GenerationPerforce Server: The Next Generation
Perforce Server: The Next Generation
 
MySQL 5.7 clustering: The developer perspective
MySQL 5.7 clustering: The developer perspectiveMySQL 5.7 clustering: The developer perspective
MySQL 5.7 clustering: The developer perspective
 
Introduction to High Availability with SQL Server
Introduction to High Availability with SQL ServerIntroduction to High Availability with SQL Server
Introduction to High Availability with SQL Server
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
 
My sql fabric webinar v1.1
My sql fabric webinar v1.1My sql fabric webinar v1.1
My sql fabric webinar v1.1
 
Scaling Your Database In The Cloud
Scaling Your Database In The CloudScaling Your Database In The Cloud
Scaling Your Database In The Cloud
 
Persistence Is Futile - Implementing Delayed Durability
Persistence Is Futile - Implementing Delayed DurabilityPersistence Is Futile - Implementing Delayed Durability
Persistence Is Futile - Implementing Delayed Durability
 
Oracle Active Data Guard 12c: Far Sync Instance, Real-Time Cascade and Other ...
Oracle Active Data Guard 12c: Far Sync Instance, Real-Time Cascade and Other ...Oracle Active Data Guard 12c: Far Sync Instance, Real-Time Cascade and Other ...
Oracle Active Data Guard 12c: Far Sync Instance, Real-Time Cascade and Other ...
 
Oracle Failover Database Cluster with Grid Infrastructure 12c
Oracle Failover Database Cluster with Grid Infrastructure 12cOracle Failover Database Cluster with Grid Infrastructure 12c
Oracle Failover Database Cluster with Grid Infrastructure 12c
 
026 Neo4j Data Loading (ETL_ELT) Best Practices - NODES2022 AMERICAS Advanced...
026 Neo4j Data Loading (ETL_ELT) Best Practices - NODES2022 AMERICAS Advanced...026 Neo4j Data Loading (ETL_ELT) Best Practices - NODES2022 AMERICAS Advanced...
026 Neo4j Data Loading (ETL_ELT) Best Practices - NODES2022 AMERICAS Advanced...
 
The architecture of oak
The architecture of oakThe architecture of oak
The architecture of oak
 

More from Boris Hristov

First Steps with Microsoft SQL Server
First Steps with Microsoft SQL ServerFirst Steps with Microsoft SQL Server
First Steps with Microsoft SQL Server
Boris Hristov
 

More from Boris Hristov (14)

The Secret to Engaging Presentations
The Secret to Engaging PresentationsThe Secret to Engaging Presentations
The Secret to Engaging Presentations
 
Presentation Design Fundamentals
Presentation Design FundamentalsPresentation Design Fundamentals
Presentation Design Fundamentals
 
How to Deliver Technical Presentations: The Right Way!
How to Deliver Technical Presentations: The Right Way!How to Deliver Technical Presentations: The Right Way!
How to Deliver Technical Presentations: The Right Way!
 
Securing SQL Azure DB? How?
Securing SQL Azure DB? How?Securing SQL Azure DB? How?
Securing SQL Azure DB? How?
 
Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014
 
Presentation Skills: The Next Level
Presentation Skills: The Next LevelPresentation Skills: The Next Level
Presentation Skills: The Next Level
 
SQL Server 2014: Ready. Steady. Go!
SQL Server 2014: Ready. Steady. Go!SQL Server 2014: Ready. Steady. Go!
SQL Server 2014: Ready. Steady. Go!
 
BI PoC for the Telco Industry
BI PoC for the Telco IndustryBI PoC for the Telco Industry
BI PoC for the Telco Industry
 
Presentation Design Basics
Presentation Design BasicsPresentation Design Basics
Presentation Design Basics
 
Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014
 
You want rules? You need Policy-Based Management!
You want rules? You need Policy-Based Management!You want rules? You need Policy-Based Management!
You want rules? You need Policy-Based Management!
 
First Steps with Microsoft SQL Server
First Steps with Microsoft SQL ServerFirst Steps with Microsoft SQL Server
First Steps with Microsoft SQL Server
 
Top 5 TSQL Improvements in SQL Server 2014
Top 5 TSQL Improvements in SQL Server 2014Top 5 TSQL Improvements in SQL Server 2014
Top 5 TSQL Improvements in SQL Server 2014
 
Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Replay your workload as it is your actual one!
Replay your workload as it is your actual one!
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

The Nightmare of Locking, Blocking and Isolation Levels!

  • 1. 13.12.2014 Welcome to the nightmare of locking, blocking and isolation levels! Boris Hristov
  • 2. Thank you to all our SPONORS! 13.12.2014
  • 3. So who am I? 13.12.2014 So who am I? @BorisHristov
  • 4. Agenda… Locks. What is there for us? Troubleshooting locking problems Transaction Isolation Levels 13.12.2014
  • 5. Locks. What is there for us? 13.12.2014
  • 6. 13.12.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…
  • 7. 13.12.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
  • 8. 13.12.2014 Common lock types 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
  • 9. 13.12.2014 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
  • 10. 13.12.2014 Lock Hierarchy Database Table Page Row
  • 11. Let’s update a row! What do we need? USE AdventureWorks2012 GO UPDATE [Person].[Address] SET AddressLine1=’Ljubljana, Slovenia' WHERE AddressID=2 13.12.2014 S IX Header Row Row Row Row Row IX X
  • 12. 13.12.2014 Methods to View Locking Information Dynamic Management Views SQL Server Profiler or Extended Events Performance monitor or Activity Monitor
  • 14. 13.12.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
  • 15. 13.12.2014 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!”) 13.12.2014 • Trace flag 1211 – disables lock escalation on server level • Trace flag 1224 – disables lock escalation if 40% of the memory used is consumed
  • 17. 13.12.2014 What Are Deadlocks? Task A Task B Resource 1 Resource 2 Who is victim? • Cost for Rollback • Deadlock priority – SET DEADLOCK_PRIORITY
  • 18. 13.12.2014 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. 13.12.2014 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 13.12.2014 Select eXclusive lock Transaction 2 Update Dirty read Suggestion: Better offload the reads or go with optimistic level concurrency!
  • 22. 13.12.2014 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 13.12.2014 select Serializable Insert Transaction 2 Even phantom records are not possible! Highest pessimistic level of isolation, lowest level of concurrency
  • 24. 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 13.12.2014 Optimistic Concurrency
  • 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 13.12.2014 • 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. 13.12.2014 DEMO Playing around with the Isolation levels
  • 27. 13.12.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!
  • 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- 13.12.2014 with-hints/5181472
  • 29. Hvala! Contacts: brshristov@live.com @BorisHristov www.borishristov.com 13.12.2014