ì

372 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
372
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ì

  1. 1. Optimizing Procedural Code Understanding Logging, Recovery, Locking and Performance Presentation HIGHLIGHT The Anatomy of a Data Modification Kimberly L. Tripp President/Founder
  2. 2. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm 1.1. User sends UPDATEUser sends UPDATE  Update is highly selective (only 5 rows)Update is highly selective (only 5 rows)  Indexes exist to aid in finding these rowsIndexes exist to aid in finding these rows efficientlyefficiently  The update is a SINGLE statement batch NOTThe update is a SINGLE statement batch NOT enclosed in BEGIN TRAN…COMMIT TRAN blockenclosed in BEGIN TRAN…COMMIT TRAN block therefore this is IMPLICIT transactiontherefore this is IMPLICIT transaction 2.2. Server receives the request and locates theServer receives the request and locates the data in cache OR reads the data from diskdata in cache OR reads the data from disk into cacheinto cache  Since this is highly selective only the necessarySince this is highly selective only the necessary pages are read into cache (maybe a few extra butpages are read into cache (maybe a few extra but that’s not important here)that’s not important here)  Let’s use an example where the 5 rows beingLet’s use an example where the 5 rows being modified are located on 3 data pagesmodified are located on 3 data pages The Anatomy of a Data ModificationThe Anatomy of a Data Modification
  3. 3. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm UPDATE… Data Log Server… Cache What it Looks Like – DataWhat it Looks Like – Data
  4. 4. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm 3.3. SQL Server proceeds to lock the necessary dataSQL Server proceeds to lock the necessary data  Locks are necessary to give us a consistent pointLocks are necessary to give us a consistent point FOR ALL rows from which to startFOR ALL rows from which to start  If any other transaction(s) have ANY of these rowsIf any other transaction(s) have ANY of these rows locked we will wait until ALL locks have beenlocked we will wait until ALL locks have been acquired before we can proceed.acquired before we can proceed.  In the case of this updateIn the case of this update (because it’s highly(because it’s highly selective and because indexes exist to make thisselective and because indexes exist to make this possible)possible) SQL Server will use row level locking.SQL Server will use row level locking.  The rows are locked but there are also “intent” locksThe rows are locked but there are also “intent” locks at higher levels to make sure other larger locks (likeat higher levels to make sure other larger locks (like page or table level locks) are not attempted (and fail)page or table level locks) are not attempted (and fail)  There are a few locks that have already occurred –There are a few locks that have already occurred – within indexes, etc. to read the data – butwithin indexes, etc. to read the data – but they are not significant herethey are not significant here This sounds complex but it’s not too bad…This sounds complex but it’s not too bad… The Anatomy of a Data ModificationThe Anatomy of a Data Modification
  5. 5. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm Cache Page Page Page Row Row Row Row Row Update Lock Update Lock Update Lock Update Lock Update Lock What it Looks Like – LocksWhat it Looks Like – Locks
  6. 6. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm 4.4. SQL Server can now begin to make theSQL Server can now begin to make the modifications – for EVERY row the processmodifications – for EVERY row the process will include:will include: 1.1. Change the lock to a stricter lock (eXclusive lock)Change the lock to a stricter lock (eXclusive lock)  An update lock helps to allow better concurrency by beingAn update lock helps to allow better concurrency by being compatible with shared locks (readers). Readers can read thecompatible with shared locks (readers). Readers can read the pre-modified data as it is still transactionally consistent andpre-modified data as it is still transactionally consistent and has not YET been modifiedhas not YET been modified  The eXclusive lock is required in order to change the dataThe eXclusive lock is required in order to change the data because once modified no other reads will be allowed to seebecause once modified no other reads will be allowed to see this transient/un-committed state of the datathis transient/un-committed state of the data 2.2. Make the modification to the data row (yes, inMake the modification to the data row (yes, in cache)cache) 3.3. Log the modification to the transaction log pagesLog the modification to the transaction log pages (also in cache)(also in cache) The Anatomy of a Data ModificationThe Anatomy of a Data Modification
  7. 7. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm Cache Page Page Page Row Row Row Row Row Update LockExclusive Lockx x Update LockExclusive Lockx x Update LockExclusive Lockxx Update LockExclusive Lockxx Update LockExclusive Lockx x L What it Looks Like – ModificationsWhat it Looks Like – Modifications
  8. 8. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm 5.5. Finally, the transaction is complete – this is theFinally, the transaction is complete – this is the MOST critical stepMOST critical step  All rows have been modifiedAll rows have been modified  There are no other statements in this transaction –There are no other statements in this transaction – i.e. this is an implicit transaction in “Autocommit”i.e. this is an implicit transaction in “Autocommit” mode. NOTE: the session setting SETmode. NOTE: the session setting SET IMPLICIT_TRANSACTIONS ON cannot be turned onIMPLICIT_TRANSACTIONS ON cannot be turned on and rarely should be!and rarely should be! Makes for sloppy developers!)Makes for sloppy developers!)  Steps to ensure durability are:Steps to ensure durability are: 1.1. Write all log pages to transaction log ON DISKWrite all log pages to transaction log ON DISK 2.2. Release the locksRelease the locks 3.3. Send a message to the user:Send a message to the user: (5 Rows Affected)(5 Rows Affected) The Anatomy of a Data ModificationThe Anatomy of a Data Modification
  9. 9. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm LData Log Server… Cache~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~ Sequential writes Change Change Change Change … Log 5 Rows Affected After the log entries are made and the locks are released… What it Looks LikeWhat it Looks Like Write-ahead LoggingWrite-ahead Logging
  10. 10. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm  The transaction log portion of theThe transaction log portion of the database ON DISK – is up to datedatabase ON DISK – is up to date  The data in CACHE – is up to dateThe data in CACHE – is up to date  But when does the data get written fromBut when does the data get written from cache to disk?cache to disk? CHECKPOINTCHECKPOINT It’s important to realize that it’s not the sole purposeIt’s important to realize that it’s not the sole purpose of checkpoint to just to writeof checkpoint to just to write committedcommitted pages…pages… Instead a checkpoint writes ALL pages which haveInstead a checkpoint writes ALL pages which have changed since they were brought into cache –changed since they were brought into cache – regardlessregardless of the state of the transactionof the state of the transaction that changed them!that changed them! The Anatomy of a Data ModificationThe Anatomy of a Data Modification
  11. 11. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm Transactions… Action Required if restart recovery None Checkpoint System Failure 11 22 33 44 55 Roll forward Roll back Roll forward Roll back L D L/D L/D L L Time Transaction Recovery andTransaction Recovery and CheckpointsCheckpoints
  12. 12. Enterprise Database Administration and Deployment SIG • Pre-conference Workshop • Tuesday, September 28 • 8:30 am – 4:30 pm  Data Portion mostly random reads – except atData Portion mostly random reads – except at checkpointcheckpoint  Log Portion mostly sequential writesLog Portion mostly sequential writes  Separate physical disks minimizes contentionSeparate physical disks minimizes contention at the drive level – first choice in tuningat the drive level – first choice in tuning  Log is critical!Log is critical!  Log is written AHEAD of the data portionLog is written AHEAD of the data portion  Log is the ONLY place where transactionalLog is the ONLY place where transactional consistency is known (i.e. guaranteed)consistency is known (i.e. guaranteed)  Once a checkpoint occurs SQL Server doesn’t needOnce a checkpoint occurs SQL Server doesn’t need the information in the log – for committed (the information in the log – for committed (a.k.a.a.k.a. inactiveinactive) transactions () transactions (the log could even be clearedthe log could even be cleared))  However… without the transaction log a databaseHowever… without the transaction log a database cannot function (cannot function (i.e. marked suspecti.e. marked suspect))  Then what?Then what? Key PointsKey Points

×