Download presentation
Presentation is loading. Please wait.
Published byBeryl York Modified over 8 years ago
1
Deadlocks 3.0. Final Edition. Everything that developer needs to know Denis Reznik Microsoft SQL Server MVP Director of R&D at Intapp Kyiv
2
Sponsors Gold Sponsors: Bronze Sponsors: Swag Sponsors:
3
About me Denis Reznik Kyiv, Ukraine Director of R&D at Intapp Kyiv Microsoft MVP Leader of Kyiv SQL Server User Group Co-author of “SQL Server MVP Deep Dives 2” Community Enthusiast
4
Agenda Locks Lock Types Lock Escalation Transaction Isolation Levels Pessimistic Optimistic Deadlocks Catching, Analyzing, Fixing
5
Locks
6
Lock Types - Shared S S S S X X
7
Lock Types - Exclusive X X X X S S
8
Lock Types - Update U U U U S S S S X X
9
Lock Types – Intent locks S S IS
10
Lock Escalation S S S S S S … S S 1 2 3 n n = 5000 Table S S if lock can’t be put on table, process will try this every n + 1250 rows 12 Buffer Pool Memory for locks S S > 24%
11
Lock Escalation Lock Block – 128 bytes 1 million blocks ~64 MB Escalation can be done only to Table or Partition level Escalation can be disabled for the table Trace Flag – 1224 Disable escalation based on rowcount
12
Transaction Isolation Levels
13
READ UNCOMMITTED The less restricted Isolation Level Allow all collisions, which are allowed Allow Dirty Reads Doesn’t set Shared locks on read operations
14
DIRTY READS IDCity 1Kiev 2Varna 3 4 5 6New York 7 IDCity 1Varna 2 3 4 5 6New York 7 SELECT * FROM Users WHERE City = 'Kiev' BEGIN TRAN UPDATE Users SET City = 'Varna' WHERE City = 'Kiev' SELECT * FROM Users WHERE City = 'Kiev' 0 Records ROLLBACK SELECT * FROM Users WHERE City = 'Kiev' X
15
READ COMMITTED Default Isolation Level Doesn’t allow Dirty Reads Shared Locks used for reads Shared locks released after the read
16
NO DIRTY READS IDCity 1Kiev 2Varna 3 4 5 6New York 7 IDCity 1Varna 2 3 4 5 6New York 7 SELECT * FROM Users WHERE City = 'Kiev' BEGIN TRAN UPDATE Users SET City = 'Varna' WHERE City = 'Kiev' SELECT * FROM Users WHERE City = 'Kiev' Wait for Shared lock on the row X S
17
NON-REPEATABLE READS IDCity 1Kiev 2Varna 3 4 5Tokyo 6New York 7 IDCity 1Kiev 2 3Varna 4 5Tokyo 6New York 7 BEGIN TRAN SELECT * FROM Users WHERE City = 'Varna' UPDATE Users SET City = 'Kiev' WHERE Id = 2 SELECT * FROM Users WHERE City = 'Varna' X S S... S
18
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
19
NO NON-REPEATABLE READS IDCity 1Kiev 2Varna 3 4 5Tokyo 6New York 7 IDCity 1Kiev 2 3Varna 4 5Tokyo 6New York 7 BEGIN TRAN SELECT * FROM Users WHERE City = 'Varna' UPDATE Users SET City = 'Kiev' WHERE Id = 2 SELECT * FROM Users WHERE City = 'Varna' X S S... S COMMIT
20
PHANTOM RECORDS IDCity 1Kiev 2Varna 3 4 5Tokyo 6New York 7 IDCity 1Kiev 2Varna 3 4 8 5Tokyo 6New York 7 BEGIN TRAN SELECT * FROM Users WHERE City = 'Varna' INSERT INTO Users VALUES (8, 'Varna') SELECT * FROM Users WHERE City = 'Varna' S S... S S
21
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
22
NO PHANTOM RECORDS IDCity 1Kiev 2Varna 3 4 5Tokyo 6New York 7 IDCity 1Kiev 2Varna 3 4 8 5Tokyo 6New York 7 BEGIN TRAN SELECT * FROM Users WHERE City = 'Varna' INSERT INTO Users VALUES (8, 'Varna') SELECT * FROM Users WHERE City = 'Varna' RANGE S-S... COMMIT
23
READ COMMITTED SNAPSHOT Optimistic concurrency for reads Use row-version store in tempdb No shared locks on reads Isolation rules are the same as in READ COMMITTED
24
READ COMMITTED SNAPSHOT IDCity 1Kiev 2Varna 3 4 5 6New York 7 IDCity 1Varna 2 3 4 5 6New York 7 BEGIN TRAN UPDATE Users SET City = 'Varna' WHERE City = 'Kiev' SELECT * FROM Users WHERE City = 'Kiev' SELECT * FROM Users WHERE City = 'Kiev' X tempdb IDCity 1Kiev Version Store
25
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
26
SNAPSHOT IDCity 1Kiev 2Varna 3 4 5 6New York 7 IDCity 1Varna 2 3 4 5 6New York 7 BEGIN TRAN UPDATE Users SET City = 'Varna' WHERE City = 'Kiev' BEGIN TRAN UPDATE Users SET City = 'London' WHERE City = 'Kiev' X tempdb IDCity 1Kiev Version Store COMMIT
27
Demo Deadlocks in Action
28
Three Rules of Deadlocks If deadlock is possible, it will occur. Deadlock should not be solved, before the real reason of it wasn’t found. There is no unsolvable deadlocks. But there can be solutions, which will not suit you completely
29
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 Keep transactions as small as possible Use stress-testing for your application or database
30
Sponsors Gold Sponsors: Bronze Sponsors: Swag Sponsors:
31
Thank You! Denis Reznik @denisreznik denisreznik@live.ru
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.