Deadlocks 3.0. Final Edition. Everything that developer needs to know Denis Reznik Microsoft SQL Server MVP Director of R&D at Intapp Kyiv.

Slides:



Advertisements
Similar presentations
Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
Advertisements

1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Accessing data Transactions. Agenda Questions from last class? Transactions concurrency Locking rollback.
1 Supplemental Notes: Practical Aspects of Transactions THIS MATERIAL IS OPTIONAL.
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004.
Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks.
Security and Transaction Management Pertemuan 8 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
Managing Concurrency in Web Applications. DBI 2007 HUJI-CS 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses.
1 Concurrency Control. 2 Transactions A transaction is a list of actions. The actions are reads (written R T (O)) and writes (written W T (O)) of database.
Managing Transaction and Lock Vu Tuyet Trinh Hanoi University of Technology 1.
1 IT420: Database Management and Organization Transactions 31 March 2006 Adina Crăiniceanu
Transactions and Exception Handling Eric Allsop SQLBits 6 th October 2007.
1099 Why Use InterBase? Bill Todd The Database Group, Inc.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
Concurrency and Transaction Processing. Concurrency models 1. Pessimistic –avoids conflicts by acquiring locks on data that is being read, so no other.
Unit 9 Transaction Processing. Key Concepts Distributed databases and DDBMS Distributed database advantages. Distributed database disadvantages Using.
Module 11 Creating Highly Concurrent SQL Server® 2008 R2 Applications.
Transactions and Locks A Quick Reference and Summary BIT 275.
Random Logic l Forum.NET l Transaction Isolation Levels Forum.NET Meeting ● Nov
Chapter 16 Concurrency. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.16-2 Topics in this Chapter Three Concurrency Problems Locking Deadlock.
SQL Server 2005 Engine Optimistic Concurrency Tony Rogerson, SQL Server MVP Independent Consultant 26 th.
Giovanni Chierico | May 2012 | Дубна Data Concurrency, Consistency and Integrity.
SQLintersection Understanding Transaction Isolation Levels Randy Knight Wednesday, 3:45-5:00.
Module 11: Managing Transactions and Locks
©Bob Godfrey, 2002, 2005 Lecture 17: Transaction Integrity and Concurrency BSA206 Database Management Systems.
Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes.
Module 14: Managing Transactions and Locks. Overview Introducing Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture.
In this session, you will learn to: Implement triggers Implement transactions Objectives.
SQL Server Deep Dive Denis Reznik Data Architect at Intapp.
Does the Optimistic Concurrency resolve your blocking problems Margarita Naumova, SQL Master Academy.
Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks
Read Dirty to Me: SQL Server Isolation Levels Wendy Pastrick Arrow IT Consulting.
SQLintersection Locks, Blocks, and Deadlocks Oh My! Randy Knight Wednesday, 2:15-3:15.
Database Transactions  Transaction Management and Concurrency Control.
SQL Server Performance Tuning
Thank You! #sqlsatdnipro Denis
Let Me Finish... Isolating Write Operations
LAB: Web-scale Data Management on a Cloud
Isolation Levels Understanding Transaction Temper Tantrums
Let Me Finish... Isolating Write Operations
Let Me Finish... Isolating Write Operations
Query Execution Expectation-Reality Denis Reznik
Optimistic Concurrency Internals
Everything you ever wanted to ask but were too shy
SQL Server 2014 Hidden Treasures Denis Reznik Microsoft SQL Server MVP
Hidden Gems of SQL Server 2014
Hidden gems of SQL Server 2016
මොඩියුල විශ්ලේෂණය Transactions කළමනාකරණය.
Transactions, Locking and Query Optimisation
Chapter 10 Transaction Management and Concurrency Control
SQL Server Performance Tuning Nowadays
Hidden Gems of SQL Server 2016
Understanding Transaction Isolation Levels
Let Me Finish... Isolating Write Operations
Hidden Gems of SQL Server 2014
Let Me Finish... Isolating Write Operations
Transactions and Concurrency
Deadlocks Everything you ever wanted to ask but were too shy
Sioux Falls, SD | Hosted by (605) SQL
Hidden Gems of SQL Server 2014
A Masters view on Locking and blocking
Hidden Gems of SQL Server 2014
About Wolf DBA for over 19 years At RDX for nearly 9
Denis Reznik SQL Server 2017 Hidden Gems.
Isolation Levels Understanding Transaction Temper Tantrums
Module 13: Creating Highly Concurrent SQL Server 2012 Applications
A Masters view on Locking and blocking
Denis Reznik SQL Server 2017 Hidden Gems.
A Masters view on Locking and blocking
Presentation transcript:

Deadlocks 3.0. Final Edition. Everything that developer needs to know Denis Reznik Microsoft SQL Server MVP Director of R&D at Intapp Kyiv

Sponsors Gold Sponsors: Bronze Sponsors: Swag Sponsors:

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

Agenda  Locks  Lock Types  Lock Escalation  Transaction Isolation Levels  Pessimistic  Optimistic  Deadlocks  Catching, Analyzing, Fixing

Locks

Lock Types - Shared S S S S X X

Lock Types - Exclusive X X X X S S

Lock Types - Update U U U U S S S S X X

Lock Types – Intent locks S S IS

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

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

Transaction Isolation Levels

READ UNCOMMITTED The less restricted Isolation Level Allow all collisions, which are allowed Allow Dirty Reads Doesn’t set Shared locks on read operations

DIRTY READS IDCity 1Kiev 2Varna New York 7 IDCity 1Varna New 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

READ COMMITTED Default Isolation Level Doesn’t allow Dirty Reads Shared Locks used for reads Shared locks released after the read

NO DIRTY READS IDCity 1Kiev 2Varna New York 7 IDCity 1Varna New 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

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

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 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

PHANTOM RECORDS IDCity 1Kiev 2Varna 3 4 5Tokyo 6New York 7 IDCity 1Kiev 2Varna Tokyo 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

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 IDCity 1Kiev 2Varna 3 4 5Tokyo 6New York 7 IDCity 1Kiev 2Varna Tokyo 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

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

READ COMMITTED SNAPSHOT IDCity 1Kiev 2Varna New York 7 IDCity 1Varna New 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

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

SNAPSHOT IDCity 1Kiev 2Varna New York 7 IDCity 1Varna New 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

Demo Deadlocks in Action

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

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

Sponsors Gold Sponsors: Bronze Sponsors: Swag Sponsors:

Thank You! Denis