The topic of SQL Server concurrency is one that many people want to understand really well, but at the same time is one that doesn't get the needed attention for some reason. In addition, isolation levels can play a huge role in both the performance and the scalability of every application and so the proper choice of isolation level is crucial. In this session we are going to go deep into the world of SQL Server isolation levels and see what is the behaviour of each one of them. We will discuss how we should approach the final decision on which level we should go with and how we can actually effectively troubleshoot concurrency issues. Last, but not at least, we will take a look at what is going on behind the scenes when our applications work and what differentiates one isolation level from the other. The session is suitable for both application developers and DBAs who want to advance their knowledge in the unending world of SQL Server concurrency.
6. Pessimistic Concurrency Optimistic Concurrency
Locks
Blocking
Versions
Version Store
Update Conflicts
Locks
Locking
Blocking
Deadlocks
Lock Escalations
7. Common lock types
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
10. Let’s update a row. What do we need?
USE AdventureWorks2012
GO
UPDATE [Person].[Address]
SET AddressLine1=‘London, UK'
WHERE AddressID=2
S
IX
Header
Row
Row
Row
Row
Row
IX
X
A query!
11. Methods to View Locking Information
Dynamic
Management
Views
SQL Server
Profiler or
Extended Events
Performance
monitor or Activity
Monitor
14. 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
(pessimisticconcurrencycontrol)
Dirty read
15. SET TRANSACTION ISOLATION LEVEL READ COMMITTED
Transaction 1 rows
Non-repeatable reads possible (updates during Transaction 1)
Phantom records possible (inserts during Transaction 1)
What else? (that’s a Pluralsight voucher right here!)
Updates and Inserts
Transaction 2
Read Committed
(pessimisticconcurrencycontrol)
S
S
S
S
16. 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
(pessimisticconcurrencycontrol)
17. Transaction 1 S(hared) lock
select
Even phantom records are not possible!
Highest pessimistic level of isolation, lowest level of concurrency
Oh, yeah, TransactionScope in C# and MSDTC transactions default to this level too
Insert
Transaction 2
Serializable
(pessimisticconcurrencycontrol)
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
23. Isolation levels can have dramatic impact on your application
As such, isolation levels must also be a business decision
The behavior of the readers changes in the various levels
Writers, though, will always block. No matter what!
And one more thing – please be careful with ORMs
Summary