Presentation is loading. Please wait.

Presentation is loading. Please wait.

Everything you ever wanted to ask but were too shy

Similar presentations


Presentation on theme: "Everything you ever wanted to ask but were too shy"— Presentation transcript:

1 Everything you ever wanted to ask but were too shy
Deadlocks Everything you ever wanted to ask but were too shy Denis Reznik Data Architect at Intapp, Inc. Microsoft Data Platform MVP

2 About me Denis Reznik Kyiv, Ukraine Data Architect at Intapp, Inc.
Microsoft Data Platform MVP Co-Founder of Ukrainian Data Community Kyiv PASS Regional Mentor, CEE Co-author of “SQL Server MVP Deep Dives 2”

3 Agenda Classic Deadlock Common Deadlock
When Deadlock Graph doesn’t Make Sense Object Deadlock Range Deadlock Implicit Deadlock Hidden Deadlock NOLOCK Deadlock One Query Deadlock Undetectable Deadlock

4 Classic Deadlock DEADLOCK! wait wait ID City 1 Kyiv 2 Ljubljana 3
BEGIN TRAN UPDATE Users SET CityId = 2 WHERE Id = 4 UPDATE City SET Name = 'Dnipro' WHERE Id = 3 BEGIN TRAN UPDATE City SET Name = 'Dnipro' WHERE Id = 3 UPDATE Users SET CityId = 2 WHERE Id = 4 ID City 1 Kyiv 2 Ljubljana 3 Dnipropetrovsk X wait wait ID User City_Id 1 Dejan Sarka 2 Mladen Prajdic 3 Matija Lah 4 John Smith X

5 How to Deal with the Deadlock?
Catch the Deadlock Find the Root Cause Fix the Deadlock

6 Demo Common Deadlock

7 Lock Types - Shared X S S

8 Lock Types - Exclusive S X X

9 Deadlocks. When Graph does not make sense.
Demo Deadlocks. When Graph does not make sense.

10 Lock Types - Update s U U X S U X X U

11 Return different results so they are non-repeatable
NON-REPEATABLE READS BEGIN TRAN SELECT * FROM Users WHERE City = 'Ljubljana' ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 3 Ljubljana 4 5 Tokyo 6 New York 7 UPDATE Users SET City = 'Kyiv' WHERE Id = 2 X S Return different results so they are non-repeatable S ... S SELECT * FROM Users WHERE City = 'Ljubljana'

12 REPEATABLE READ ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo 6 New York 7 ID
BEGIN TRAN SELECT * FROM Users WHERE City = 'Ljubljana' ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 3 Ljubljana 4 5 Tokyo 6 New York 7 UPDATE Users SET City = 'Kyiv' WHERE Id = 2 X S S ... S SELECT * FROM Users WHERE City = 'Ljubljana' COMMIT

13 Demo Object Deadlock

14 Lock Escalation … >= 25% 1 2 Table S S Buffer Pool n = 5000
Memory for locks 1 S >= 25% 2 S 3 S n S n = 5000 if lock can’t be put on table, process will try this every n rows

15 Lock Types – Intent locks
IS IS S

16 Demo Range Deadlock

17 PHANTOM RECORDS Phantom Record ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo
BEGIN TRAN SELECT * FROM Users WHERE City = 'Ljubljana' ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 Ljubljana 3 4 8 5 Tokyo 6 New York 7 INSERT INTO Users VALUES (8, 'Ljubljana') S S ... S S SELECT * FROM Users WHERE City = 'Ljubljana' Phantom Record

18 SERIALIZABLE ID City 1 Kyiv 2 Ljubljana 3 4 8 5 Tokyo 6 New York 7 ID
BEGIN TRAN SELECT * FROM Users WHERE City = 'Ljubljana' ID City 1 Kyiv 2 Ljubljana 3 4 8 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo 6 New York 7 INSERT INTO Users VALUES (8, 'Ljubljana') RANGE S-S ... SELECT * FROM Users WHERE City = 'Ljubljana' COMMIT

19 Demo Range Deadlock

20 READ COMMITTED SNAPSHOT
SELECT * FROM Users WHERE City = 'Kyiv' BEGIN TRAN UPDATE Users SET City = 'Ljubljana' WHERE City = 'Kyiv' ID City 1 Ljubljana 2 3 4 5 6 New York 7 ID City 1 Kyiv 2 Ljubljana 3 4 5 6 New York 7 X SELECT * FROM Users WHERE City = 'Kyiv' Version Store ID City 1 Kyiv tempdb

21 SNAPSHOT ID City 1 Ljubljana 2 3 4 5 6 New York 7 ID City 1 Kyiv 2
BEGIN TRAN UPDATE Users SET City = 'Ljubljana' WHERE City = 'Kyiv' BEGIN TRAN UPDATE Users SET City = 'London' WHERE City = 'Kyiv' X COMMIT Version Store ID City 1 Kyiv tempdb

22 Demo Implicit Deadlock

23 READ UNCOMMITTED (NOLOCK)
SELECT * FROM Users WHERE City = 'Kyiv' ID City 1 Kyiv 2 Ljubljana 3 4 5 6 7 ID City 1 Kyiv 2 Ljubljana 3 4 5 6 New York 7 BEGIN TRAN UPDATE Users SET City = 'Kyiv' WHERE City = 'New York' SELECT * FROM Users WHERE City = 'Kyiv' ROLLBACK Dirty Read X X SELECT * FROM Users WHERE City = 'Kyiv'

24 Demo NOLOCK Deadlock

25 Three Rules of Deadlocks
If deadlock is possible, it will occur. Deadlock should not be solved, before the root cause of it wasn’t found. There are no unsolvable deadlocks. But there can be solutions, which will not suit you completely 25 |

26 How to Avoid? Design database so, that it will be no possibility for a deadlock occur Ha-ha-ha  Modify tables in the same order Choose appropriate Transaction Isolation Level Consider optimistic ones Keep transactions as small as possible Use stress-testing for your application or database 26 |

27 Thank You! Denis Reznik Twitter: @denisreznik
Blog: Facebook: LinkedIn:


Download ppt "Everything you ever wanted to ask but were too shy"

Similar presentations


Ads by Google