Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 13: Creating Highly Concurrent SQL Server 2012 Applications

Similar presentations


Presentation on theme: "Module 13: Creating Highly Concurrent SQL Server 2012 Applications"— Presentation transcript:

1 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

2 That’s not a Hekaton Talk!
(In-Memory OLTP)

3 So who’s Boris? @BorisHristov

4 Module 4: Managing Security
Course 2786B Session’s Timeline Module 4: Managing Security complexity ? Fundamentals Isolation Levels time

5 Module 13: Creating Highly Concurrent SQL Server 2012 Applications
Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications The Fundamentals

6 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

7 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

8 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)

9 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

10 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

11 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

12 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

13 Transaction isolation levels (pessimistic concurrency)
Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications Transaction isolation levels (pessimistic concurrency)

14 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!

15 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!)

16 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)

17 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

18 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)

19 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

20 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

21 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

22 Module 13: Creating Highly Concurrent SQL Server 2012 Applications
Course 10776A Module 13: Creating Highly Concurrent SQL Server 2012 Applications Just before we end…

23 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 

24 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: @BorisHristov


Download ppt "Module 13: Creating Highly Concurrent SQL Server 2012 Applications"

Similar presentations


Ads by Google