SlideShare une entreprise Scribd logo
1  sur  31
Télécharger pour lire hors ligne
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!

Contenu connexe

Tendances

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
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sql
Umang Gupta
 
SSL, X.509, HTTPS - How to configure your HTTPS server
SSL, X.509, HTTPS - How to configure your HTTPS serverSSL, X.509, HTTPS - How to configure your HTTPS server
SSL, X.509, HTTPS - How to configure your HTTPS server
hannob
 

Tendances (16)

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
The nightmare of locking, blocking and isolation levelsThe 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!Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!
 
Deep Into Isolation Levels
Deep Into Isolation LevelsDeep Into Isolation Levels
Deep Into 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!
 
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!
 
Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction Management
 
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
 
Distributed Systems Theory for Mere Mortals - Java Day Istanbul May 2017
Distributed Systems Theory for Mere Mortals - Java Day Istanbul May 2017 Distributed Systems Theory for Mere Mortals - Java Day Istanbul May 2017
Distributed Systems Theory for Mere Mortals - Java Day Istanbul May 2017
 
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)
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Top Mistakes When Writing Reactive Applications - Scala by the Bay 2016
Top Mistakes When Writing Reactive Applications - Scala by the Bay 2016Top Mistakes When Writing Reactive Applications - Scala by the Bay 2016
Top Mistakes When Writing Reactive Applications - Scala by the Bay 2016
 
Distributed Systems Theory for Mere Mortals
Distributed Systems Theory for Mere MortalsDistributed Systems Theory for Mere Mortals
Distributed Systems Theory for Mere Mortals
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sql
 
SSL, X.509, HTTPS - How to configure your HTTPS server
SSL, X.509, HTTPS - How to configure your HTTPS serverSSL, X.509, HTTPS - How to configure your HTTPS server
SSL, X.509, HTTPS - How to configure your HTTPS server
 

Similaire à 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
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12
Syed Asrarali
 
Linux synchronization tools
Linux synchronization toolsLinux synchronization tools
Linux synchronization tools
mukul bhardwaj
 

Similaire à 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
 
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
 
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
 
Troubleshooting Deadlocks in SQL Server 2000
Troubleshooting Deadlocks in SQL Server 2000Troubleshooting Deadlocks in SQL Server 2000
Troubleshooting Deadlocks in SQL Server 2000
 
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...
 
Database security
Database securityDatabase security
Database security
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
 
Ibm db2 case study
Ibm db2 case studyIbm db2 case study
Ibm db2 case study
 
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and LockingGeek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
 
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
 
Webinar slides: 9 DevOps Tips for Going in Production with Galera Cluster for...
Webinar slides: 9 DevOps Tips for Going in Production with Galera Cluster for...Webinar slides: 9 DevOps Tips for Going in Production with Galera Cluster for...
Webinar slides: 9 DevOps Tips for Going in Production with Galera Cluster for...
 
Lecture 5. MS SQL. Transactions
Lecture 5. MS SQL. TransactionsLecture 5. MS SQL. Transactions
Lecture 5. MS SQL. Transactions
 
MariaDB High Availability Webinar
MariaDB High Availability WebinarMariaDB High Availability Webinar
MariaDB High Availability Webinar
 
Persistence Is Futile - Implementing Delayed Durability
Persistence Is Futile - Implementing Delayed DurabilityPersistence Is Futile - Implementing Delayed Durability
Persistence Is Futile - Implementing Delayed Durability
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12
 
Best Practice for Achieving High Availability in MariaDB
Best Practice for Achieving High Availability in MariaDBBest Practice for Achieving High Availability in MariaDB
Best Practice for Achieving High Availability in MariaDB
 
Choosing A Concurrency Model, Optimistic Or Pessimistic
Choosing A Concurrency Model, Optimistic Or PessimisticChoosing A Concurrency Model, Optimistic Or Pessimistic
Choosing A Concurrency Model, Optimistic Or Pessimistic
 
Linux synchronization tools
Linux synchronization toolsLinux synchronization tools
Linux synchronization tools
 
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
 

Plus de Boris Hristov

The World of Business Intelligence
The World of Business IntelligenceThe World of Business Intelligence
The World of Business Intelligence
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
 

Plus de Boris Hristov (18)

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
 
The World of Business Intelligence
The World of Business IntelligenceThe World of Business Intelligence
The World of Business Intelligence
 
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
 
Securing SQL Azure DB? How?
Securing SQL Azure DB? How?Securing SQL Azure DB? How?
Securing SQL Azure DB? How?
 
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
 
Database Performance
Database PerformanceDatabase Performance
Database Performance
 
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!
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
+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@
 

Dernier (20)

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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
+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...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

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