SlideShare uma empresa 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

Mais conteúdo relacionado

Mais 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

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 slidevu2urc
 
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 StrategiesBoston Institute of Analytics
 
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.pdfhans926745
 
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 2024The Digital Insurer
 
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 BusinessPixlogix Infotech
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
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 RobisonAnna Loughnan Colquhoun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
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 WorkerThousandEyes
 
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 2024Rafal Los
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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 AutomationSafe Software
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
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
 
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 2024The Digital Insurer
 
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
 
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...DianaGray10
 

Último (20)

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
 
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
 
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
 
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
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
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
 
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
 
+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...
 
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...
 

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