READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
-
Upload
mark-broadbent -
Category
Technology
-
view
873 -
download
0
description
Transcript of READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
![Page 1: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/1.jpg)
November 6-9, Seattle, WA
READPAST & Furious: Transactions, Locking and Isolation
Mark Broadbent
Senior SQL Specialist
SQLCloud SQLCLOUD.CO.UK
![Page 2: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/2.jpg)
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
![Page 3: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/3.jpg)
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
![Page 4: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/4.jpg)
November 6-9, Seattle, WA
TRANSACTIONS Part I
![Page 5: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/5.jpg)
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
![Page 6: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/6.jpg)
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
![Page 7: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/7.jpg)
November 6-9, Seattle, WA
TRANSACTIONS Demo
![Page 8: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/8.jpg)
November 6-9, Seattle, WA
LOCKING Part II
![Page 9: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/9.jpg)
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
![Page 10: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/10.jpg)
DBA-309
![Page 11: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/11.jpg)
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
![Page 12: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/12.jpg)
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
![Page 13: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/13.jpg)
Free Space
Data Page
Intent Locks
DBA-309
orders
IX
IX
Table
X
X
Row
![Page 14: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/14.jpg)
Free Space
Data Page
Intent Locks
DBA-309
orders
IX
Lock must wait!
S
IX
Table
X
X
Row
![Page 15: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/15.jpg)
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
![Page 16: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/16.jpg)
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
![Page 17: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/17.jpg)
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
![Page 18: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/18.jpg)
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
![Page 19: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/19.jpg)
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
![Page 20: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/20.jpg)
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
![Page 21: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/21.jpg)
November 6-9, Seattle, WA
LOCKING Demo
![Page 22: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/22.jpg)
November 6-9, Seattle, WA
ISOLATION Part III
![Page 23: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/23.jpg)
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!
![Page 24: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/24.jpg)
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.
![Page 25: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/25.jpg)
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
![Page 26: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/26.jpg)
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
![Page 27: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/27.jpg)
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 28: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/28.jpg)
November 6-9, Seattle, WA
ISOLATION Demo
![Page 29: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/29.jpg)
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 30: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/30.jpg)
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
![Page 31: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/31.jpg)
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
![Page 32: READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition](https://reader034.fdocuments.in/reader034/viewer/2022052601/55905b681a28ab6f2e8b45b9/html5/thumbnails/32.jpg)
November 6-9, Seattle, WA
Thank you for attending this session and
the 2012 PASS Summit in Seattle
DBA-309