Your SlideShare is downloading. ×
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

READPAST & Furious - Transactions, Locking and Isolation. PASS Summit 2012 Edition

809

Published on

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, …

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.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
809
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. READPAST & Furious: Transactions, Locking and Isolation Mark BroadbentSenior SQL Specialist SQLCloud SQLCLOUD.CO.UK November 6-9, Seattle, WA
  • 2. AboutMark Broadbent“30 billion times more intelligent than a live mattress”Email: mark.broadbent@sqlcambs.org.ukTwitter: retracementBlog: http://tenbulls.co.ukEvent Lead to the UK’s first ever SQLSaturday (Cambridge)http://www.sqlsaturday.com/events.aspxCambridgeshire PASS Chapter UG Leader http://sqlcambs.org.uk DBA-309
  • 3. AgendaTRANSACTIONSStructure, Scope, Management, the weird and the wonderful!LOCKINGCompatibility, Multi Granularity and EscalationNOLOCK, READPAST and UPDLOCKISOLATIONIsolation LevelsSnapshot Isolation and Read Committed SnapshotRolling Database SnapshotsAlwaysOn Readable Secondaries DBA-309
  • 4. TRANSACTIONSPart I November 6-9, Seattle, WA
  • 5. Transactions can be… Batch Scope Auto-Commit UPDATE … INSERT … MSDTC MSDTC INSERT … DELETE … DELETE … GO DistributedExplicit BEGIN TRAN … Binding Token spid 115 spid 162 Implicit (evil muthas!) BEGIN TRAN BEGIN TRAN UPDATE … INSERT … INSERT … BEGIN TRAN DELETE … Session Bindingand provide Atomicity (all or nothing) …or do they?!They effect the duration of locks. It can be really confusing! DBA-309
  • 6. Transactions are easy-peasy… Transaction Name Transaction MarkBEGIN TRAN transaction_1 WITH MARK ‘restorepoint’ BEGIN TRAN --do something COMMIT TRAN Savepoint SAVE TRAN savepoint BEGIN TRAN transaction_3 BEGIN TRAN Nested --do something else Transaction COMMIT IF {something_wrong} THEN ROLLBACK TRAN savepoint COMMITCOMMIT DBA-309
  • 7. TRANSACTIONSDemo November 6-9, Seattle, WA
  • 8. LOCKINGPart II November 6-9, Seattle, WA
  • 9. Locks, the Lock Manager and LockingLocks 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 deadlocksLock Manager compares locks ONLY on same resourceAND lock partition• So that’s why intent locks are needed (for granularity) DBA-309
  • 10. DBA-309
  • 11. NL SCH-S SCH-M S U X IS IU IX SIU SIX UIX BU RS-S RS-U RI-N RI-S RI-U RI-X RX-S RX-U RX-X NLSCH-SSCH-M S U X IS IU IX SQL Server SIU Lock Compatibility Chart SIX UIX BURS-SRS-URI-NRI-S No ConflictRI-U ConflictRI-XRX-S IllegalRX-URX-X NL No Lock IX Intent Exclusive RI-S Insert Range-Shared SCH-S Schema Stability Lock SIU Share with Intent Update RI-U Insert Range-Update SCH-M Schema Modification Lock SIX Share with Intent Exclusive RI-X Insert Range-Exclusive S Shared UIX Update with Intent Exclusive RX-S Exclusive Range-Shared U Update BU Bulk Update RX-U Exclusive Range-Update X Exclusive RS-S Shared Range-Shared RX-X Exclusive Range-Exclusive IS Intent Shared RS-U Shared Range-Update IU Intent Update RI-N Insert Range-Null DBA-309
  • 12. READPAST & Furious: Locking Blocking and Isolation · Mark Broadbent · sqlcloud.co.uk NL SCH-S SCH-M S U X IS IU IX SIU SIX UIX BU RS-S RS-U RI-N RI-S RI-U RI-X RX-S RX-U RX-X NLSCH-SSCH-M S U X IS IU IX SQL Server SIU Lock Compatibility Chart SIX UIX BURS-SRS-URI-NRI-S CompatibleRI-U IncompatibleRI-XRX-S IllegalRX-URX-X NL No Lock IX Intent Exclusive RI-S Insert Range-Shared SCH-S Schema Stability Lock SIU Share with Intent Update RI-U Insert Range-Update SCH-M Schema Modification Lock SIX Share with Intent Exclusive RI-X Insert Range-Exclusive S Shared UIX Update with Intent Exclusive RX-S Exclusive Range-Shared U Update BU Bulk Update RX-U Exclusive Range-Update X Exclusive RS-S Shared Range-Shared RX-X Exclusive Range-Exclusive IS Intent Shared RS-U Shared Range-Update IU Intent Update RI-N Insert Range-Null http://bit.ly/sc9f7l DBA-309
  • 13. Intent Locks IX Table orders Data Page IX X Row Free Space X DBA-309
  • 14. Intent Locks Lock must wait! IX S Table orders Data Page IX X Row Free Space X DBA-309
  • 15. EscalationDon’t escalate by TF1211or take artificial IS on table TableIgnore # locks by TF1224 IX orders Data Page IX Partition X Row Free Space X ALTER <table> SET (LOCK_ESCALATION = auto| table| disable) DBA-309
  • 16. EscalationDon’t escalate by TF1211or take artificial IS on table TableIgnore # locks by TF1224 IX X orders Data Page Partition Row Free Space ALTER <table> SET (LOCK_ESCALATION = auto| table| disable) DBA-309
  • 17. 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” concurrencyWhen can this ever be acceptable? DBA-309
  • 18. Well since you mention it… AlwaysOn Readable Secondaries Scalable Shared Database “There is SQL Server NOLOCK” Instance A SQL Server Instance B Instance C SQL Server Instance B SQL Server Instance A Filegroup1 Filegroup2 Filegroup3 Readonly Database Database Snapshot Readonly Filegroup/s Blog post: “When should you use NOLOCK?“ http://bit.ly/rdGzow DBA-309
  • 19. READPAST & (your boss) may beFurious!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
  • 20. Using READPAST and Table Queues --Get the first record orders --possible and update UPDATE u SET … X X FROM 2 U ( SELECT TOP(1) * FROM orders U WITH (READPAST ,UPDLOCK) WHERE … 1 ) AS U DBA-309
  • 21. LOCKINGDemo November 6-9, Seattle, WA
  • 22. ISOLATIONPart III November 6-9, Seattle, WA
  • 23. BAD DEPENDENCIES Pessimistic Non- Optimistic Update Dirty Read Repeatable Phantoms Conflict Read1 READ UNCOMMITTED Warning! Warning! Warning! OK2 READ COMMITTED OK Warning! Warning! OK …WITH SNAPSHOT OK Warning! Warning! OK3 REPEATABLE READ OK OK Warning! OK4 SERIALIZABLE OK OK OK OK5 SNAPSHOT OK OK OK Warning! DBA-309
  • 24. Read Committed Snapshot vsSnapshot Isolation Read Committed Snapshot SnapshotIsolation at the statement level. Isolation at the transaction level.Becomes the new default Must be explicitly SET in eachIsolation. connection.Requires Exclusive Transaction Requires no active transactions inWorkspace lock (i.e. no other order to transition.connections to DB). Is allowed master, tempdb &Not allowed on master, tempdb & msdb.msdb. Implements automatic updateConflict detection not required. conflict detection. DBA-309
  • 25. Optimistic Concurrency #FAILSnapshot Isolation introduces update conflicts!TempDB overhead for version store.Writers STILL block writers and their escalation can still cause aconcurrency problem!RCSI (and SI) are not a silver bullet for Concurrency. DBA-309
  • 26. Database Snapshots spid 115 5 COMMIT or COMMIT or spid 115 ROLLBACK ROLLBACK1 BEGIN TRAN UPDATE spid 162 2 orders CREATE DATABASE ssdb SET … … WHERE … Buffer Cache AS SNAPSHOT OF db 3 4 Dirty Pages Automatic Recovery CHECKPOINT Datafile Transaction Log Blog post: “Lifting the lid on database snapshots“ http://bit.ly/NS64qA DBA-309
  • 27. Readable SecondariesGreat for reporting, but…Cause a 14 byte pointer to be added to Primary database –sinceSecondary uses SNAPSHOT isolation under the covers.Beware sizing considerations and page splitting.Data latency from redo thread can cause unexpected results if yourapplication does not expect it.More administration overhead and knowledge.You cannot write to them (name is the giveaway!). DBA-309
  • 28. ISOLATIONDemo November 6-9, Seattle, WA
  • 29. SummaryConcurrency is a very complicated topic, ensure you understand theeffects of running different transactional modes and Isolation levels.Understand the duration and compatibility of common locks and howthey are effected.Isolation can be achieved through scalability mechanisms andtechniques, your ultimate aim is to separate writers from readers,readers from writers and writers from writers. In short maximize lockcompatibility.
  • 30. References and ThanksKalen Delaney, SQLPASS Summit 2011DBA301P - Locking and Blockingand Row Versions, Oh My! – DVDJose Barretos Blog http://bit.ly/nPJGjvPaul White http://sqlblog.com/blogs/paul_whiteBenjamin Neverez http://www.benjaminnevarez.com/ DBA-309
  • 31. PASS Resources Free SQL Server and BI training Free 1-day Training Events Regional Event Local and Virtual User Groups Free Online Technical Training This is Community Learning Center DBA-309
  • 32. Thank you for attending this session and the 2012 PASS Summit in SeattleDBA-309 November 6-9, Seattle, WA

×