SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Deadlock detected! All is lost or it's
too early to sound the alarm?
Denis Reznik
Sponsors
About me






3 |

Denis Reznik
Kiev, Ukraine
Database Architect at The Frayman Group
Microsoft MVP
Community enthusiast
Agenda
 Locks
 Lock Types
 Transaction Isolation Levels

 Deadlocks
 Classic Deadlocks
 Not obvious deadlocks
 Deadlock detecting and analyzing
Lock Types- Shared

X
S

S
Lock Types - Exclusive

S
X
X
Lock Types - Update

S
U
X
U

S
Lock Types – Intent locks

IS

S

IS
READ UNCOMMITTED
 The less restricted Isolation Level
 Allow all collisions, which READ
COMMITTED allow
 Allow Dirty Reads
 Doesn’t set Shared locks on read operations
Demo
READ UNCOMMITTED
DIRTY READS
SELECT * FROM Users
WHERE City = 'Kiev'

ID

Sofia
Kiev

2

Sofia

3

Sofia
Sofia

SELECT * FROM Users
WHERE City = 'Kiev'

5
ROLLBACK

1

4

BEGIN TRAN
X
UPDATE Users
SET City = 'Sofia'
WHERE City = 'Kiev'

City

Moscow

0 Records

6

New York

7

New York

SELECT * FROM Users
WHERE City = 'Kiev'
READ COMMITTED
 Default Isolation Level
 Doesn’t allow Dirty Reads
 Shared locks released after the read
NO DIRTY READS
ID
BEGIN TRAN
UPDATE Users
SET City = 'Sofia'
WHERE City = 'Kiev'

X

City

1

Sofia
Kiev

2

Sofia

3

Sofia

4

Sofia

5

Moscow

6

New York

7

New York

SELECT * FROM Users
WHERE City = 'Kiev'
S

SELECT * FROM Users
WHERE City = 'Kiev'

Wait for
Shared lock
on the row
Demo
READ COMMITTED
NON-REPEATABLE READS
BEGIN TRAN

ID
UPDATE Users
SET City = 'Kiev'
WHERE Id = 2

1
X

City
Kiev

2

Sofia
Kiev

S

3

Sofia

4

Sofia

S ...
S

5

Moscow

6

New York

7

New York

SELECT * FROM Users
WHERE City = 'Sofia'

SELECT * FROM Users
WHERE City = 'Sofia'
REPEATABLE READ





More restricted than READ COMMITTED
Doesn’t allow Dirty Reads
Doesn’t allow Non-Repeatable reads
Shared locks are hold to the end of the
transaction
NO NON-REPEATABLE READS
BEGIN TRAN

ID
UPDATE Users
SET City = ‘Kiev'
WHERE Id = 2

1
X

City
Kiev

2

Sofia
Kiev

S

3

Sofia

4

Sofia

S ...
S

5

Moscow

6

New York

7

New York

SELECT * FROM Users
WHERE City = 'Sofia'

SELECT * FROM Users
WHERE City = 'Sofia'
COMMIT
Demo
REPEATABLE READ
PHANTOM RECORDS
BEGIN TRAN

ID
UPDATE Users
SET City = 'Sofia'
WHERE Id = 'Kiev'

X

City

1

Sofia
Kiev

S

2

Sofia

S

3

Sofia

4

Sofia

S ...
S

5

Moscow

6

New York

7

New York

SELECT * FROM Users
WHERE City = 'Sofia'

SELECT * FROM Users
WHERE City = 'Sofia'
SERIALIZABLE






The most restricted Isolation Level
Doesn’t allow Dirty Reads
Doesn’t allow Non-Repeatable reads
Doesn’t allow Phantom Records
Lock range of keys
NO PHANTOM RECORDS
BEGIN TRAN

UPDATE Users
SET City = 'Sofia'
WHERE Id = 'Kiev'

X

City

1

Sofia
Kiev

2

Sofia

3

Sofia

4

Sofia

5

Moscow

6

New York

7

New York

RANGE S-S

ID

SELECT * FROM Users
WHERE City = 'Sofia'
...
SELECT * FROM Users
WHERE City = 'Sofia'
COMMIT
Demo
SERIALIZABLE
READ COMMITTED SNAPSHOT





Optimistic concurrency for reads
Use row-version store in tempdb
No shared locks on reads
The same collisions as in READ
COMMITTED
READ COMMITTED SNAPSHOT
ID
BEGIN TRAN
X
UPDATE Users
SET City = 'Sofia'
WHERE City = 'Kiev'
Version Store

ID

City

1

Kiev

tempdb

City

1

Sofia
Kiev

2

Sofia

3

Sofia

4

Sofia

5

Moscow

6

New York

7

New York

SELECT * FROM Users
WHERE City = 'Kiev'

SELECT * FROM Users
WHERE City = 'Kiev'
Demo
SNAPSHOT READ COMMITTED
SNAPSHOT








Optimistic concurrency for reads
Use row-version store in tempdb
No shared locks on reads
Doesn’t allow Dirty Reads
Doesn’t allow Non-Repeatable reads
Doesn’t allow Phantom Records
Update conflict detection
Demo
SNAPSHOT
How to avoid?
 Design database so, that it will be no
possibility for a deadlock occur 
 Modify tables in the same order
 Choose appropriate Transaction Isolation
Level and check possibility of a deadlock in
your Isolation Level
 There is no unsolvable deadlocks.. But there
can be solution, which will not suit you
completely
Sponsors
Thank you!
 Denis Reznik






Twitter: @denisreznik
Email: denisreznik@live.ru
Blog (in russian): http://reznik.uneta.com.ua
Facebook: https://www.facebook.com/denis.reznik.5
LinkedIn: http://ua.linkedin.com/pub/denis-reznik/3/502/234

Weitere ähnliche Inhalte

Mehr von Денис Резник (14)

iForum 2015: SQL vs. NoSQL
iForum 2015: SQL vs. NoSQLiForum 2015: SQL vs. NoSQL
iForum 2015: SQL vs. NoSQL
 
SqlSaturday199 - Columnstore Indexes
SqlSaturday199 - Columnstore IndexesSqlSaturday199 - Columnstore Indexes
SqlSaturday199 - Columnstore Indexes
 
Sql rally 2013 columnstore indexes
Sql rally 2013   columnstore indexesSql rally 2013   columnstore indexes
Sql rally 2013 columnstore indexes
 
24 hop - Deadlocks
24 hop - Deadlocks24 hop - Deadlocks
24 hop - Deadlocks
 
Масштабирование в SQL Azure - SQL Azure Federations
Масштабирование в SQL Azure - SQL Azure FederationsМасштабирование в SQL Azure - SQL Azure Federations
Масштабирование в SQL Azure - SQL Azure Federations
 
Масштабирование в SQL Azure - SQL Azure Federations
Масштабирование в SQL Azure - SQL Azure FederationsМасштабирование в SQL Azure - SQL Azure Federations
Масштабирование в SQL Azure - SQL Azure Federations
 
SQL Server Denali
SQL Server DenaliSQL Server Denali
SQL Server Denali
 
Sql azure и все, все, все...
Sql azure и все, все, все...Sql azure и все, все, все...
Sql azure и все, все, все...
 
начинаем работать с Sql server compact
начинаем работать с Sql server compactначинаем работать с Sql server compact
начинаем работать с Sql server compact
 
Sql server 2011
Sql server 2011Sql server 2011
Sql server 2011
 
MS Swit 2010
MS Swit 2010MS Swit 2010
MS Swit 2010
 
WebMatrix
WebMatrixWebMatrix
WebMatrix
 
ASP.NET MVC 3
ASP.NET MVC 3ASP.NET MVC 3
ASP.NET MVC 3
 
SQL Server StreamIinsight - data processing in real time
SQL Server StreamIinsight - data processing in real timeSQL Server StreamIinsight - data processing in real time
SQL Server StreamIinsight - data processing in real time
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
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 TerraformAndrey Devyatkin
 
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 FresherRemote DBA Services
 
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
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
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 DiscoveryTrustArc
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
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, Adobeapidays
 
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 Takeoffsammart93
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
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 SavingEdi Saputra
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
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 businesspanagenda
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 

Kürzlich hochgeladen (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
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
 
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, ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
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
 
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
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

SqlSaturday199 - Deadlocks

  • 1. Deadlock detected! All is lost or it's too early to sound the alarm? Denis Reznik
  • 3. About me      3 | Denis Reznik Kiev, Ukraine Database Architect at The Frayman Group Microsoft MVP Community enthusiast
  • 4. Agenda  Locks  Lock Types  Transaction Isolation Levels  Deadlocks  Classic Deadlocks  Not obvious deadlocks  Deadlock detecting and analyzing
  • 6. Lock Types - Exclusive S X X
  • 7. Lock Types - Update S U X U S
  • 8. Lock Types – Intent locks IS S IS
  • 9. READ UNCOMMITTED  The less restricted Isolation Level  Allow all collisions, which READ COMMITTED allow  Allow Dirty Reads  Doesn’t set Shared locks on read operations
  • 11. DIRTY READS SELECT * FROM Users WHERE City = 'Kiev' ID Sofia Kiev 2 Sofia 3 Sofia Sofia SELECT * FROM Users WHERE City = 'Kiev' 5 ROLLBACK 1 4 BEGIN TRAN X UPDATE Users SET City = 'Sofia' WHERE City = 'Kiev' City Moscow 0 Records 6 New York 7 New York SELECT * FROM Users WHERE City = 'Kiev'
  • 12. READ COMMITTED  Default Isolation Level  Doesn’t allow Dirty Reads  Shared locks released after the read
  • 13. NO DIRTY READS ID BEGIN TRAN UPDATE Users SET City = 'Sofia' WHERE City = 'Kiev' X City 1 Sofia Kiev 2 Sofia 3 Sofia 4 Sofia 5 Moscow 6 New York 7 New York SELECT * FROM Users WHERE City = 'Kiev' S SELECT * FROM Users WHERE City = 'Kiev' Wait for Shared lock on the row
  • 15. NON-REPEATABLE READS BEGIN TRAN ID UPDATE Users SET City = 'Kiev' WHERE Id = 2 1 X City Kiev 2 Sofia Kiev S 3 Sofia 4 Sofia S ... S 5 Moscow 6 New York 7 New York SELECT * FROM Users WHERE City = 'Sofia' SELECT * FROM Users WHERE City = 'Sofia'
  • 16. REPEATABLE READ     More restricted than READ COMMITTED Doesn’t allow Dirty Reads Doesn’t allow Non-Repeatable reads Shared locks are hold to the end of the transaction
  • 17. NO NON-REPEATABLE READS BEGIN TRAN ID UPDATE Users SET City = ‘Kiev' WHERE Id = 2 1 X City Kiev 2 Sofia Kiev S 3 Sofia 4 Sofia S ... S 5 Moscow 6 New York 7 New York SELECT * FROM Users WHERE City = 'Sofia' SELECT * FROM Users WHERE City = 'Sofia' COMMIT
  • 19. PHANTOM RECORDS BEGIN TRAN ID UPDATE Users SET City = 'Sofia' WHERE Id = 'Kiev' X City 1 Sofia Kiev S 2 Sofia S 3 Sofia 4 Sofia S ... S 5 Moscow 6 New York 7 New York SELECT * FROM Users WHERE City = 'Sofia' SELECT * FROM Users WHERE City = 'Sofia'
  • 20. SERIALIZABLE      The most restricted Isolation Level Doesn’t allow Dirty Reads Doesn’t allow Non-Repeatable reads Doesn’t allow Phantom Records Lock range of keys
  • 21. NO PHANTOM RECORDS BEGIN TRAN UPDATE Users SET City = 'Sofia' WHERE Id = 'Kiev' X City 1 Sofia Kiev 2 Sofia 3 Sofia 4 Sofia 5 Moscow 6 New York 7 New York RANGE S-S ID SELECT * FROM Users WHERE City = 'Sofia' ... SELECT * FROM Users WHERE City = 'Sofia' COMMIT
  • 23. READ COMMITTED SNAPSHOT     Optimistic concurrency for reads Use row-version store in tempdb No shared locks on reads The same collisions as in READ COMMITTED
  • 24. READ COMMITTED SNAPSHOT ID BEGIN TRAN X UPDATE Users SET City = 'Sofia' WHERE City = 'Kiev' Version Store ID City 1 Kiev tempdb City 1 Sofia Kiev 2 Sofia 3 Sofia 4 Sofia 5 Moscow 6 New York 7 New York SELECT * FROM Users WHERE City = 'Kiev' SELECT * FROM Users WHERE City = 'Kiev'
  • 26. SNAPSHOT        Optimistic concurrency for reads Use row-version store in tempdb No shared locks on reads Doesn’t allow Dirty Reads Doesn’t allow Non-Repeatable reads Doesn’t allow Phantom Records Update conflict detection
  • 28. How to avoid?  Design database so, that it will be no possibility for a deadlock occur   Modify tables in the same order  Choose appropriate Transaction Isolation Level and check possibility of a deadlock in your Isolation Level  There is no unsolvable deadlocks.. But there can be solution, which will not suit you completely
  • 30. Thank you!  Denis Reznik      Twitter: @denisreznik Email: denisreznik@live.ru Blog (in russian): http://reznik.uneta.com.ua Facebook: https://www.facebook.com/denis.reznik.5 LinkedIn: http://ua.linkedin.com/pub/denis-reznik/3/502/234