SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

Más de Денис Резник

Масштабирование в 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 compact
начинаем работать с Sql server compactначинаем работать с Sql server compact
начинаем работать с Sql server compact
Денис Резник
 
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
Денис Резник
 

Más de Денис Резник (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
 

Último

+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@
 
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
 
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
Safe Software
 

Último (20)

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
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
 
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...
 
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
 
+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...
 
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
 
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
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
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
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
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
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
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 - 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...
 
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
 

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