Troubleshooting Deadlocks in SQL Server 2000

3,249 views
3,033 views

Published on

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

No Downloads
Views
Total views
3,249
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
92
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Troubleshooting Deadlocks in SQL Server 2000

  1. 2. PASS Community Summit - Seattle November 18-22, 2002 S231/S348 - Resolving Deadlocks in SQL Server 2000 Ron Talmage - Prospice, LLC
  2. 3. Agenda: Troubleshooting Deadlocks in SQL Server 2000 <ul><li>I. Deadlocking Concepts </li></ul><ul><li>II. Types of Lock Deadlocks </li></ul><ul><li>III. Gathering Deadlock Information </li></ul><ul><li>IV. Finding Causes </li></ul><ul><li>V. Resolution Techniques </li></ul><ul><li>This session will focus on deadlocks due to lock contention </li></ul>
  3. 4. Troubleshooting Deadlocks in SQL Server 2000 <ul><li>I. Deadlocking Concepts </li></ul><ul><li>II. Types of Deadlocks </li></ul><ul><li>III. Gathering Deadlock Information </li></ul><ul><li>IV. Finding Causes </li></ul><ul><li>V. Resolution Techniques </li></ul>
  4. 5. <ul><li>A SQL Server deadlock involves two or more processes or threads </li></ul><ul><li>Each must wait to acquire some resource that the other thread already has </li></ul><ul><li>Neither thread can finish, resulting in a deadly embrace </li></ul>Deadlocking Defined
  5. 6. Lock-Based Deadlocking Structure GRANT T3 Request(WAIT on Tran1) T5 Blocked (blocking removed) Deadlock Victim T6 Commit T7 Request (WAIT on Tran2) T4 Blocked GRANT T2 Begin Tran Begin Tran T1 Tran 2 Tran 1 Time
  6. 7. Deadlocking Illustrated Thread 1 Thread 2 Grant Wait Blocked request Resource Resource
  7. 8. SQL Server Resource Waits <ul><li>According to BOL, SQL Server threads can wait on </li></ul><ul><ul><li>Locks </li></ul></ul><ul><ul><li>Parallel query resources </li></ul></ul><ul><ul><li>Other threads </li></ul></ul><ul><ul><li>Memory resource </li></ul></ul><ul><ul><li>Application events </li></ul></ul><ul><li>Deadlocking can occur with locks, parallelism, threads, and application events. </li></ul><ul><ul><li>Memory waits are resolved by query time-out. </li></ul></ul><ul><li>The most frequent source of SQL Server deadlocking is resource locking </li></ul>
  8. 9. Deadlocks Involving Locks <ul><li>Involve two or more threads, at least one transaction, and one or more resources </li></ul><ul><li>The deadlock takes place in two stages: </li></ul><ul><ul><li>Grant stage </li></ul></ul><ul><ul><ul><li>Each thread is granted a lock on its resource </li></ul></ul></ul><ul><ul><li>Blocked Request stage </li></ul></ul><ul><ul><ul><li>Each thread requests an incompatible lock on the other thread's resource </li></ul></ul></ul><ul><ul><ul><li>Each thread waits on the other to release its locks before it can complete </li></ul></ul></ul><ul><li>SQL Server detects the deadlocked state and kills one of the threads </li></ul>
  9. 10. Deadlocking is more than Blocking <ul><li>Blocking occurs when one thread is waiting on another </li></ul><ul><li>Some blocking is normal </li></ul><ul><ul><li>Expected in an RDBMS that uses locking to maintain transaction isolation, and not versioning </li></ul></ul><ul><ul><li>Only blocks with long durations should be considered a problem </li></ul></ul><ul><li>Lock-based deadlocking is a special type of blocking </li></ul><ul><ul><li>Two or more threads mutually block each other </li></ul></ul>
  10. 11. How SQL Server handles a Deadlock <ul><li>The Lock Monitor thread detects the deadlock </li></ul><ul><ul><li>Uses periodic detection (~5 seconds) </li></ul></ul><ul><li>Automatically chooses one thread as the deadlock victim </li></ul><ul><ul><li>Rolls back its transaction </li></ul></ul><ul><ul><li>Returns error 1205 to the client </li></ul></ul><ul><ul><li>Cancels its current query </li></ul></ul><ul><li>Generally chooses the least expensive transaction to roll back </li></ul><ul><ul><li>You can override this somewhat using SET DEADLOCK_PRIORITY to LOW for a session </li></ul></ul>
  11. 12. Error 1205 Notes <ul><li>Text of the message: </li></ul><ul><ul><ul><li>&quot;Server: Msg 1205, Level 13, State 50, Line 1 </li></ul></ul></ul><ul><ul><ul><li>Transaction (Process ID 54) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.&quot; </li></ul></ul></ul><ul><li>Error 1205 is non-fatal - connection not terminated </li></ul><ul><ul><ul><li>SQL Server aborts the batch and cancels the query </li></ul></ul></ul><ul><ul><ul><li>Error 1205 cannot be detected by @@ERROR within the batch or from calling a stored procedure </li></ul></ul></ul><ul><ul><ul><li>Severity level 13 – correctable by the user </li></ul></ul></ul><ul><li>Error 1205 does not reveal </li></ul><ul><ul><ul><li>Statements at the Grant stage </li></ul></ul></ul><ul><ul><ul><li>Statements at the Blocked Request stage </li></ul></ul></ul><ul><ul><ul><li>The identity of the other participating spid or spids </li></ul></ul></ul>
  12. 13. SQL Server Deadlocking Factors <ul><li>Lock compatibility </li></ul><ul><li>Transaction timing </li></ul><ul><li>Order of lock requests </li></ul><ul><li>Isolation level </li></ul>
  13. 14. 1. Lock Modes Used with bulk copy into a table with either TABLOCK hint or table lock option is set BU Bulk Update Have update lock with intent to acquire exclusive lock at a finer level UIX Update Intent Exclusive Have shared lock with intent to acquire exclusive lock at a finer level SIX Shared Intent Exclusive Have shared lock with intent to acquire update lock at a finer level SIU Shared Intent Update Have or intend to request exclusive lock(s) at a finer level IX Intent Exclusive Have or intend to request update lock(s) at a finer level IU Intent Update Have or intend to request shared lock(s) at a finer level IS Intent Shared Used for writing (insert, update, delete) X Exclusive Used to evaluate prior to writing (may become exclusive) U Update Used for reading (read lock) S Shared Used for DDL operations (ALTER or DROP) on a table schema Sch-M Schema Modification Used when compiling queries Sch-S Schema-Stability Description Abbreviation Lock Mode
  14. 15. Lock Compatibility Matrix (default isolation level) Lock Mode Already Granted BU Sch-M Sch-S X SIX IX U S IS Lock Mode Requested No No Yes No Yes Yes Yes Yes Yes IS No No Yes No No No Yes Yes Yes S No No Yes No No No No Yes Yes U No No Yes No No Yes No No Yes IX No No Yes No No No No No Yes SIX No No Yes No No No No No No X Yes No Yes No No No No No No BU No No No No No No No No No Sch-M Yes No Yes Yes Yes Yes Yes Yes Yes Sch-S
  15. 16. 2. Transaction Timing <ul><li>Transactions must overlap in time for blocking and deadlocking to occur </li></ul><ul><li>Transactions must have sufficiently long duration to make blocking and deadlocking likely </li></ul><ul><li>Some deadlocks require a large number of concurrent transactions to surface </li></ul>
  16. 17. 3. Order of Lock Requests <ul><li>Initial locks must be granted before incompatible locks are requested </li></ul><ul><li>Changing the order of lock requests may release locks early or remove incompatible locks </li></ul>
  17. 18. 4. Isolation Level <ul><li>Higher isolation levels make deadlocks more likely </li></ul><ul><li>Deadlocks involving all exclusive locks are independent of isolation level </li></ul><ul><li>Some deadlocks involving shared locks can occur with the READ COMMITTED isolation level </li></ul><ul><li>Other deadlocks involving shared locks require isolation levels higher than READ COMMITTED </li></ul>
  18. 19. Isolation Level and Lock Accumulation Held until end of transaction Held until end of transaction unless promoted to exclusive or released Held until data read and processed Read Un-committed Held until end of transaction Held until end of transaction unless promoted to exclusive Held until end of transaction Repeatable Read Held until end of transaction Held until end of transaction Exclusive Held until end of transaction unless promoted to exclusive Held until end of transaction unless promoted to exclusive or released Update Held until end of transaction Held until data read and processed Shared Serializable Read Committed Lock Mode
  19. 20. Troubleshooting Deadlocks in SQL Server 2000 <ul><li>I. Deadlocking Concepts </li></ul><ul><li>II. Types of Lock Deadlocks </li></ul><ul><li>III. Gathering Deadlock Information </li></ul><ul><li>IV. Finding Causes </li></ul><ul><li>V. Resolution Techniques </li></ul>
  20. 21. Types of Lock Deadlocks <ul><li>1. Single-Resource Deadlock </li></ul><ul><ul><li>Sometimes called 'conversion' deadlock </li></ul></ul><ul><ul><li>Involves a single resource </li></ul></ul><ul><ul><li>Deadlock occurs when attempting a conversion from compatible to incompatible locks </li></ul></ul><ul><li>2. Multi-Resource Deadlock </li></ul><ul><ul><li>Sometimes called ‘cycle’ deadlock </li></ul></ul><ul><ul><li>Involves distinct resources for each thread </li></ul></ul><ul><ul><li>Deadlock occurs when each thread requests an incompatible lock on the other’s resource </li></ul></ul>
  21. 22. <ul><li>Single-Resource Deadlock Overview </li></ul><ul><li>Involves only one resource </li></ul><ul><li>Initial Grant stage requires shared locks, because both threads must obtain a lock on a single resource </li></ul><ul><ul><li>Requires an isolation level higher than READ COMMITTED to hold share locks </li></ul></ul><ul><li>Blocked Request stage requires exclusive locks </li></ul>
  22. 23. Single-Resource Deadlock Illustrated Resource Thread 1 Thread 2 Grant Wait Blocked request
  23. 24. Single-Resource Deadlock Select * From Authors With (HOLDLOCK) Where au_id = '172-32-1176' T3 GRANT Update Authors Set contract = 1 Where au_id = '172-32-1176' T5 Blocked (blocking removed) Deadlock Victim T6 Commit T7 Update Authors Set contract = 0 Where au_id = '172-32-1176' T4 Blocked Select * From Authors With (HOLDLOCK) Where au_id = '172-32-1176' T2 GRANT Begin Tran Begin Tran T1 Tran 2 Tran 1 Time
  24. 25. Single-Resource Deadlock Notes <ul><li>Often called 'conversion' deadlocks </li></ul><ul><ul><ul><li>Attempt is made to 'convert' a shared lock to an exclusive lock </li></ul></ul></ul><ul><ul><ul><li>Require shared locks at the Grant stage </li></ul></ul></ul><ul><li>Are comparatively rare because they require a higher than default isolation level: </li></ul><ul><ul><ul><li>SQL Server’s default isolation level is READ COMMITTED </li></ul></ul></ul><ul><ul><ul><li>Conversion deadlocks require REPEATABLE READ or SERIALIZABLE isolation levels </li></ul></ul></ul><ul><ul><ul><li>HOLDLOCK hint is effectively the SERIALIZABLE isolation level </li></ul></ul></ul><ul><li>Require exclusive locks at the Blocked Request stage </li></ul><ul><ul><ul><li>Requests must be blocked by existing share locks </li></ul></ul></ul>
  25. 26. 2. Multi-Resource Deadlocks Overview <ul><li>Involve more than one resource </li></ul><ul><li>Grant stage normally requires exclusive locks </li></ul><ul><li>There are many variations; here are two: </li></ul><ul><ul><li>Exclusive only (X-only): threads only make exclusive lock requests </li></ul></ul><ul><ul><li>Mixed exclusive and shared (X-S): </li></ul></ul><ul><ul><ul><li>First, threads are granted exclusive locks </li></ul></ul></ul><ul><ul><ul><li>Then they request shared locks and are blocked </li></ul></ul></ul><ul><ul><ul><li>Mixed X-S deadlocks can be more subtle and challenging </li></ul></ul></ul>
  26. 27. MR Deadlock Illustrated Res 1 Thread 1 Thread 2 Res 2 Grant Wait Blocked request
  27. 28. X-only MR Deadlock Variation <ul><li>Involves only exclusive locks </li></ul><ul><li>Simplest type of MR deadlock </li></ul><ul><li>Works at any isolation level </li></ul><ul><ul><li>Exclusive locks are always held to the end of a transaction </li></ul></ul>
  28. 29. X-only MR Deadlock Update Titles Set ytd_sales = 0 Where title_id = ‘BU1032' T3 GRANT Update Authors Set contract = 0 Where au_id = '172-32-1176' T5 Blocked (blocking removed) Deadlock Victim T6 Commit T7 Update Titles Set ytd_sales = 0 Where title_id = ‘BU1032' T4 Blocked Update Authors Set contract = 0 Where au_id = '172-32-1176' T2 GRANT Begin Tran Begin Tran T1 Tran 2 Tran 1 Time
  29. 30. X-only MR Deadlock Notes <ul><li>Relatively easy to diagnose </li></ul><ul><ul><li>Involves two threads or transactions accessing the same resources in inverse order </li></ul></ul><ul><li>Could use Update locks in place of Exclusive locks </li></ul><ul><ul><li>Like an exclusive lock, only one update lock can be placed on a resource at one time </li></ul></ul><ul><li>Often used as an example of 'cycle' deadlock </li></ul><ul><li>Easy to prevent if all resources are accessed in the same order </li></ul>
  30. 31. Mixed X-S MR Deadlock Variation <ul><li>Is a more complex and subtle type of deadlock </li></ul><ul><li>Many variations: </li></ul><ul><ul><li>X-X:S-S, </li></ul></ul><ul><ul><li>X-X-X:S-S-S, </li></ul></ul><ul><ul><li>X-IS:IS-X, </li></ul></ul><ul><ul><li>S-IX:IX-S, </li></ul></ul><ul><ul><li>X-X:U-U, etc. </li></ul></ul><ul><li>Can occur with the READ COMMITTED isolation level </li></ul>
  31. 32. Mixed X-S MR Example #1 Update Titles Set ytd_sales = 0 Where title_id = ‘BU1032' T3 GRANT Select * From Authors Where au_id = '172-32-1176' T5 Blocked (blocking removed) Deadlock Victim T6 Commit T7 Select * From Titles Where title_id = ‘BU1032' T4 Blocked Update Authors Set contract = 0 Where au_id = '172-32-1176' T2 GRANT Begin Tran Begin Tran T1 Tran 2 Tran 1 Time
  32. 33. Mixed X-S MR Example #2 Insert Authors Values ('111-11-1112', 'test2', '', '', '', '', '', '11111', 0) T3 GRANT Select * From Authors T5 Blocked (blocking removed) Deadlock Victim T6 Commit T7 Select * From Authors T4 Blocked Insert Authors Values ('111-11-1111', 'test1', '', '', '', '', '', '11111', 0) T2 GRANT Begin Tran Begin Tran T1 Tran 2 Tran 1 Time
  33. 34. Mixed X-S MR Deadlock Notes <ul><li>Can be much more difficult to diagnose </li></ul><ul><li>Can take place with the default isolation level of READ COMMITTED </li></ul><ul><li>SELECT statements can also participate with READ COMMITTED or higher isolation level </li></ul><ul><li>Requires exclusive locks at the initial stage </li></ul><ul><ul><li>An update lock is compatible with the subsequent shared locks </li></ul></ul>
  34. 35. Troubleshooting Deadlocks in SQL Server 2000 <ul><li>I. Deadlocking Concepts </li></ul><ul><li>II. Types of Lock Deadlocks </li></ul><ul><li>III. Gathering Deadlock Information </li></ul><ul><li>IV. Finding Causes </li></ul><ul><li>V. Resolution Techniques </li></ul>
  35. 36. Gathering Deadlock Information <ul><li>1. Use the Perfmon Deadlocks/sec counter </li></ul><ul><li>2. Use the 1204 trace flag </li></ul><ul><ul><li>Redirect deadlock event information to the SQL Server Error Log </li></ul></ul><ul><li>3. Use SQL Trace/Profiler </li></ul>
  36. 37. 1. Use the Deadlocks/sec Sysmon (Perfmon) counter <ul><li>Useful in measuring frequent deadlocking </li></ul><ul><li>Useful when combined with multiuser testing to determine the minimum number of users required to reproduce deadlocks </li></ul>
  37. 38. Adding the Deadlock Counter
  38. 39. 2. Use the 1204 Trace Flag <ul><li>Used in combination with the 3605 flag: </li></ul><ul><ul><ul><li>DBCC TRACEON(3605) </li></ul></ul></ul><ul><ul><ul><li>DBCC TRACEON(1204) </li></ul></ul></ul><ul><li>Output will be sent to the SQL Server error log </li></ul><ul><li>It can be useful to make a SQL Agent job execute these statements at startup </li></ul>
  39. 40. Retrieving 1204 Output <ul><li>When using Enterprise Manager to read the 1204 output, statements occur in unusual order </li></ul><ul><ul><li>Do not export the log </li></ul></ul><ul><li>Instead, use a text editor like Notepad to read the statements in correct order </li></ul><ul><ul><li>You can strip out the datetime and spid data to give more readable output </li></ul></ul>
  40. 41. Sample Trace Flag 1204 Output <ul><li>Deadlock encountered .... Printing deadlock information </li></ul><ul><li>Wait-for graph </li></ul><ul><li>Node:1 </li></ul><ul><li>KEY: 7:1977058079:1 (f600d547433a) CleanCnt:1 Mode: X Flags: 0x0 </li></ul><ul><li>Grant List:: </li></ul><ul><li>Owner:0x19090e20 Mode: X Flg:0x0 Ref:0 Life:02000000 SPID:54 ECID:0 </li></ul><ul><li>SPID: 54 ECID: 0 Statement Type: SELECT Line #: 1 </li></ul><ul><li>Input Buf: Language Event: Select * From Authors </li></ul><ul><li>Requested By: </li></ul><ul><li>ResType:LockOwner Stype:'OR' Mode: S SPID:53 ECID:0 Ec:(0x19c5d530) Value:0x19090dc0 Cost:(0/114) </li></ul><ul><li>Node:2 </li></ul><ul><li>KEY: 7:1977058079:1 (f5006f164aa3) CleanCnt:1 Mode: X Flags: 0x0 </li></ul><ul><li>Grant List:: </li></ul><ul><li>Owner:0x19091000 Mode: X Flg:0x0 Ref:0 Life:02000000 SPID:53 ECID:0 </li></ul><ul><li>SPID: 53 ECID: 0 Statement Type: SELECT Line #: 1 </li></ul><ul><li>Input Buf: Language Event: Select * From Authors </li></ul><ul><li>Requested By: </li></ul><ul><li>ResType:LockOwner Stype:'OR' Mode: S SPID:54 ECID:0 Ec:(0x19dc3530) Value:0x19091180 Cost:(0/114) </li></ul><ul><li>Victim Resource Owner: </li></ul><ul><li>ResType:LockOwner Stype:'OR' Mode: S SPID:54 ECID:0 Ec:(0x19dc3530) Value:0x19091180 Cost:(0/114) </li></ul>Locked resource (leaf node of clustered key, I.e. row) Spid owning the lock Current input buffer Type of lock granted Requesting spid and lock type requested Deadlock victim spid Hash of key value
  41. 42. Interpreting Trace Flag 1204 Output <ul><li>KEY: 7:1977058079:1 (f600d547433a) </li></ul><ul><ul><li>Database 7, Object 1977058079, Indid 1 </li></ul></ul><ul><ul><ul><li>Reflects a row lock on a clustered index </li></ul></ul></ul><ul><ul><li>Hash of key value: f600d547433a </li></ul></ul><ul><li>Spid numbers are important when cross-referencing to SQL Trace data </li></ul><ul><ul><li>See 'Displaying Locking Information' and 'Troubleshooting Deadlocks' in BOL for more information </li></ul></ul>
  42. 43. Trace Flag 1204 Output Notes <ul><li>What 1204 shows: </li></ul><ul><ul><li>The input buffer of the Blocked Request statements </li></ul></ul><ul><ul><li>The locks held at the Grant stage </li></ul></ul><ul><li>What it does not show: </li></ul><ul><ul><li>The statements initiating the Grant locks </li></ul></ul><ul><ul><li>The transaction history for each spid </li></ul></ul><ul><li>Input buffer may be incomplete </li></ul><ul><ul><li>Some system deadlocks provide incomplete information </li></ul></ul>
  43. 44. 3. Use SQL Trace with Profiler <ul><li>Add Deadlock events </li></ul><ul><ul><li>Lock:Deadlock </li></ul></ul><ul><ul><li>Lock:Deadlock Chain </li></ul></ul><ul><li>Save trace data to a table </li></ul><ul><li>Reproduce the transaction history of the deadlock </li></ul>
  44. 45. Configuring SQL Trace/Profiler <ul><li>When tracing specifically for deadlocks, include </li></ul><ul><ul><li>Events </li></ul></ul><ul><ul><ul><li>Cursors: CursorExecute, CursorPrepare </li></ul></ul></ul><ul><ul><ul><li>Errors and Warnings: Attentions and Exceptions </li></ul></ul></ul><ul><ul><ul><li>Locks: Lock:Deadlock and Lock:Deadlock Chain </li></ul></ul></ul><ul><ul><ul><li>Stored Procedures: RPC:Completed, RPC:Starting, SP:StmtCompleted, SP:StmtStarted </li></ul></ul></ul><ul><ul><ul><li>Transactions: SQL Transaction </li></ul></ul></ul><ul><ul><ul><li>TSQL: SQL:BatchCompleted, SQL:BatchStarting, SQL:StmtCompleted, SQL:StmtStarting </li></ul></ul></ul><ul><ul><li>Data Columns: </li></ul></ul><ul><ul><ul><li>EventClass, EventSubclass, Spid, IntegerData, Mode, StartTime, TextData, DatabaseID, IndexID, HostName, ObjectID, TransactionID, Severity </li></ul></ul></ul>
  45. 46. SQL Trace and SP2 <ul><li>Make sure you apply SQL Server 2000 Service Pack 2 for deadlock tracing </li></ul><ul><li>See &quot;FIX: Deadlock Information Reported with SQL Server 2000 Profiler Is Incorrect&quot; (Q282749) </li></ul>
  46. 47. Viewing Deadlocks in Profiler Deadlock spids now cross-referenced in SP2
  47. 48. SQL Trace Output Notes <ul><li>What SQL Trace shows: </li></ul><ul><ul><li>The statements involved in the deadlock </li></ul></ul><ul><ul><ul><li>The deadlock victim will have a statement starting but not ending </li></ul></ul></ul><ul><ul><li>Shows the beginning and end of each spid's transaction </li></ul></ul><ul><ul><li>Can be used to reproduce each deadlocking spid's transaction history </li></ul></ul><ul><li>What it does not show: </li></ul><ul><ul><li>The statements gaining the initial locks at the Grant stage </li></ul></ul>
  48. 49. Troubleshooting Deadlocks in SQL Server 2000 <ul><li>I. Deadlocking Concepts </li></ul><ul><li>II. Types of Lock Deadlocks </li></ul><ul><li>III. Gathering Deadlock Information </li></ul><ul><li>IV. Finding Causes </li></ul><ul><li>V. Resolution Techniques </li></ul>
  49. 50. Isolating the Cause <ul><li>Find a method to reproduce the deadlock </li></ul><ul><ul><li>Stress test, load test </li></ul></ul><ul><ul><li>Minimal conditions possible </li></ul></ul><ul><li>Reproduce with both 1204 Trace Flag and SQL Trace active </li></ul><ul><li>Examine Trace Flag 1204 output </li></ul><ul><ul><li>Determine which spids are involved </li></ul></ul><ul><ul><li>Determine what types of locks are involved </li></ul></ul><ul><li>Get each spid’s transaction history </li></ul><ul><ul><li>Save SQL Trace data output to SQL Server table </li></ul></ul><ul><ul><li>Reproduce the deadlock under controlled conditions </li></ul></ul>
  50. 51. Steps for Reproducing a Complex Deadlock <ul><li>Save the Trace output to a table </li></ul><ul><li>Find the beginning and ending row numbers of each spid's transaction </li></ul><ul><li>Select the StartTime and TextData for a given spid the transaction in question </li></ul><ul><li>Arrange the transaction histories according to StartTime </li></ul><ul><li>Remove extraneous commands </li></ul><ul><li>Observe locks held using sp_lock </li></ul>
  51. 52. Extracting a Transaction History
  52. 53. Reproducing the Deadlock
  53. 54. Observing Locks <ul><li>Use sp_lock to detect locks per spid </li></ul><ul><li>Additional tools: </li></ul><ul><ul><li>Both sp_lock2 and sp_blockinglocks from Kalen Delaney, Inside SQL Server 2000 </li></ul></ul><ul><li>Identify the statements resulting in the Grant stage locks </li></ul><ul><ul><li>Find the statement causing the lock found in the 1204 output </li></ul></ul>
  54. 55. Identifying the query
  55. 56. Identifying the locks KEY: 7:1977058079:1 (f600d547433a) (from trace flag 1204 output)
  56. 57. Troubleshooting Deadlocks in SQL Server 2000 <ul><li>I. Deadlocking Concepts </li></ul><ul><li>II. Types of Lock Deadlocks </li></ul><ul><li>III. Gathering Deadlock Information </li></ul><ul><li>IV. Finding Causes </li></ul><ul><li>V. Resolution Techniques </li></ul>
  57. 58. Resolution Techniques <ul><li>1. Remove incompatible lock requests </li></ul><ul><li>2. Change the timing of transactions </li></ul><ul><li>3. Change the order of resource requests </li></ul><ul><li>4. Change the isolation level </li></ul>
  58. 59. 1. Remove incompatible lock requests <ul><li>Queries may be obtaining too many locks </li></ul><ul><ul><li>WHERE clauses may be too broad </li></ul></ul><ul><ul><ul><li>I.e., reading a whole table or range of rows where only one row's data is actually desired </li></ul></ul></ul><ul><li>Queries may be obtaining the wrong kinds of locks </li></ul><ul><ul><li>Using hints, such as HOLDLOCK or UPDLOCK, unnecessarily </li></ul></ul>
  59. 60. 2. Change the timing of transactions <ul><li>Make transactions shorter </li></ul><ul><ul><li>Long transactions increase the likelihood of deadlocks </li></ul></ul><ul><li>Transactions may not need to overlap </li></ul><ul><ul><li>Run batch processes sequentially rather than in parallel </li></ul></ul>
  60. 61. 3. Change the order of resource requests <ul><li>For X-only Multi-Resource deadlocks, ensure that resources are accessed in the same order </li></ul><ul><li>May not work for Mixed X-S MR deadlocks </li></ul>
  61. 62. 4. Change the Isolation Level <ul><li>For single-resource 'conversion' deadlocks </li></ul><ul><ul><li>lower the isolation level </li></ul></ul><ul><li>For Mixed X-S MR deadlocks, use a locking hint on the Share lock queries: </li></ul><ul><ul><li>Use READPAST when it is not important to read uncommitted data </li></ul></ul><ul><ul><li>Use NOLOCK when uncommited data must also be read </li></ul></ul>
  62. 63. Additional Resources <ul><li>SQL Server Locking Internals </li></ul><ul><ul><li>Inside SQL Server 2000 , Kalen Delaney (MS Press) </li></ul></ul><ul><li>Transaction Isolation Level Concepts </li></ul><ul><ul><li>Transaction Processing: Concepts and Techniques, Gray and Reuter (Morgan Kaufmann) </li></ul></ul><ul><ul><li>http:// research.microsoft.com/~gray/Isolation.doc </li></ul></ul><ul><li>Relevant Knowledge Base Articles </li></ul><ul><ul><li>Q224453 - INF: Understanding and Resolving SQL Server 7.0 or 2000 Blocking Problems </li></ul></ul><ul><ul><li>Q271509 - INF: How to Monitor SQL Server 2000 Blocking </li></ul></ul><ul><ul><li>Q282749 - FIX: Deadlock Information Reported with SQL Server 2000 Profiler Is Incorrect </li></ul></ul>
  63. 64. Thank you! Thank you for attending this session and PASS Community Summit - Seattle! Please help us improve the quality of our conference by completing your session evaluation form. Completed evaluation forms may be given to the room monitor as you exit or to staff at the registration desk.
  64. 65. Produced by In association with

×