Download presentation
Presentation is loading. Please wait.
Published byClyde Jefferson Modified over 8 years ago
1
SQLintersection Locks, Blocks, and Deadlocks Oh My! Randy Knight randy@sqlsolutionsgroup.com Wednesday, 2:15-3:15
2
© SQLintersection. All rights reserved. http://www.SQLintersection.com Randy Knight Microsoft Certified Master in SQL Server 20+Years of database experience, focusing on SQL Server since 1997. Worked in a variety of settings, including six years as a Database Architect for match.com Founder, SQL Solutions Group
3
© SQLintersection. All rights reserved. http://www.SQLintersection.com Overview Review ACID Properties Transaction Isolation Levels Defined Locking Blocking Deadlocks Detection and Prevention Excessive Blocking Deadlocks
4
© SQLintersection. All rights reserved. http://www.SQLintersection.com ACID Atomic Consistent Isolated Durable
5
© SQLintersection. All rights reserved. http://www.SQLintersection.com Isolation Levels ANSI Standard Levels Level 0: Read Uncommitted Level 1: Read Committed Level 2: Repeatable Read Level 3: Serializable Snapshot / Versioning
6
© SQLintersection. All rights reserved. http://www.SQLintersection.com Locking Defined This is how SQL Server handles Isolation Read Locks Shared Concurrent Write Locks Exclusive Isolated Lock Levels Table, Page, Row
7
© SQLintersection. All rights reserved. http://www.SQLintersection.com 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
8
Demo Locks and Blocks
9
© SQLintersection. All rights reserved. http://www.SQLintersection.com Preventing Excessive Blocking Short transactions Appropriate Indexes SNAPSHOT isolation level
10
© SQLintersection. All rights reserved. http://www.SQLintersection.com 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
11
© SQLintersection. All rights reserved. http://www.SQLintersection.com 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
12
© SQLintersection. All rights reserved. http://www.SQLintersection.com 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
13
Demo Deadlocks
14
© SQLintersection. All rights reserved. http://www.SQLintersection.com Common Deadlock Causes Table Order Key Lookup Serializable Isolation Level Parallelism
15
© SQLintersection. All rights reserved. http://www.SQLintersection.com Recommendations Minimize blocking Short transactions SNAPSHOT when appropriate Locking is normal Blocking is normal Excessive Blocking is not Deadlocks are not normal behavior
16
© SQLintersection. All rights reserved. http://www.SQLintersection.com Review Review ACID Properties Transaction Isolation Levels Defined Locking Blocking Deadlocks Detection and Prevention Excessive Blocking Deadlocks
17
© SQLintersection. All rights reserved. http://www.SQLintersection.com References Forums MSDN SQLServerCentral pluralsight.com SQL Server: Deadlock Analysis and Prevention Twitter #sqlhelp, #sqlblog @randy_knight @SQLGroup
18
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!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.