SlideShare une entreprise Scribd logo
1  sur  29
Welcome to the nightmare of 
locking, blocking and isolation levels! 
Boris Hristov
Sponsors 
Main Sponsor 
Gold Sponsors 
#sqlsatistanbul
So who am I? 
#sqlsatistanbul 
So who am I? 
@BorisHristov
Agenda… 
Locks. What is there for us? 
Troubleshooting locking problems 
Transaction Isolation Levels 
#sqlsatistanbul
Locks. What is there for us? 
#sqlsatistanbul
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… 
#sqlsatistanbul
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 
#sqlsatistanbul
#sqlsatistanbul 
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
#sqlsatistanbul 
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
#sqlsatistanbul 
Lock Hierarchy 
Database 
Table 
Page 
Row
Let’s update a row! 
What do we need? 
USE AdventureWorks2012 
GO 
UPDATE [Person].[Address] 
SET AddressLine1=’Istanbul, Turkey' 
WHERE AddressID=2 
#sqlsatistanbul 
S 
IX 
Header 
Row 
Row 
Row 
Row 
Row 
IX 
X
Methods to View Locking Information 
Dynamic 
Management 
#sqlsatistanbul 
Views 
SQL Server 
Profiler or 
Extended Events 
Performance 
monitor or 
Activity Monitor
Troubleshooting locking problems 
#sqlsatistanbul
#sqlsatistanbul 
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
#sqlsatistanbul 
Lock escalation 
S 
S 
X 
>= 5000 
IX 
Header 
Row 
Row 
Row 
Row 
Row 
IX 
X 
X 
X 
X
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 
#sqlsatistanbul 
Controlling Lock escalation 
SELECT lock_escalation_desc 
FROM sys.tables 
WHERE name = 'Person.Address' 
ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE | DISABLE}
#sqlsatistanbul 
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 
#sqlsatistanbul
#sqlsatistanbul 
DEMO 
Monitor for locks with xEvents 
Lock escalation – both to table and partition 
Deadlock and the SET DEADLOCK_PRIORITY option
Transaction isolation levels 
#sqlsatistanbul
Read Uncommitted 
(pessimistic concurrency control) 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?) 
Transaction 1 
eXclusive lock 
Update 
#sqlsatistanbul 
Select 
Transaction 2 
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 
No non-repeatable reads possible (updates during Transaction 1) 
Phantom records still possible (inserts during Transaction 1) 
#sqlsatistanbul 
Update 
Transaction 2
Transaction 1 S(hared) lock 
select 
Even phantom records are not possible! 
Highest pessimistic level of isolation, lowest level of concurrency 
#sqlsatistanbul 
Insert 
Transaction 2 
Serializable 
(pessimistic concurrency control) 
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
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 
#sqlsatistanbul 
Optimistic Concurrency
Transaction 1 
Select Select in RCSI 
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 
#sqlsatistanbul 
RCSI and SI 
(optimistic concurrency control) 
V1 V2 
Transaction 2 
Select in SI
#sqlsatistanbul 
DEMO 
Playing around with the Isolation levels
#sqlsatistanbul 
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 
#sqlsatistanbul
Teşekkür ederim! 
Contacts: 
brshristov@live.com 
@BorisHristov 
www.borishristov.com 
#sqlsatistanbul

Contenu connexe

Tendances

11. transaction sql
11. transaction sql11. transaction sql
11. transaction sql
Umang Gupta
 
4. concurrency control
4. concurrency control4. concurrency control
4. concurrency control
AbDul ThaYyal
 
Concurrency (Distributed computing)
Concurrency (Distributed computing)Concurrency (Distributed computing)
Concurrency (Distributed computing)
Sri Prasanna
 
16. Concurrency Control in DBMS
16. Concurrency Control in DBMS16. Concurrency Control in DBMS
16. Concurrency Control in DBMS
koolkampus
 

Tendances (20)

SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction Management
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sql
 
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)
 
MariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale:  an Intelligent Database ProxyMariaDB MaxScale:  an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database Proxy
 
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
 
TransactionScope
TransactionScopeTransactionScope
TransactionScope
 
4. concurrency control
4. concurrency control4. concurrency control
4. concurrency control
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Concurrency (Distributed computing)
Concurrency (Distributed computing)Concurrency (Distributed computing)
Concurrency (Distributed computing)
 
MariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database ProxyMariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database Proxy
 
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
 
[Altibase] 6 what is the mvcc
[Altibase] 6 what is the mvcc[Altibase] 6 what is the mvcc
[Altibase] 6 what is the mvcc
 
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
 
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...
 
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
 
Distributed Systems Theory for Mere Mortals
Distributed Systems Theory for Mere MortalsDistributed Systems Theory for Mere Mortals
Distributed Systems Theory for Mere Mortals
 
Drizzle Talk
Drizzle TalkDrizzle Talk
Drizzle Talk
 
16. Concurrency Control in DBMS
16. Concurrency Control in DBMS16. Concurrency Control in DBMS
16. Concurrency Control in DBMS
 
[Altibase] 5 durability
[Altibase] 5 durability[Altibase] 5 durability
[Altibase] 5 durability
 
Optimistic concurrency control in Distributed Systems
Optimistic concurrency control in Distributed SystemsOptimistic concurrency control in Distributed Systems
Optimistic concurrency control in Distributed Systems
 

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

Similaire à The Nightmare of Locking, Blocking and Isolation Levels! (20)

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!
 
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
 
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 deadlocking. SQLSaturday #257, Verona
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, VeronaThe nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Olier
 
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
 
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...
 
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
 
Database security
Database securityDatabase security
Database security
 
Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
 
Lecture 5. MS SQL. Transactions
Lecture 5. MS SQL. TransactionsLecture 5. MS SQL. Transactions
Lecture 5. MS SQL. Transactions
 
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)
 
Transactions and Concurrency Control Patterns
Transactions and Concurrency Control PatternsTransactions and Concurrency Control Patterns
Transactions and Concurrency Control Patterns
 
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
 
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
 
Non blocking programming and waiting
Non blocking programming and waitingNon blocking programming and waiting
Non blocking programming and waiting
 
Transactions and Concurrency Control Patterns
Transactions and Concurrency Control PatternsTransactions and Concurrency Control Patterns
Transactions and Concurrency Control Patterns
 
Ibm db2 case study
Ibm db2 case studyIbm db2 case study
Ibm db2 case study
 

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 (19)

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

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

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
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...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
+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...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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...
 
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
 
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
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 

The Nightmare of Locking, Blocking and Isolation Levels!

  • 1. Welcome to the nightmare of locking, blocking and isolation levels! Boris Hristov
  • 2. Sponsors Main Sponsor Gold Sponsors #sqlsatistanbul
  • 3. So who am I? #sqlsatistanbul So who am I? @BorisHristov
  • 4. Agenda… Locks. What is there for us? Troubleshooting locking problems Transaction Isolation Levels #sqlsatistanbul
  • 5. Locks. What is there for us? #sqlsatistanbul
  • 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… #sqlsatistanbul
  • 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 #sqlsatistanbul
  • 8. #sqlsatistanbul 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. #sqlsatistanbul 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. #sqlsatistanbul Lock Hierarchy Database Table Page Row
  • 11. Let’s update a row! What do we need? USE AdventureWorks2012 GO UPDATE [Person].[Address] SET AddressLine1=’Istanbul, Turkey' WHERE AddressID=2 #sqlsatistanbul S IX Header Row Row Row Row Row IX X
  • 12. Methods to View Locking Information Dynamic Management #sqlsatistanbul Views SQL Server Profiler or Extended Events Performance monitor or Activity Monitor
  • 14. #sqlsatistanbul 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. #sqlsatistanbul Lock escalation S S X >= 5000 IX Header Row Row Row Row Row IX X X X X
  • 16. 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 #sqlsatistanbul Controlling Lock escalation SELECT lock_escalation_desc FROM sys.tables WHERE name = 'Person.Address' ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE | DISABLE}
  • 17. #sqlsatistanbul 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 #sqlsatistanbul
  • 19. #sqlsatistanbul DEMO Monitor for locks with xEvents Lock escalation – both to table and partition Deadlock and the SET DEADLOCK_PRIORITY option
  • 20. Transaction isolation levels #sqlsatistanbul
  • 21. Read Uncommitted (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?) Transaction 1 eXclusive lock Update #sqlsatistanbul Select Transaction 2 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 No non-repeatable reads possible (updates during Transaction 1) Phantom records still possible (inserts during Transaction 1) #sqlsatistanbul Update Transaction 2
  • 23. Transaction 1 S(hared) lock select Even phantom records are not possible! Highest pessimistic level of isolation, lowest level of concurrency #sqlsatistanbul Insert Transaction 2 Serializable (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  • 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 #sqlsatistanbul Optimistic Concurrency
  • 25. Transaction 1 Select Select in RCSI 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 #sqlsatistanbul RCSI and SI (optimistic concurrency control) V1 V2 Transaction 2 Select in SI
  • 26. #sqlsatistanbul DEMO Playing around with the Isolation levels
  • 27. #sqlsatistanbul 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 #sqlsatistanbul
  • 29. Teşekkür ederim! Contacts: brshristov@live.com @BorisHristov www.borishristov.com #sqlsatistanbul