Download presentation
Presentation is loading. Please wait.
Published byさゆり ありはら Modified over 5 years ago
1
Deadlocks Everything you ever wanted to ask but were too shy
Denis Reznik Deadlocks Everything you ever wanted to ask but were too shy
2
Sponsors Gold Sponsors Innovation Sponsor PASS Bronze Sponsors
3
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”
4
Agenda Classic Deadlock Common Deadlock
When Deadlock Graph doesn’t Make Sense Object Deadlock Range Deadlock Implicit Deadlock NOLOCK Deadlock Hidden Deadlock One Query Deadlock Undetectable Deadlock
5
Agenda – More Background
Locks (S) Shared (X) Exclusive (U) Update (Range*-*) Range (I*) Intent (Sch-S) Schema Stability (Sch-M) Schema Modification (BU) Bulk Update
6
Agenda – More Background
Transaction Isolation Levels Pessimistic READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE Optimistic READ COMMITTED SNAPSHOT SNAPSHOT
7
Classic Deadlock DEADLOCK! wait wait ID City 1 Kyiv 2 Sofia 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 Sofia 3 Dnipropetrovsk X wait wait ID User City_Id 1 Dejan Sarka 2 Mihail Mateev 3 Marcin Sheliga 4 John Smith X
8
Demo Common Deadlock
9
Lock Types - Shared X S S
10
Lock Types - Exclusive S X X
11
Demo Common Deadlock
12
Demo When Deadlock Graph does not make sense
13
Lock Types - Update s U U X S U X X U
14
Return different results so they are non-repeatable
NON-REPEATABLE READS BEGIN TRAN SELECT * FROM Users WHERE City = 'Sofia' ID City 1 Kyiv 2 Sofia 3 4 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 3 Sofia 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 = 'Sofia'
15
REPEATABLE READ ID City 1 Kyiv 2 Sofia 3 4 5 Tokyo 6 New York 7 ID
BEGIN TRAN SELECT * FROM Users WHERE City = 'Sofia' ID City 1 Kyiv 2 Sofia 3 4 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 3 Sofia 4 5 Tokyo 6 New York 7 UPDATE Users SET City = 'Kyiv' WHERE Id = 2 X S S ... S SELECT * FROM Users WHERE City = 'Sofia' COMMIT
16
Demo When Deadlock Graph does not make sense
17
Demo Object Deadlock
18
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
19
Lock Types – Intent locks
IS IS S
20
Demo Object Deadlock
21
Demo Range Deadlock
22
PHANTOM RECORDS Phantom Record ID City 1 Kyiv 2 Sofia 3 4 8 5 Tokyo 6
BEGIN TRAN SELECT * FROM Users WHERE City = 'Sofia' ID City 1 Kyiv 2 Sofia 3 4 8 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 Sofia 3 4 5 Tokyo 6 New York 7 INSERT INTO Users VALUES (8, 'Sofia') S S ... S S SELECT * FROM Users WHERE City = 'Sofia' Phantom Record
23
SERIALIZABLE ID City 1 Kyiv 2 Sofia 3 4 8 5 Tokyo 6 New York 7 ID City
BEGIN TRAN SELECT * FROM Users WHERE City = 'Sofia' ID City 1 Kyiv 2 Sofia 3 4 8 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 Sofia 3 4 5 Tokyo 6 New York 7 INSERT INTO Users VALUES (8, 'Sofia') RANGE S-S ... SELECT * FROM Users WHERE City = 'Sofia' COMMIT
24
Demo Range Deadlock
25
Demo Implicit Deadlock
26
READ COMMITTED SNAPSHOT
SELECT * FROM Users WHERE City = 'Kyiv' BEGIN TRAN UPDATE Users SET City = 'Sofia' WHERE City = 'Kyiv' ID City 1 Sofia 2 3 4 5 6 New York 7 ID City 1 Kyiv 2 Sofia 3 4 5 6 New York 7 X SELECT * FROM Users WHERE City = 'Kyiv' Version Store ID City 1 Kyiv tempdb
27
Demo Implicit Deadlock
28
Demo NOLOCK Deadlock
29
READ UNCOMMITTED (NOLOCK)
SELECT * FROM Users WHERE City = 'Kyiv' ID City 1 Kyiv 2 Sofia 3 4 5 6 New York 7 ID City 1 Kyiv 2 Sofia 3 4 5 6 7 BEGIN TRAN UPDATE Users SET City = 'Kyiv' WHERE City = 'New York' SELECT * FROM Users WHERE City = 'Kyiv' Dirty Read ROLLBACK X X SELECT * FROM Users WHERE City = 'Kyiv'
30
Demo NOLOCK Deadlock
31
Demo Hidden Deadlock
32
Demo One Query Deadlock
33
Demo Undetectable Deadlock
34
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
35
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
36
Sponsors Gold Sponsors Innovation Sponsor PASS Bronze Sponsors
37
Thank You! Denis Reznik Blog: Facebook: LinkedIn:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.