SQLintersection Locks, Blocks, and Deadlocks Oh My! Randy Knight Wednesday, 2:15-3:15
© SQLintersection. All rights reserved. Randy Knight Microsoft Certified Master in SQL Server 20+Years of database experience, focusing on SQL Server since Worked in a variety of settings, including six years as a Database Architect for match.com Founder, SQL Solutions Group
© SQLintersection. All rights reserved. Overview Review ACID Properties Transaction Isolation Levels Defined Locking Blocking Deadlocks Detection and Prevention Excessive Blocking Deadlocks
© SQLintersection. All rights reserved. ACID Atomic Consistent Isolated Durable
© SQLintersection. All rights reserved. Isolation Levels ANSI Standard Levels Level 0: Read Uncommitted Level 1: Read Committed Level 2: Repeatable Read Level 3: Serializable Snapshot / Versioning
© SQLintersection. All rights reserved. Locking Defined This is how SQL Server handles Isolation Read Locks Shared Concurrent Write Locks Exclusive Isolated Lock Levels Table, Page, Row
© SQLintersection. All rights reserved. Blocking Defined Definition User A has a lock on Resource X User B needs a lock on Resource X Locks are incompatible This is a Block This is normal behavior! What we care about is EXCESSIVE blocking Blocking Chains User A block B, B blocks C, and on and on
Demo Locks and Blocks
© SQLintersection. All rights reserved. Preventing Excessive Blocking Short transactions Appropriate Indexes SNAPSHOT isolation level
© SQLintersection. All rights reserved. Deadlocks Defined User A and User B both have locks that prevent the other’s transaction from completing Will never clear itself SQL server handles automatically Never normal
© SQLintersection. All rights reserved. Deadlock Detection Lock Monitor Thread Every 5 seconds (normally) Looks at blocks Can the block clear itself Cyclic blocks would last forever Chooses a victim if needed How is the victim chosen DEADLOCK_PRIORITY If they are the same priority, the least expensive transaction to rollback
© SQLintersection. All rights reserved. Viewing Deadlock Information SQL Server Error Log Trace Flags 1204 1222 SQL Server Profiler Extended Events Already on in the default session XML is different so doesn’t show up as a deadlock graph
Demo Deadlocks
© SQLintersection. All rights reserved. Common Deadlock Causes Table Order Key Lookup Serializable Isolation Level Parallelism
© SQLintersection. All rights reserved. Recommendations Minimize blocking Short transactions SNAPSHOT when appropriate Locking is normal Blocking is normal Excessive Blocking is not Deadlocks are not normal behavior
© SQLintersection. All rights reserved. Review Review ACID Properties Transaction Isolation Levels Defined Locking Blocking Deadlocks Detection and Prevention Excessive Blocking Deadlocks
© SQLintersection. All rights reserved. References Forums MSDN SQLServerCentral pluralsight.com SQL Server: Deadlock Analysis and Prevention Twitter #sqlhelp, #sqlblog
Don’t forget to complete an online evaluation on EventBoard! Your evaluation helps organizers build better conferences and helps speakers improve their sessions. Questions? Thank you! Locks, Blocks, and Deadlocks Oh My!