READPAST & Furious: Locking

  • View
    701

  • Download
    4

Embed Size (px)

Text of READPAST & Furious: Locking

  • 1. READPAST & Furious: Locking Blocking and Isolation MarkBroadbent sqlcloud.co.ukREADPAST & Furious:Transactions, Locking and IsolationMark BroadbentSQLCloud

2. AgendaTRANSACTIONS Structure, Scope and ManagementLOCKING Compatibility, Multi Granularity and Escalation NOLOCK, READPAST and UPDLOCKISOLATION Snapshot Isolation and Read Committed Snapshot Rolling Database SnapshotsREADPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 3. A bit more about little old me!More than 21 years in IT and more than 15 years using SQL Server.Worked at many large global corporations and SMEs such asMicrosoft, Nokia, Hewlett Packard and Encyclopaedia Britannica.Blog: tenbulls.co.ukCambridgeshire SQL UG Leader sqlcambs.org.uk JOINPresented at SQLBits 7, 8 (and now 9), SQL Rally Orlando US!!!Awarded Microsoft Community Contributor Award 2011MCITP DB Development SQL 2008MCITP DBA SQL 2008/ 2005 and MCDBA SQL 2000Microsoft Certified Application Developer (C# .net)Microsoft Certified Systems Engineer + InternetParticipate on #sqlhelp, MSDN ForumsLinkedIn SQL Server Scripting Group http://linkd.in/pfBkRIREADPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 4. Transactions basicallyProvide Atomicity (all or nothing) or does it!Can be defined as Implicit (evil), Explicit (declarative) orAuto-Commit (default)Effect duration of locksTransactions can have batch scope (MARS), be distributedand even be bound across sessions or servers.Can be really confusing! READPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 5. Transactions are easy-peasyBEGIN TRAN transaction_1 WITH MARK restorepointBEGIN TRAN --do somethingTransaction NameTransaction MarkCOMMIT TRANSAVE TRAN savepointSavepointBEGIN TRAN transaction_3 BEGIN TRAN--do something elseNested Transaction COMMIT IF {something_wrong} THEN ROLLBACK TRAN savepointCOMMITCOMMITREADPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 6. READPAST & Furious: Locking Blocking and Isolation MarkBroadbent sqlcloud.co.ukTransactions Demo 7. Locks, the Lock Manager and LockingLocks are memory structures only and can be converted or escalatedhave a compatibility or incompatibility with other locksare taken depending upon the Isolation levelcan cause blocking or deadlockswill wait if they are incompatibleLock Manager compares locks ONLY on same resource and thats why so intent locks are needed (granular)Lock escalation will occur for performance and memory savingsand escalate straight to Table by default (not partitions)!!!but you can turn off ALL escalation (TF1211), due to # locks(TF1224) or artificially by taking IS on table or by ALTER statement SET (LOCK_ESCALATION = auto|table|disable) READPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 8. READPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 9. NL SCH-S SCH-MS UX ISIU IXSIU SIX UIX BU RS-S RS-U RI-N RI-S RI-U RI-X RX-S RX-U RX-X NLSCH-SSCH-MSUXIS IUIX SQL Server SIU SIX Lock Compatibility Chart UIX BURS-SRS-URI-NRI-SNo ConflictRI-UConflictRI-XRX-SIllegalRX-URX-X NLNo Lock IX Intent ExclusiveRI-S Insert Range-Shared SCH-S Schema Stability Lock SIUShare with Intent UpdateRI-U Insert Range-Update SCH-M Schema Modification LockSIXShare with Intent Exclusive RI-X Insert Range-Exclusive S SharedUIXUpdate with Intent ExclusiveRX-S Exclusive Range-Shared U UpdateBU Bulk Update RX-U Exclusive Range-Update X Exclusive RS-S Shared Range-Shared RX-X Exclusive Range-Exclusive ISIntent Shared RS-U Shared Range-Update IUIntent Update RI-N Insert Range-NullREADPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 10. Lock must wait!Intent Locks Table IX Xorders Data PageIX Free Space X RowREADPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 11. NOLOCK (a Wolf in Sheeps clothing?)Should use READUNCOMMITTED hint instead.Doesnt acquire Shared locks so is able to perform DIRTYREADS, NON-REPEATABLE READS, PHANTOMSand I bet you didnt know it can also return DUPLICATEREADS!It DOES NOT give Oracle style concurrencyWhen can this ever be acceptable? READPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 12. Well since you mention itScalable Shared DatabaseThere isSQL ServerNOLOCKInstance BInstance C Database SnapshotSQL ServerInstance AFilegroup1 Filegroup2Filegroup3ReadonlyDatabase Readonly Filegroup/s When should you use NOLOCK? http://bit.ly/rdGzow READPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 13. READPAST & (your boss) may be Furious! Is an alternative to NOLOCK/ READUNCOMMITTED. Skip over resources holding incompatible (to S) locks. Does not therefore cause dirty or duplicate reads. (yay!) But It still DOES NOT give Oracle style concurrency and can return INCOMPLETE DATA SETS! When can this ever be acceptable? READPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 14. Using READPAST and Table Queues --Get the first record orders--possible and update UPDATE u SET XXFROM 2 U ( SELECT TOP(1) *FROM ordersU WITH (READPAST,UPDLOCK)WHERE 1 ) AS UREADPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 15. READPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.ukLocking Demo 16. Read Committed Snapshot vs Snapshot Isolation SI provides isolation at the transaction level, RCS provides isolation at the statement level SI must be explicitly SET in each connection, for RCS it becomes the new default Enabling SI level requires no active transactions in order to transition. Enabling RCS requires Exclusive Transaction Workspace lock (and therefore no other connections to DB). RCS not allowed on master, tempdb and msdb, SI is allowed SI implements automatic update conflict detectionREADPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 17. Database Snapshots5COMMIT1BEGIN TRAN UPDATE orders 4 SET SELECT * WHERE Buffer Cache FROM orders o 23 CHECKPOINTTransaction LogData FIleREADPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk 18. READPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.ukSnapshots Demo 19. References and ThanksProfessional SQL Server 2008 Internals and Troubleshooting Lockingand Latches Chapter - James Rowland JonesKalen Delaney, SQLPASS Summit 2011 DBA301P - Locking and Blockingand Row Versions, Oh My! DVDJose Barretos Bloghttp://blogs.technet.com/b/josebda/archive/2009/03/19/sql-server-2008-locking.aspxA special thanks to :-Paul White @sql_kiwi http://sqlblog.com/blogs/paul_whiteBenjamin Neverez @BenjaminNeverez http://www.benjaminnevarez.com/ READPAST & Furious: Locking Blocking and Isolation Mark Broadbent sqlcloud.co.uk