sqlsaturday #188 lisbon - readpast & furious: transactions, locking and isolation

40
READPAST & Furious: Transactions, Locking and Isolation Mark Broadbent Principal Consultant SQLCloud

Upload: mark-broadbent

Post on 07-Jul-2015

448 views

Category:

Technology


1 download

DESCRIPTION

The weird and the wonderful world of SQL Server Concurrency model looking at transactions, isolation levels and locking.

TRANSCRIPT

Page 1: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

READPAST & Furious:

Transactions, Locking and Isolation

Mark BroadbentPrincipal ConsultantSQLCloud

Page 2: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

About

Mark Broadbent “Possibly the hardest drinking DBA on the planet”

Email: [email protected]

Twitter: retracement

Blog: http://tenbulls.co.uk

Event Lead to the UK’s first ever SQLSaturday (Cambridge) http://www.sqlsaturday.com/events.aspx

Cambridgeshire PASS Chapter UG Leader http://sqlcambs.org.uk

DBA-309

Page 3: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Our Sponsors:

Page 4: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Upcoming SQL Server events:

XXXIII Encontro da Comunidade SQLPort

Data Evento: 23 Abril 2013 - 18:30

Local do Evento: Auditório Microsoft, Parque das Nações, Lisboa

18:30 - Abertura e recepção.

19:10 - "Analyzing Twitter Data" - Niko Neugebauer (SQL Server MVP, Community Evangelist –PASS)

20:15 - Coffee break

20:30 - "First Approach to SQL Server Analysis Services" - João Fialho (Consultor BI Independente)

21:30 - Sorteio de prémios

XXXIV Encontro da Comunidade SQLPort

Data Evento: 7 Maio 2013 - 19:00

Local do Evento: Porto

18:30 - Abertura e recepção.

19:00 - «Apresentação para Developers» - para definir

20:15 - Coffee break

20:30 - «Apresentação para definir» - para definir

21:30 - Sorteio de prémios

Page 5: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Volunteers:

They spend their FREE time to give you this

event. (2 months per person)

Because they are crazy.

Because they want YOU

to learn from the BEST IN THE WORLD.

If you see a guy with “STAFF” on their back –

buy them a beer, they deserve it.

Page 6: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Paulo Matos:

Page 7: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Paulo Borges:

Page 8: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

João Fialho:

Page 9: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Bruno Basto:

Page 10: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Niko Neugebauer:

Page 11: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Agenda

TRANSACTIONSStructure, Scope, Management, the weird and the wonderful!

LOCKINGCompatibility, Multi Granularity and Escalation

NOLOCK, READPAST and UPDLOCK

ISOLATIONIsolation Levels

Snapshot Isolation and Read Committed Snapshot

Rolling Database Snapshots

AlwaysOn Readable Secondaries

Page 12: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

March 16, Lisbon, Portugal

TRANSACTIONSPart I

Page 13: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Transactions can be…

Batch Scope

Session Binding

BEGIN TRANUPDATE …

spid 115

BEGIN TRANINSERT …

spid 162

Distributed

MSDTC MSDTC

UPDATE …INSERT …DELETE …GO

Auto-Commit

INSERT …DELETE …

Explicit

BEGIN TRAN…

Implicit (evil muthas!)

INSERT …DELETE …

BEGIN TRAN

and provide Atomicity (all or nothing) …or do they?!

They effect the duration of locks. It can be really confusing!

Binding Token

Page 14: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

BEGIN TRAN WITH MARKBEGIN TRAN

--do somethingCOMMIT TRANSAVE TRANBEGIN TRAN transaction_3

--do something elseCOMMITIF {something_wrong} THEN ROLLBACK TRAN savepoint

COMMITCOMMIT

Transactions are easy-peasy…

‘restorepoint’

Transaction Name Transaction Mark

Savepoint

Nested

Transaction

transaction_1

savepoint

BEGIN TRAN

Page 15: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

March 16, Lisbon, Portugal

TRANSACTIONSDemo

Page 16: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

March 16, Lisbon, Portugal

LOCKINGPart II

Page 17: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Locks are ONLY memory structures

• They can be converted or escalated

• Lock escalation will occur for performance and memory savings

• Are compatible OR incompatible with other locks

• Will wait if they are incompatible

• Are taken depending upon the Isolation level

• Can cause blocking or deadlocks

Lock Manager compares locks ONLY on same resource

AND lock partition

• So that’s why intent locks are needed (for granularity)

Locks, the Lock Manager and Locking

Page 18: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation
Page 19: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

X

IS

IU

SIU

SIX

UIX

BU

RS-S

RS-U

RI-N

RI-S

RI-U

RI-X

RX-U

RX-X

IX

RX-S

U

S

SCH-M

SCH-S

NL

NL SCH-S SCH-M S U X IS IU SIU SIX UIX BU RS-S RS-U RI-N RI-S RI-U RI-X RX-S RX-U RX-XIX

No Conflict

Conflict

Illegal

NL

SCH-S

SCH-M

S

U

X

IS

IU

No Lock

Schema Stability Lock

Schema Modification Lock

Shared

Update

Exclusive

Intent Shared

Intent Update

IX

SIU

SIX

UIX

BU

RS-S

RS-U

RI-N

Intent Exclusive

Share with Intent Update

Share with Intent Exclusive

Update with Intent Exclusive

Bulk Update

Shared Range-Shared

Shared Range-Update

Insert Range-Null

RI-S

RI-U

RI-X

RX-S

RX-U

RX-X

Insert Range-Shared

Insert Range-Update

Insert Range-Exclusive

Exclusive Range-Shared

Exclusive Range-Update

Exclusive Range-Exclusive

SQL Server Lock Compatibility Chart

Page 20: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

READPAST & Furious: Locking Blocking and Isolation ·

Mark Broadbent · sqlcloud.co.uk

X

IS

IU

SIU

SIX

UIX

BU

RS-S

RS-U

RI-N

RI-S

RI-U

RI-X

RX-U

RX-X

IX

RX-S

U

S

SCH-M

SCH-S

NL

NL SCH-S SCH-M S U X IS IU SIU SIX UIX BU RS-S RS-U RI-N RI-S RI-U RI-X RX-S RX-U RX-XIX

Compatible

Incompatible

Illegal

NL

SCH-S

SCH-M

S

U

X

IS

IU

No Lock

Schema Stability Lock

Schema Modification Lock

Shared

Update

Exclusive

Intent Shared

Intent Update

IX

SIU

SIX

UIX

BU

RS-S

RS-U

RI-N

Intent Exclusive

Share with Intent Update

Share with Intent Exclusive

Update with Intent Exclusive

Bulk Update

Shared Range-Shared

Shared Range-Update

Insert Range-Null

RI-S

RI-U

RI-X

RX-S

RX-U

RX-X

Insert Range-Shared

Insert Range-Update

Insert Range-Exclusive

Exclusive Range-Shared

Exclusive Range-Update

Exclusive Range-Exclusive

http://bit.ly/sc9f7l

SQL Server Lock Compatibility Chart

Page 21: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Free Space

Data Page

Intent Locks

orders

IX

IX

Table

X

X

Row

Page 22: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Free Space

Data Page

Intent Locks

orders

IX

Lock must wait!

S

IX

Table

X

X

Row

Page 23: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Free Space

Data Page

Escalation

IX

Partition

orders

IX Table

X

X

IX

Row

ALTER <table> SET

(LOCK_ESCALATION

= auto| table| disable)

Don’t escalate by TF1211

or take artificial IS on table

Ignore # locks by TF1224 X

Page 24: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

NOLOCK (a Wolf in Sheep’s Clothing?)

Should use READUNCOMMITTED hint instead –but ONLY in very

specialist situations.

Doesn’t acquire Shared locks so is able to perform DIRTY READS,

NON-REPEATABLE READS, PHANTOMS

…it can also return DUPLICATE READS!

It DOES NOT give “Oracle style” concurrency

When can this ever be acceptable?

Page 25: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Well since you mention it…

Readonly Filegroup/s

Scalable Shared Database

Filegroup1

Filegroup2

Filegroup3

SQL ServerInstance BInstance C

ReadonlyDatabase

DatabaseSnapshot

SQL ServerInstance A

“There is NOLOCK”

Blog post: “When should you use NOLOCK?“ http://bit.ly/rdGzow

AlwaysOn Readable Secondaries

SQL ServerInstance B

SQL ServerInstance A

Page 26: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

READPAST & (your boss) may be Furious!

Is an alternative to NOLOCK/ READUNCOMMITTED.

Skips over resources holding incompatible (to S) locks.

…does not therefore cause dirty or duplicate reads. (yay!)

DOES NOT give “Oracle style” concurrency

…and can return INCOMPLETE DATA SETS!

When can this ever be acceptable?

Page 27: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Using READPAST and Table Queues

orders

U

X

U

X

1

2

--Get the first record--possible and update

UPDATE u SET …FROM(SELECT TOP(1) *

FROM ordersWITH (READPAST

,UPDLOCK)WHERE …

) AS U

Page 28: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

March 16, Lisbon, Portugal

LOCKINGDemo

Page 29: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

March 16, Lisbon, Portugal

ISOLATIONPart III

Page 30: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

REPEATABLE

READ

SNAPSHOT

SERIALIZABLE

Pessimistic

Optimistic

READ

COMMITTED

…WITH SNAPSHOT

BAD DEPENDENCIES

OK OK OK Warning!

OKOKOK OK

OK

OK

OK

Warning!

Warning!

Warning!

OK OK

OK

OK Warning!

Warning!

Dirty Read

Non-

Repeatable

Read

PhantomsUpdate

Conflict

READ

UNCOMMITTED

3

4

5

2

1

OKWarning!Warning!Warning!

Page 31: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Read Committed Snapshot vsSnapshot Isolation

Read Committed Snapshot Snapshot

Isolation at the statement level.

Becomes the new default

Isolation.

Requires Exclusive Transaction

Workspace lock (i.e. no other

connections to DB).

Not allowed on master, tempdb &

msdb.

Conflict detection not required.

Isolation at the transaction level.

Must be explicitly SET in each

connection.

Requires no active transactions in

order to transition.

Is allowed master, tempdb &

msdb.

Implements automatic update

conflict detection.

Page 32: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Optimistic Concurrency #FAIL

Snapshot Isolation introduces update conflicts!

TempDB overhead for version store.

Writers STILL block writers and their escalation can still cause a

concurrency problem!

RCSI (and SI) are not a silver bullet for Concurrency.

Page 33: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Database Snapshots

DBA-309

COMMIT or ROLLBACK

BEGIN TRANUPDATE

ordersSET …WHERE …

CREATE DATABASE ssdb…AS SNAPSHOT OF db

AutomaticCHECKPOINT

3

2

1

Blog post: “Lifting the lid on database snapshots“ http://bit.ly/NS64qA

COMMIT or ROLLBACK

5

Datafile Transaction Log

Buffer Cache

Dirty PagesRecovery

4

spid 115

spid 162

spid 115

Page 34: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Readable Secondaries

Great for reporting, but…

Cause a 14 byte pointer to be added to Primary database –since

Secondary uses SNAPSHOT isolation under the covers.

Beware sizing considerations and page splitting.

Data latency from redo thread can cause unexpected results if your

application does not expect it.

More administration overhead and knowledge.

You cannot write to them (name is the giveaway!).

DBA-309

Page 35: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

March 16, Lisbon, Portugal

ISOLATIONDemo

Page 36: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

Summary

Concurrency is a very complicated topic, ensure you understand the

effects of running different transactional modes and Isolation levels.

Understand the duration and compatibility of common locks and how

they are effected.

Isolation can be achieved through scalability mechanisms and

techniques, your ultimate aim is to separate writers from readers,

readers from writers and writers from writers. In short maximize lock

compatibility.

Page 37: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

References and Thanks

Kalen Delaney, SQLPASS Summit 2011

DBA301P - Locking and Blocking

and Row Versions, Oh My! – DVD

Jose Barreto's Blog http://bit.ly/nPJGjv

Paul White http://sqlblog.com/blogs/paul_white

Benjamin Neverez http://www.benjaminnevarez.com/

Page 39: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

March 16, Lisbon, Portugal

Thank youfor attending this session and

SQLSaturday #188 Lisbon

Page 40: SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

www.passbaconference.com

#passbac

I want to

upgrade

my skills

I want to take

the path from

DBA to Data

Analytics Guru

I want to give my career a

competitive edge

I want to expand

my network in the

business analytics

industry

What Are Your Professional Development

Goals?

Sound familiar? Get a head start and join us today at:

Enjoy $150 off registration: use code CHM2D