Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications DEEP INTO ISOLATION LEVELS Boris Hristov SQLUG Sweden, September 2015
That’s not a Hekaton Talk! (In-Memory OLTP)
So who’s Boris? @BorisHristov
Module 4: Managing Security Course 2786B Session’s Timeline Module 4: Managing Security complexity ? Fundamentals Isolation Levels time
Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications The Fundamentals
Module 4: Managing Security Course 2786B Pessimistic Concurrency Optimistic Concurrency Module 4: Managing Security Locks Locking Blocking Deadlocks Lock Escalations Versions Version store Locks Blocking Update Conflicts
Module 4: Managing Security Course 2786B Common lock types Module 4: Managing Security 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) Intent Used for: Preventing incompatible locks Duration: End of the transaction Exclusive (X) Used for: Modifying Duration: End of the transaction
Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A Lock Compatibility Module 13: Creating Highly Concurrent SQL Server 2012 Applications Lock Shared Update Exclusive Shared (S) X Update (U) Exclusive (X)
Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications Lock Hierarchy Database Table Page Row
Let’s update a row. What do we need? Course 10776A Let’s update a row. What do we need? Module 13: Creating Highly Concurrent SQL Server 2012 Applications S A query! IX USE AdventureWorks2012 GO UPDATE [Person].[Address] SET AddressLine1=‘Stockholm, Sweden' WHERE AddressID=2 IX Header Row X
Methods to View Locking Information Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications Methods to View Locking Information Dynamic Management Views SQL Server Profiler or Extended Events Performance monitor or Activity Monitor
Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications DEMO Locking and Locking Hierarchies
Transaction isolation levels (pessimistic concurrency) Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications Transaction isolation levels (pessimistic concurrency)
Module 4: Managing Security Course 2786B Module 4: Managing Security Read Uncommitted (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?) Transaction 1 eXclusive lock Update Select Transaction 2 Dirty read Suggestion: Better offload the reads or go with optimistic level concurrency!
Module 4: Managing Security Course 2786B Module 4: Managing Security Read Committed (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL READ COMMITTED Transaction 1 rows S S Transaction 2 S S Updates and Inserts Non-repeatable reads possible (updates during Transaction 1) Phantom records possible (inserts during Transaction 1) What else? (that’s a Pluralsight voucher right here!)
Module 4: Managing Security Course 2786B Module 4: Managing Security Repeatable Read (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL REPEATABLE READ Transaction 1 S(hared) lock select Transaction 2 Update No non-repeatable reads possible (updates during Transaction 1) Phantom records still possible (inserts during Transaction 1)
Module 4: Managing Security Course 2786B Serializable (pessimistic concurrency control) Module 4: Managing Security SET TRANSACTION ISOLATION LEVEL SERIALIZABLE S(hared) lock Transaction 1 select Transaction 2 Insert Even phantom records are not possible! Highest pessimistic level of isolation, lowest level of concurrency Oh, TransactionScope in C# and MSDTC transactions default to this level too
Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications DEMO Playing with Isolation levels (Pessimistic Concurrency)
Transaction isolation levels (optimistic concurrency) Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications Transaction isolation levels (optimistic concurrency) -14 bytes – XSN transaction + pointer
Read Committed and Snapshot Isolation levels Course 2786B Read Committed and Snapshot Isolation levels Module 4: Managing Security Transaction 1 V1 V2 Select Select in RCSI Transaction 2 Select in SI 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
Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications DEMO Playing with Isolation levels (Optimistic Concurrency) And Azure DB too
Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications Just before we end…
Module 4: Managing Security Course 2786B Module 4: Managing Security Summary Isolation levels can have dramatic impact on your application They must also be a business decision Only the behavior of the readers changes in the various levels And one more thing – please be careful with ORMs
Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications Thanks a ton, SQLug.se! Contacts: brshristov@live.com www.borishristov.com @BorisHristov