Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
#tarabica15
#tarabica15
Boris Hristov
SQL Server Consultant and Trainer
Welcome to the nightmare of locking,
blocking and isolation le...
#tarabica15
#tarabica15
@BorisHristov
So who am I?
#tarabica15
Agenda…
Locks. What is there for us?
Troubleshooting locking problems
Transaction Isolation Levels
#tarabica15
Locks. What is there for us?
#tarabica15
Methods of Concurrency Control
1. Pessimistic
– SQL Server uses locks, causes blocks and who said deadlocks?
2...
#tarabica15
What Are Locks and what is locking?
Lock – internal memory structure that “tells” us what we all do with the r...
#tarabica15
Intent
Used for: Preventing incompatible
locks
Duration: End of the transaction
Shared (S)
Used for: Reading
D...
#tarabica15
Lock Compatibility
Not all locks are compatible with other locks.
Lock Shared Update Exclusive
Shared (S)
  ...
#tarabica15
Lock Hierarchy
Database
Table
Page
Row
#tarabica15
Let’s update a row!
What do we need?
USE AdventureWorks2012
GO
UPDATE [Person].[Address]
SET AddressLine1=’Bel...
#tarabica15
Methods to View Locking Information
Dynamic
Management
Views
SQL Server
Profiler or
Extended
Events
Performanc...
#tarabica15
Troubleshooting locking problems
#tarabica15
Locking and blocking
Locking and blocking are often confused!
Locking
• The action of taking and potentially h...
#tarabica15
Lock escalation
S
S
X
>= 5000
IX
Header
Row
Row
Row
Row
Row
X
X
X
IX
X
#tarabica15
Controlling Lock Escalation
1. Switch the escalation level (per table)
AUTO – Partition-level escalation if th...
#tarabica15
What Are Deadlocks?
Task A
Task B
Resource 1
Resource 2
Who is victim?
• Cost for Rollback
• Deadlock priority...
#tarabica15
1. Keep the transactions as short as possible
2. No user interactions required in the middle of the transactio...
#tarabica15
DEMO
Monitor for locks with xEvents
Lock escalation – both to table and partition
Deadlock and the SET DEADLOC...
#tarabica15
Transaction isolation levels
#tarabica15
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?)
Transaction 1
Transaction 2
Suggestion: Better offl...
#tarabica15
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
Transaction 1 S(hared) lock
select
No non-repeatable reads pos...
#tarabica15
Transaction 1 S(hared) lock
select
Even phantom records are not possible!
Highest pessimistic level of isolati...
#tarabica15
Based on Row versioning (stored inside tempdb’s version store area)
• No dirty, non-repeatable reads or phanto...
#tarabica15
RCSI – Read Committed Snapshot Isolation Level
• Statement level versioning
• Requires ALTER DATABASE SET READ...
#tarabica15
DEMO
Playing around with the Isolation levels
#tarabica15
Summary
1. Blocking is something normal when it’s not for long
2. There are numerous of ways to monitor lockin...
#tarabica15
Resources
MCM Readiness videos on locking lecture and demo
MCM Readiness video on Snapshot Isolation Level
htt...
#tarabica15
Thank you!
Boris Hristov
brshristov@live.com
www.borishristov.com
Upcoming SlideShare
Loading in …5
×

The Nightmare of Locking, Blocking and Isolation Levels!

1,530 views

Published on

I am sure you all know that troubleshooting problems related to locking and blocking (hey, sometimes there are deadlocks too) can be a real nightmare! In this session, you will be able to see and understand why and how locking actually works, what problems it causes and how can we use isolation levels and various other techniques to resolve them!

Published in: Technology
  • Be the first to comment

The Nightmare of Locking, Blocking and Isolation Levels!

  1. 1. #tarabica15
  2. 2. #tarabica15 Boris Hristov SQL Server Consultant and Trainer Welcome to the nightmare of locking, blocking and isolation levels!
  3. 3. #tarabica15
  4. 4. #tarabica15 @BorisHristov So who am I?
  5. 5. #tarabica15 Agenda… Locks. What is there for us? Troubleshooting locking problems Transaction Isolation Levels
  6. 6. #tarabica15 Locks. What is there for us?
  7. 7. #tarabica15 Methods of Concurrency Control 1. Pessimistic – SQL Server uses locks, causes blocks and who said deadlocks? 2. Optimistic – SQL Server generates versions for everyone, but the updates…
  8. 8. #tarabica15 What Are Locks and what is locking? Lock – internal memory structure that “tells” us what we all do with the resources inside the system Locking – mechanism to protect the resources and guarantee consistent data
  9. 9. #tarabica15 Intent Used for: Preventing incompatible locks Duration: End of the transaction Shared (S) Used for: Reading Duration: Released almost immediately (depends on the isolation level) Update (U) Used for: Preparing to modify Duration: End of the transaction or until converted to exclusive (X) Exclusive (X) Used for: Modifying Duration: End of the transaction Common Lock Types
  10. 10. #tarabica15 Lock Compatibility Not all locks are compatible with other locks. Lock Shared Update Exclusive Shared (S)   X Update (U)  X X Exclusive (X) X X X
  11. 11. #tarabica15 Lock Hierarchy Database Table Page Row
  12. 12. #tarabica15 Let’s update a row! What do we need? USE AdventureWorks2012 GO UPDATE [Person].[Address] SET AddressLine1=’Belgrade, Serbia' WHERE AddressID=2 S IX Header Row Row Row Row Row IX X
  13. 13. #tarabica15 Methods to View Locking Information Dynamic Management Views SQL Server Profiler or Extended Events Performance Monitor or Activity Monitor
  14. 14. #tarabica15 Troubleshooting locking problems
  15. 15. #tarabica15 Locking and blocking Locking and blocking are often confused! Locking • The action of taking and potentially holding locks • Used to implement concurrency control Blocking is result of locking! • One process needs to wait for another process to release locked resources • In a multiuser environment, there is always, always blocking! • Only a problem if it lasts too long
  16. 16. #tarabica15 Lock escalation S S X >= 5000 IX Header Row Row Row Row Row X X X IX X
  17. 17. #tarabica15 Controlling Lock Escalation 1. Switch the escalation level (per table) AUTO – Partition-level escalation if the table is partitioned TABLE – Always table-level escalation DISABLE – Do not escalate until absolutely necessary 2. Just disable it (that’s not Nike’s “Just do it!”) • Trace flag 1211 – disables lock escalation on server level • Trace flag 1224 – disables lock escalation if 40% of the memory used is consumed SELECT lock_escalation_desc FROM sys.tables WHERE name = 'Person.Address' ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE | DISABLE}
  18. 18. #tarabica15 What Are Deadlocks? Task A Task B Resource 1 Resource 2 Who is victim? • Cost for Rollback • Deadlock priority – SET DEADLOCK_PRIORITY
  19. 19. #tarabica15 1. Keep the transactions as short as possible 2. No user interactions required in the middle of the transaction 3. Use indexes (proper ones) 4. Consider a server to offload some of the workloads 5. Choose isolation level Resolving blocking a.k.a live locking
  20. 20. #tarabica15 DEMO Monitor for locks with xEvents Lock escalation – both to table and partition Deadlock and the SET DEADLOCK_PRIORITY option
  21. 21. #tarabica15 Transaction isolation levels
  22. 22. #tarabica15 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?) Transaction 1 Transaction 2 Suggestion: Better offload the reads or go with optimistic level concurrency! Select Update eXclusive lock Read Uncommitted (pessimistic concurrency control) Dirty read
  23. 23. #tarabica15 SET TRANSACTION ISOLATION LEVEL REPEATABLE READ Transaction 1 S(hared) lock select No non-repeatable reads possible (updates during Transaction 1) Phantom records still possible (inserts during Transaction 1) Update Transaction 2 Repeatable Read (pessimistic concurrency control)
  24. 24. #tarabica15 Transaction 1 S(hared) lock select Even phantom records are not possible! Highest pessimistic level of isolation, lowest level of concurrency Insert Transaction 2 Serializable (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  25. 25. #tarabica15 Based on Row versioning (stored inside tempdb’s version store area) • No dirty, non-repeatable reads or phantom records • Every single modification is versioned even if not used • Adds 14 bytes per row Readers do not block writers and writers do not block readers Writers can and will block writers, this can cause conflicts Optimistic Concurrency
  26. 26. #tarabica15 RCSI – Read Committed Snapshot Isolation Level • Statement level versioning • Requires ALTER DATABASE SET READ_COMMITTED_SNAPSHOT ON Snapshot Isolation Level • Transaction level versioning • Requires ALTER DATABASE SET ALLOW_SNAPSHOT_ISOLATION ON • Requires SET TRANSACTION ISOLATION LEVEL SNAPSHOT RCSI and SI (optimistic concurrency control) V1 V2 Transaction 1 Transaction 2 Select in RCSISelect Select in SI
  27. 27. #tarabica15 DEMO Playing around with the Isolation levels
  28. 28. #tarabica15 Summary 1. Blocking is something normal when it’s not for long 2. There are numerous of ways to monitor locking and blocking 3. Be extremely careful for lock escalations 4. Choosing the Isolation level is also a business decision!
  29. 29. #tarabica15 Resources MCM Readiness videos on locking lecture and demo MCM Readiness video on Snapshot Isolation Level http://blogs.msdn.com/b/bartd/archive/tags/sql+locking http://www.sqlskills.com/blogs/paul/category/locking/ Lock hints - http://www.techrepublic.com/article/control- sql-server-locking-with-hints/5181472
  30. 30. #tarabica15 Thank you! Boris Hristov brshristov@live.com www.borishristov.com

×