SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
READPAST & Furious:
Transactions, Locking and Isolation
Mark Broadbent
Principal Consultant
SQLCloud
About

Mark Broadbent
“Possibly the hardest drinking DBA on the planet”

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
Our Sponsors:
Upcoming SQL Server events:
XXXIII Encontro da Comunidade SQLPort
Data Evento: 23 Abril 2013 - 18:30
Local do Evento: Auditório Microsoft, Parque das Nações, Lisboa

18:30 - Abertura e recepção.
19:10 - "Analyzing Twitter Data" - Niko Neugebauer (SQL Server MVP, Community Evangelist –
PASS)
20:15 - Coffee break
20:30 - "First Approach to SQL Server Analysis Services" - João Fialho (Consultor BI Independente)
21:30 - Sorteio de prémios


XXXIV Encontro da Comunidade SQLPort
Data Evento: 7 Maio 2013 - 19:00
Local do Evento: Porto

18:30 - Abertura e recepção.
19:00 - «Apresentação para Developers» - para definir
20:15 - Coffee break
20:30 - «Apresentação para definir» - para definir
21:30 - Sorteio de prémios
Volunteers:
 They spend their FREE time to give you this
  event. (2 months per person)
 Because they are crazy.
 Because they want YOU
      to learn from the BEST IN THE WORLD.
 If you see a guy with “STAFF” on their back –
  buy them a beer, they deserve it.
Paulo Matos:
Paulo Borges:
João Fialho:
Bruno Basto:
Niko Neugebauer:
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
TRANSACTIONS
Part I




         March 16, Lisbon, Portugal
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!
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
TRANSACTIONS
Demo




       March 16, Lisbon, Portugal
LOCKING
Part II




          March 16, Lisbon, Portugal
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)
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
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
Intent Locks

                      IX            Table

                           orders
    Data Page
                 IX

                      X     Row


    Free Space
                      X
Intent Locks
                      Lock must wait!



                        IX      S         Table

                                 orders
    Data Page
                 IX

                         X          Row


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


        Free Space
                                 X      ALTER <table> SET
                                        (LOCK_ESCALATION
                                        = auto| table| disable)
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?
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
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?
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
LOCKING
Demo




          March 16, Lisbon, Portugal
ISOLATION
Part III




           March 16, Lisbon, Portugal
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!
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.
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.
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




        March 16, Lisbon, Portugal
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/
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
Thank you
for attending this session and
SQLSaturday #188 Lisbon




                       March 16, Lisbon, Portugal
What Are Your Professional Development
Goals?
  I want to take        I want to            I want to         I want to expand
  the path from         upgrade               give my          my network in the
   DBA to Data                                career a         business analytics
  Analytics Guru
                         my skills          competitive             industry
                                                edge


Sound familiar? Get a head start and join us today at:



                                                          www.passbaconference.com
                                                                      #passbac




Enjoy $150 off registration: use code CHM2D

Weitere ähnliche Inhalte

Mehr von Mark Broadbent

lock, block & two smoking barrels
lock, block & two smoking barrelslock, block & two smoking barrels
lock, block & two smoking barrelsMark Broadbent
 
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 EditionMark Broadbent
 
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 EditionMark Broadbent
 
Ye Olde Cluster Curiosity Shoppe
Ye Olde Cluster Curiosity ShoppeYe Olde Cluster Curiosity Shoppe
Ye Olde Cluster Curiosity ShoppeMark Broadbent
 
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)Mark Broadbent
 
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 2012Mark Broadbent
 
SQL Server Clustering for Dummies
SQL Server Clustering for DummiesSQL Server Clustering for Dummies
SQL Server Clustering for DummiesMark 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-slideshareMark Broadbent
 
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 lotMark Broadbent
 

Mehr von Mark Broadbent (9)

lock, block & two smoking barrels
lock, block & two smoking barrelslock, block & two smoking barrels
lock, block & two smoking barrels
 
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
 
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
 

Kürzlich hochgeladen

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 

Kürzlich hochgeladen (20)

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 

SQLSaturday #188 Lisbon - READPAST & Furious: Transactions, Locking and Isolation

  • 1. READPAST & Furious: Transactions, Locking and Isolation Mark Broadbent Principal Consultant SQLCloud
  • 2. About Mark Broadbent “Possibly the hardest drinking DBA on the planet” 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
  • 4. Upcoming SQL Server events: XXXIII Encontro da Comunidade SQLPort Data Evento: 23 Abril 2013 - 18:30 Local do Evento: Auditório Microsoft, Parque das Nações, Lisboa 18:30 - Abertura e recepção. 19:10 - "Analyzing Twitter Data" - Niko Neugebauer (SQL Server MVP, Community Evangelist – PASS) 20:15 - Coffee break 20:30 - "First Approach to SQL Server Analysis Services" - João Fialho (Consultor BI Independente) 21:30 - Sorteio de prémios XXXIV Encontro da Comunidade SQLPort Data Evento: 7 Maio 2013 - 19:00 Local do Evento: Porto 18:30 - Abertura e recepção. 19:00 - «Apresentação para Developers» - para definir 20:15 - Coffee break 20:30 - «Apresentação para definir» - para definir 21:30 - Sorteio de prémios
  • 5. Volunteers:  They spend their FREE time to give you this event. (2 months per person)  Because they are crazy.  Because they want YOU to learn from the BEST IN THE WORLD.  If you see a guy with “STAFF” on their back – buy them a beer, they deserve it.
  • 11. 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
  • 12. TRANSACTIONS Part I March 16, Lisbon, Portugal
  • 13. 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!
  • 14. 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
  • 15. TRANSACTIONS Demo March 16, Lisbon, Portugal
  • 16. LOCKING Part II March 16, Lisbon, Portugal
  • 17. 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)
  • 18.
  • 19. 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
  • 20. 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
  • 21. Intent Locks IX Table orders Data Page IX X Row Free Space X
  • 22. Intent Locks Lock must wait! IX S Table orders Data Page IX X Row Free Space X
  • 23. Escalation Don’t escalate by TF1211 or take artificial IS on table Table Ignore # locks by TF1224 IX X orders Data Page IX Partition X Row Free Space X ALTER <table> SET (LOCK_ESCALATION = auto| table| disable)
  • 24. 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?
  • 25. 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
  • 26. 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?
  • 27. 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
  • 28. LOCKING Demo March 16, Lisbon, Portugal
  • 29. ISOLATION Part III March 16, Lisbon, Portugal
  • 30. 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!
  • 31. 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.
  • 32. 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.
  • 33. 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
  • 34. 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
  • 35. ISOLATION Demo March 16, Lisbon, Portugal
  • 36. 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.
  • 37. 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/
  • 38. 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
  • 39. Thank you for attending this session and SQLSaturday #188 Lisbon March 16, Lisbon, Portugal
  • 40. What Are Your Professional Development Goals? I want to take I want to I want to I want to expand the path from upgrade give my my network in the DBA to Data career a business analytics Analytics Guru my skills competitive industry edge Sound familiar? Get a head start and join us today at: www.passbaconference.com #passbac Enjoy $150 off registration: use code CHM2D