24
DEEP INTO ISOLATION LEVELS Boris Hristov SQLBits, Superheroes, 2015

Deep Into Isolation Levels

Embed Size (px)

Citation preview

Page 1: Deep Into Isolation Levels

DEEP INTO

ISOLATION LEVELS

Boris HristovSQLBits, Superheroes, 2015

Page 2: Deep Into Isolation Levels

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

Page 3: Deep Into Isolation Levels

So who am I?

@BorisHristov

Page 4: Deep Into Isolation Levels

Isolation Levels time

com

ple

xity

Fundamentals

?

Session’s Timeline

Page 5: Deep Into Isolation Levels

The Fundamentals

Page 6: Deep Into Isolation Levels

Pessimistic Concurrency Optimistic Concurrency

Locks

Blocking

Versions

Version Store

Update Conflicts

Locks

Locking

Blocking

Deadlocks

Lock Escalations

Page 7: Deep Into Isolation Levels

Common lock types

Intent

Used for: Preventing incompatible locksDuration: End of the transaction

Shared (S)

Used for: ReadingDuration: Released almost immediately

(depends on the isolation level)

Update (U)

Used for: Preparing to modifyDuration: End of the transaction or until

converted to exclusive (X)

Exclusive (X)

Used for: ModifyingDuration: End of the transaction

Page 8: Deep Into Isolation Levels

Lock Compatibility

Lock Shared Update Exclusive

Shared (S) X

Update (U) X X

Exclusive (X) X X X

Page 9: Deep Into Isolation Levels

Lock Hierarchy

Database

Table

Page

Row

Page 10: Deep Into Isolation Levels

Let’s update a row. What do we need?

USE AdventureWorks2012GOUPDATE [Person].[Address]SET AddressLine1=‘London, UK'WHERE AddressID=2

S

IX

Header

Row

Row

Row

Row

Row

IX

X

A query!

Page 11: Deep Into Isolation Levels

Methods to View Locking Information

Dynamic Management

Views

SQL Server Profiler or

Extended Events

Performance monitor or Activity

Monitor

Page 12: Deep Into Isolation Levels

DEMO Locking and Locking Hierarchies

Page 13: Deep Into Isolation Levels

Transaction isolation levels(pessimistic concurrency)

Page 14: Deep Into Isolation Levels

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

Page 15: Deep Into Isolation Levels

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(pessimistic concurrency control)

S

S

S

S

Page 16: Deep Into Isolation Levels

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)

Page 17: Deep Into Isolation Levels

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(pessimistic concurrency control)

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

Page 18: Deep Into Isolation Levels

DEMO Playing with Isolation levels (Pessimistic Concurrency)

Page 19: Deep Into Isolation Levels

Transaction isolation levels(optimistic concurrency)

Page 20: Deep Into Isolation Levels

Read Committed and Snapshot Isolation levels

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

V1 V2Transaction 1

Transaction 2

Select in RCSISelect

Select in SI

Page 21: Deep Into Isolation Levels

DEMO Playing with Isolation levels (Optimistic Concurrency)

… and Azure DB too

Page 22: Deep Into Isolation Levels

Just before we end…

Page 23: Deep Into Isolation Levels

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