SQLintersection Understanding Transaction Isolation Levels Randy Knight [email protected]...

22
SQLintersection Understanding Transaction Isolation Levels Randy Knight [email protected] Wednesday, 3:45-5:00

Transcript of SQLintersection Understanding Transaction Isolation Levels Randy Knight [email protected]...

Page 1: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

SQLintersection

Understanding Transaction Isolation Levels

Randy [email protected]

Wednesday, 3:45-5:00

Page 2: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Randy Knight

Microsoft Certified Master in SQL Server 20+Years of database experience, focusing on SQL Server since 1997. Worked in a variety of settings, including six years as a Database

Architect for match.com Founder, SQL Solutions Group

Page 3: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Overview

ACID Properties ANSI Isolation Levels

READ COMMITTED READ UNCOMMITTED REPEATABLE READ SERIALIZABLE

Versioning SNAPSHOT ISOLATION LEVEL

Dangers of NOLOCK!

Page 4: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

ACIDAll Relational Databases

ATOMIC CONSISTENT ISOLATED DURABLE

Page 5: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Atomicity

Everything succeeds or nothing succeeds Business Transaction Classic Example: Transferring Funds Between Accounts

Page 6: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Consistency

Data cannot be left in an inconsistent state DBMS According to all Business Rules

Business Rule Side is violated all over the place Bugs Incomplete and/or Inadequate Requirements

Page 7: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Isolation

No transaction can interfere with any other transaction Accomplished in one of two ways

Locking Versioning

Also sometimes called multiversion concurrency Snapshot isolation RCSI

Page 8: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Durability

Once a change is committed, it is permanent Power Failure Hardware Failure User or Application Error

Page 9: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Isolation Levels

ANSI Standard Levels Level 0: Read Uncommitted Level 1: Read Committed Level 2: Repeatable Read Level 3: Serializable

Snapshot / Versioning

Page 10: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

READ UNCOMMITTED

WITH (NOLOCK) Can read Data that is “in-flight”

Has been modified but not committed Could still be changing Could be rolled back

“Dirty Reads” i.e. “Who cares if it’s right”

Page 11: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

READ COMMITTED

Default Behavior Only committed data is readable Locks are released as data is read

Only have a lock on a page long enough to read that page Not repeatable

Data may have changed since it was last read Sometimes known as “non-repeatable reads”

Page 12: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

REPEATABLE READ

Holds Shared Locks throughout the life of the transaction Guaranteed consistent data for your entire result set LOTS of blocking Because we don’t release locks, rows can be “repeatably read” Doesn’t guarantee new data won’t enter your set during the transaction

Phantom Rows

Page 13: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

SERIALIZABLE

Lock the entire set Prevents new rows from entering the set Tremendous Amount of Blocking Quite Often Can Be a Table Lock

Filtered Indexes Can Help This

Page 14: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Versioning / Snapshot

Snapshot Isolation Multi Version Concurrency Two Levels

Statement Level (RCSI) Transaction Level

Version Store Used to Maintain Committed Copy Completely separate from writers Does not block No Shared Locks This is the “snapshot”

Page 15: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Statement Level Snapshot (RCSI)

ALTER DATABASE SET READ_COMMITTED_SNAPSHOT ON Becomes the default behavior of the database

Snapshot lives for the duration of a single statement Can get inconsistent data in multi-statement transactions Only guarantees consistency per statement Great for long running statements

Large Data Sets Reports

On or Off

Page 16: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Transaction Level Snapshot

SET ALLOW_SNAPSHOT_ISOLATION ON Turns on versioning Default will still use locking Key word is ALLOW User can request snapshots

SET TRANSATION ISOLATION LEVEL SNAPSHOT Data will be consistent throughout the entire transaction

Page 17: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

Demo

Impact of Isolation Levels

Page 18: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Why not always use Snapshot?

No such thing as a free lunch Version Store is in tempdb Need to tune and monitor tempdb Maybe not “always” but should be used a LOT more than it is now

Eradicate (NOLOCK)!

Page 19: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Recommendations

Start with default behavior Avoid READ UNCOMMITTED like the plague If you think you need (nolock)

Implement snapshot

Page 20: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Review

ACID Properties ANSI Isolation Levels

READ COMMITTED READ UNCOMMITTED REPEATABLE READ SERIALIZABLE

Versioning SNAPSHOT ISOLATION LEVEL

Dangers of NOLOCK!

Page 21: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

© SQLintersection. All rights reserved.http://www.SQLintersection.com

References White Papers

Row Versioning Based Isolation http://msdn.microsoft.com/en-us/library/ms345124(v=sql.90).aspx

Working with tempdb http://technet.microsoft.com/en-us/library/cc966545.aspx

Tempdb monitoring solutions http://www.sqlservercentral.com/articles/tempdb+utilization/65149

/ Twitter

#sqlhelp, #sqlblog @randy_knight @SQLGroup

Page 22: SQLintersection Understanding Transaction Isolation Levels Randy Knight randy@sqlsolutionsgroup.com Wednesday, 3:45-5:00.

Don’t forget to complete an online evaluation on EventBoard!

Your evaluation helps organizers build better conferences and helps speakers improve their sessions.

Questions?

Thank you!

Understanding Transaction Isolation Levels

Randy Knight