SlideShare a Scribd company logo
1 of 32
Download to read offline
READPAST & Furious:
                        Transactions, Locking and Isolation
   Mark Broadbent
Senior SQL Specialist
          SQLCloud
    SQLCLOUD.CO.UK




                                               November 6-9, Seattle, WA
About

Mark Broadbent
“30 billion times more intelligent than a live mattress”

Email: mark.broadbent@sqlcambs.org.uk

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
TRANSACTIONS
Part I




         November 6-9, Seattle, WA
Transactions can be…
                                                                         Batch Scope
 Auto-Commit
                                                                           UPDATE …
  INSERT …                      MSDTC       MSDTC                          INSERT …
  DELETE …                                                                 DELETE …
                                                                           GO

                                   Distributed
Explicit
 BEGIN TRAN
 …                                                                Binding Token
                                                    spid 115                          spid 162

                  Implicit (evil muthas!)                BEGIN TRAN        BEGIN TRAN
                                                         UPDATE …          INSERT …
              INSERT …
                               BEGIN TRAN
              DELETE …
                                                               Session Binding

and provide Atomicity (all or nothing) …or do they?!
They effect the duration of locks. It can be really confusing!

                                                                                  DBA-309
Transactions are easy-peasy…
      Transaction Name               Transaction Mark


BEGIN 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
    COMMIT
COMMIT



                                                        DBA-309
TRANSACTIONS
Demo




       November 6-9, Seattle, WA
LOCKING
Part II




          November 6-9, Seattle, WA
Locks, the Lock Manager and Locking

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)




                                                                    DBA-309
DBA-309
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
 NL
SCH-S
SCH-M
   S
  U
   X
  IS
 IU
  IX                                                                                                             SQL Server
 SIU
                                                                                                           Lock Compatibility Chart
 SIX
 UIX
 BU
RS-S
RS-U
RI-N
RI-S                                                               No Conflict
RI-U                                                               Conflict
RI-X
RX-S                                                               Illegal
RX-U
RX-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
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
 NL
SCH-S
SCH-M
   S
  U
  X
  IS
 IU
  IX                                                                                                            SQL Server
 SIU
                                                                                                          Lock Compatibility Chart
 SIX
 UIX
 BU
RS-S
RS-U
RI-N
RI-S                                                               Compatible
RI-U                                                               Incompatible
RI-X
RX-S                                                               Illegal
RX-U
RX-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
Intent Locks

                      IX            Table

                           orders
    Data Page
                 IX

                      X     Row


    Free Space
                      X



                                            DBA-309
Intent Locks
                      Lock must wait!



                        IX      S         Table

                                 orders
    Data Page
                 IX

                         X          Row


    Free Space
                         X



                                                  DBA-309
Escalation
Don’t escalate by TF1211
or take artificial IS on table                      Table
Ignore # locks by TF1224         IX
                                      orders
        Data Page
                         IX
                                                        Partition
                                 X     Row


        Free Space
                                 X      ALTER <table> SET
                                        (LOCK_ESCALATION
                                        = auto| table| disable)


                                                            DBA-309
Escalation
Don’t escalate by TF1211
or take artificial IS on table                      Table
Ignore # locks by TF1224         IX
                                  X
                                      orders
        Data Page


                                                        Partition
                                       Row


        Free Space

                                        ALTER <table> SET
                                        (LOCK_ESCALATION
                                        = auto| table| disable)


                                                            DBA-309
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…
                                                           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
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

                  --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
LOCKING
Demo




          November 6-9, Seattle, WA
ISOLATION
Part III




           November 6-9, Seattle, WA
BAD DEPENDENCIES
     Pessimistic
                                    Non-
     Optimistic                                            Update
                     Dirty Read   Repeatable   Phantoms
                                                           Conflict
                                    Read
1
       READ
    UNCOMMITTED      Warning!      Warning!    Warning!      OK

2      READ
     COMMITTED          OK         Warning!    Warning!      OK

    …WITH SNAPSHOT      OK         Warning!    Warning!      OK

3
     REPEATABLE
        READ            OK           OK        Warning!      OK

4
    SERIALIZABLE        OK           OK          OK          OK

5
     SNAPSHOT           OK           OK          OK       Warning!


                                                                  DBA-309
Read Committed Snapshot vs
Snapshot Isolation
       Read Committed Snapshot                    Snapshot

Isolation at the statement level.   Isolation at the transaction level.

Becomes the new default             Must be explicitly SET in each
Isolation.                          connection.

Requires Exclusive Transaction      Requires no active transactions in
Workspace lock (i.e. no other       order to transition.
connections to DB).
                                    Is allowed master, tempdb &
Not allowed on master, tempdb &     msdb.
msdb.
                                    Implements automatic update
Conflict detection not required.    conflict detection.
                                                                     DBA-309
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                                   spid 115
                                                                                5
                                                           COMMIT or
                                                    COMMIT or
     spid 115
                                                           ROLLBACK
                                                    ROLLBACK
1

    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
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
ISOLATION
Demo




        November 6-9, Seattle, WA
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   This is Community




  Learning Center




                                                                                        DBA-309
Thank you
          for attending this session and
          the 2012 PASS Summit in Seattle




DBA-309                         November 6-9, Seattle, WA

More Related Content

More from Mark Broadbent

Enter the Dragon - SQL 2014 on Server Core PASS Summit 2014 Edition
Enter the Dragon -  SQL 2014 on Server Core PASS Summit 2014 EditionEnter the Dragon -  SQL 2014 on Server Core PASS Summit 2014 Edition
Enter the Dragon - SQL 2014 on Server Core PASS Summit 2014 Edition
Mark Broadbent
 
SQL Server Clustering for Dummies
SQL Server Clustering for DummiesSQL Server Clustering for Dummies
SQL Server Clustering for Dummies
Mark Broadbent
 
Orders of-magnitude-scale-out-your-sql-server-data-slideshare
Orders of-magnitude-scale-out-your-sql-server-data-slideshareOrders of-magnitude-scale-out-your-sql-server-data-slideshare
Orders of-magnitude-scale-out-your-sql-server-data-slideshare
Mark Broadbent
 

More from Mark Broadbent (15)

Persistence Is Futile - Implementing Delayed Durability
Persistence Is Futile - Implementing Delayed DurabilityPersistence Is Futile - Implementing Delayed Durability
Persistence Is Futile - Implementing Delayed Durability
 
Lockless in Seattle - Using In-Memory OLTP for Transaction Processing
Lockless in Seattle -  Using In-Memory OLTP for Transaction ProcessingLockless in Seattle -  Using In-Memory OLTP for Transaction Processing
Lockless in Seattle - Using In-Memory OLTP for Transaction Processing
 
Schizophrenic High Availability with SQL and Windows 2016
Schizophrenic High Availability with SQL and Windows 2016Schizophrenic High Availability with SQL and Windows 2016
Schizophrenic High Availability with SQL and Windows 2016
 
SharePoint is from Mars, SQL Server is from Venus (SQL Server for SharePoint ...
SharePoint is from Mars, SQL Server is from Venus (SQL Server for SharePoint ...SharePoint is from Mars, SQL Server is from Venus (SQL Server for SharePoint ...
SharePoint is from Mars, SQL Server is from Venus (SQL Server for SharePoint ...
 
lock, block & two smoking barrels
lock, block & two smoking barrelslock, block & two smoking barrels
lock, block & two smoking barrels
 
Enter the Dragon - SQL 2014 on Server Core PASS Summit 2014 Edition
Enter the Dragon -  SQL 2014 on Server Core PASS Summit 2014 EditionEnter the Dragon -  SQL 2014 on Server Core PASS Summit 2014 Edition
Enter the Dragon - SQL 2014 on Server Core PASS Summit 2014 Edition
 
Being Buck Woody - PASS Summit 2014 Edition
Being Buck Woody - PASS Summit 2014 EditionBeing Buck Woody - PASS Summit 2014 Edition
Being Buck Woody - PASS Summit 2014 Edition
 
Enter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto Edition
Enter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto EditionEnter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto Edition
Enter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto Edition
 
Ye Olde Cluster Curiosity Shoppe
Ye Olde Cluster Curiosity ShoppeYe Olde Cluster Curiosity Shoppe
Ye Olde Cluster Curiosity Shoppe
 
SQL Server AlwaysOn for Dummies SQLSaturday #202 Edition
SQL Server AlwaysOn for Dummies SQLSaturday #202 EditionSQL Server AlwaysOn for Dummies SQLSaturday #202 Edition
SQL Server AlwaysOn for Dummies SQLSaturday #202 Edition
 
Moves Like Jagger - Upgrading to SQL Server 2012 (SQLBits XI Edition)
Moves Like Jagger - Upgrading to SQL Server 2012 (SQLBits XI Edition)Moves Like Jagger - Upgrading to SQL Server 2012 (SQLBits XI Edition)
Moves Like Jagger - Upgrading to SQL Server 2012 (SQLBits XI Edition)
 
PASS 2012 "Moves Like Jagger" - Upgrading to SQL Server 2012
PASS 2012 "Moves Like Jagger" - Upgrading to SQL Server 2012PASS 2012 "Moves Like Jagger" - Upgrading to SQL Server 2012
PASS 2012 "Moves Like Jagger" - Upgrading to SQL Server 2012
 
SQL Server Clustering for Dummies
SQL Server Clustering for DummiesSQL Server Clustering for Dummies
SQL Server Clustering for Dummies
 
Orders of-magnitude-scale-out-your-sql-server-data-slideshare
Orders of-magnitude-scale-out-your-sql-server-data-slideshareOrders of-magnitude-scale-out-your-sql-server-data-slideshare
Orders of-magnitude-scale-out-your-sql-server-data-slideshare
 
Thinking outside the box, learning a little about a lot
Thinking outside the box, learning a little about a lotThinking outside the box, learning a little about a lot
Thinking outside the box, learning a little about a lot
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

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

  • 1. READPAST & Furious: Transactions, Locking and Isolation Mark Broadbent Senior SQL Specialist SQLCloud SQLCLOUD.CO.UK November 6-9, Seattle, WA
  • 2. About Mark Broadbent “30 billion times more intelligent than a live mattress” Email: mark.broadbent@sqlcambs.org.uk 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
  • 3. 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
  • 4. TRANSACTIONS Part I November 6-9, Seattle, WA
  • 5. Transactions can be… Batch Scope Auto-Commit UPDATE … INSERT … MSDTC MSDTC INSERT … DELETE … DELETE … GO Distributed Explicit BEGIN TRAN … Binding Token spid 115 spid 162 Implicit (evil muthas!) BEGIN TRAN BEGIN TRAN UPDATE … INSERT … INSERT … BEGIN TRAN DELETE … Session Binding and 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 Mark BEGIN 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 COMMIT COMMIT DBA-309
  • 7. TRANSACTIONS Demo November 6-9, Seattle, WA
  • 8. LOCKING Part II November 6-9, Seattle, WA
  • 9. Locks, the Lock Manager and Locking 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) 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 NL SCH-S SCH-M S U X IS IU IX SQL Server SIU Lock Compatibility Chart SIX UIX BU RS-S RS-U RI-N RI-S No Conflict RI-U Conflict RI-X RX-S Illegal RX-U RX-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 NL SCH-S SCH-M S U X IS IU IX SQL Server SIU Lock Compatibility Chart SIX UIX BU RS-S RS-U RI-N RI-S Compatible RI-U Incompatible RI-X RX-S Illegal RX-U RX-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. Escalation Don’t escalate by TF1211 or take artificial IS on table Table Ignore # 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. Escalation Don’t escalate by TF1211 or take artificial IS on table Table Ignore # 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” concurrency When 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 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
  • 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. LOCKING Demo November 6-9, Seattle, WA
  • 22. ISOLATION Part III November 6-9, Seattle, WA
  • 23. BAD DEPENDENCIES Pessimistic Non- Optimistic Update Dirty Read Repeatable Phantoms Conflict Read 1 READ UNCOMMITTED Warning! Warning! Warning! OK 2 READ COMMITTED OK Warning! Warning! OK …WITH SNAPSHOT OK Warning! Warning! OK 3 REPEATABLE READ OK OK Warning! OK 4 SERIALIZABLE OK OK OK OK 5 SNAPSHOT OK OK OK Warning! DBA-309
  • 24. Read Committed Snapshot vs Snapshot Isolation Read Committed Snapshot Snapshot Isolation at the statement level. Isolation at the transaction level. Becomes the new default Must be explicitly SET in each Isolation. connection. Requires Exclusive Transaction Requires no active transactions in Workspace lock (i.e. no other order to transition. connections to DB). Is allowed master, tempdb & Not allowed on master, tempdb & msdb. msdb. Implements automatic update Conflict detection not required. conflict detection. DBA-309
  • 25. 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
  • 26. Database Snapshots spid 115 5 COMMIT or COMMIT or spid 115 ROLLBACK ROLLBACK 1 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 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
  • 28. ISOLATION Demo November 6-9, Seattle, WA
  • 29. 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.
  • 30. 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
  • 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 Seattle DBA-309 November 6-9, Seattle, WA