readpast & furious - transactions, locking and isolation. pass summit 2012 edition
DESCRIPTION
Concurrency is probably one of the most important subjects in SQL Server, but one of the least talked about -and one of the most misunderstood. We will attempt to delve into the world of Transactions, Locking and Isolation and reveal some of SQL's dirty little secrets.TRANSCRIPT
November 6-9, Seattle, WA
READPAST & Furious: Transactions, Locking and Isolation
Mark Broadbent
Senior SQL Specialist
SQLCloud SQLCLOUD.CO.UK
About
Mark Broadbent
“30 billion times more intelligent than a live mattress”
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
Agenda
TRANSACTIONS Structure, Scope, Management, the weird and the wonderful!
LOCKING Compatibility, Multi Granularity and Escalation
NOLOCK, READPAST and UPDLOCK
ISOLATION Isolation Levels
Snapshot Isolation and Read Committed Snapshot
Rolling Database Snapshots
AlwaysOn Readable Secondaries
DBA-309
November 6-9, Seattle, WA
TRANSACTIONS Part I
Transactions can be…
DBA-309
Batch Scope
Session Binding
BEGIN TRAN UPDATE …
spid 115
BEGIN TRAN INSERT …
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
BEGIN TRAN WITH MARK BEGIN TRAN --do something COMMIT TRAN SAVE TRAN BEGIN TRAN transaction_3
--do something else COMMIT IF {something_wrong} THEN ROLLBACK TRAN savepoint COMMIT
COMMIT
Transactions are easy-peasy…
DBA-309
‘restorepoint’
Transaction Name Transaction Mark
Savepoint
Nested
Transaction
transaction_1
savepoint
BEGIN TRAN
November 6-9, Seattle, WA
TRANSACTIONS Demo
November 6-9, Seattle, WA
LOCKING Part II
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
DBA-309
DBA-309
DBA-309
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-X IX
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
DBA-309
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-X IX
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
Free Space
Data Page
Intent Locks
DBA-309
orders
IX
IX
Table
X
X
Row
Free Space
Data Page
Intent Locks
DBA-309
orders
IX
Lock must wait!
S
IX
Table
X
X
Row
Free Space
Data Page
Escalation
DBA-309
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
Free Space
Data Page
Escalation
DBA-309
Partition
orders
IX Table 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
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?
DBA-309
Well since you mention it…
DBA-309
Readonly Filegroup/s
Scalable Shared Database
Filegroup1
Filegroup2
Filegroup3
SQL Server Instance B Instance C
Readonly Database
Database Snapshot
SQL Server Instance A
“There is NOLOCK”
Blog post: “When should you use NOLOCK?“ http://bit.ly/rdGzow
AlwaysOn Readable Secondaries
SQL Server Instance B
SQL Server Instance A
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?
DBA-309
Using READPAST and Table Queues
DBA-309
orders
U
X
U
X
1
2
--Get the first record --possible and update
UPDATE u SET … FROM ( SELECT TOP(1) * FROM orders WITH (READPAST ,UPDLOCK) WHERE … ) AS U
November 6-9, Seattle, WA
LOCKING Demo
November 6-9, Seattle, WA
ISOLATION Part III
DBA-309
REPEATABLE
READ
SNAPSHOT
SERIALIZABLE
Pessimistic
Optimistic
READ
COMMITTED
…WITH SNAPSHOT
BAD DEPENDENCIES
OK OK OK Warning!
OK OK OK OK
OK
OK
OK
Warning!
Warning!
Warning!
OK OK
OK
OK Warning!
Warning!
Dirty Read
Non-
Repeatable
Read
Phantoms Update
Conflict
READ
UNCOMMITTED
3
4
5
2
1
OK Warning! Warning! Warning!
Read Committed Snapshot vs Snapshot Isolation
DBA-309
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.
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.
DBA-309
Database Snapshots
DBA-309
COMMIT or ROLLBACK
BEGIN TRAN UPDATE
orders SET … WHERE …
CREATE DATABASE ssdb … AS SNAPSHOT OF db
Automatic CHECKPOINT
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 Pages Recovery
4
spid 115
spid 162
spid 115
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
November 6-9, Seattle, WA
ISOLATION Demo
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.
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/
DBA-309
PASS Resources
Free SQL Server and BI training Free 1-day Training Events Regional Event
Local and Virtual User Groups Free Online Technical Training
Learning Center
This is Community
DBA-309
November 6-9, Seattle, WA
Thank you for attending this session and
the 2012 PASS Summit in Seattle
DBA-309