troubleshooting deadlocks in sql server 2000

65

Upload: elliando-dias

Post on 19-May-2015

2.889 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Troubleshooting Deadlocks in SQL Server 2000
Page 2: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

PASS Community Summit - SeattlePASS Community Summit - SeattleNovember 18-22, 2002November 18-22, 2002

S231/S348 - Resolving Deadlocks in SQL Server 2000

Ron Talmage - Prospice, LLC

Page 3: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

33

Agenda: Agenda: Troubleshooting Deadlocks Troubleshooting Deadlocks in SQL Server 2000in SQL Server 2000 I. Deadlocking ConceptsI. Deadlocking Concepts II. Types of Lock DeadlocksII. Types of Lock Deadlocks III. Gathering Deadlock InformationIII. Gathering Deadlock Information IV. Finding CausesIV. Finding Causes V. Resolution Techniques V. Resolution Techniques

This session will focus on deadlocks due to lock This session will focus on deadlocks due to lock contentioncontention

Page 4: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

44

Troubleshooting Deadlocks Troubleshooting Deadlocks in SQL Server 2000in SQL Server 2000 I. Deadlocking ConceptsI. Deadlocking Concepts II. Types of DeadlocksII. Types of Deadlocks III. Gathering Deadlock InformationIII. Gathering Deadlock Information IV. Finding CausesIV. Finding CausesV. Resolution TechniquesV. Resolution Techniques

Page 5: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

55

• A SQL Server deadlock involves two or A SQL Server deadlock involves two or more processes or threadsmore processes or threads

• Each must wait to acquire some Each must wait to acquire some resource that the other thread already resource that the other thread already hashas

• Neither thread can finish, resulting in a Neither thread can finish, resulting in a deadly embracedeadly embrace

Deadlocking DefinedDeadlocking Defined

Page 6: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

66

Lock-Based Deadlocking Lock-Based Deadlocking StructureStructureTimeTime Tran 1Tran 1 Tran 2Tran 2

T1T1 Begin TranBegin Tran Begin TranBegin Tran

T2T2 GRANTGRANT

T3T3 GRANTGRANT

T4T4

BlockedBlocked

Request (WAIT on Request (WAIT on Tran2)Tran2)

T5T5

BlockedBlocked

Request(WAIT on Tran1)Request(WAIT on Tran1)

T6T6 Deadlock VictimDeadlock Victim (blocking removed)(blocking removed)

T7T7 CommitCommit

Page 7: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

77

Deadlocking IllustratedDeadlocking Illustrated

ResourceResourceResourceResourceThread 1Thread 1 Thread 2Thread 2

GrantGrant

WaitWait

BlockedBlockedrequestrequest

Page 8: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

88

SQL Server Resource WaitsSQL Server Resource Waits According to BOL, SQL Server threads can According to BOL, SQL Server threads can

wait onwait on LocksLocks Parallel query resourcesParallel query resources Other threads Other threads Memory resourceMemory resource Application eventsApplication events

Deadlocking can occur with locks, parallelism, Deadlocking can occur with locks, parallelism, threads, and application events.threads, and application events. Memory waits are resolved by query time-out.Memory waits are resolved by query time-out.

The most frequent source of SQL Server The most frequent source of SQL Server deadlocking is resource lockingdeadlocking is resource locking

Page 9: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

99

Deadlocks Involving LocksDeadlocks Involving Locks Involve two or more threads, at least one Involve two or more threads, at least one

transaction, and one or more resourcestransaction, and one or more resources The deadlock takes place in two stages:The deadlock takes place in two stages:

Grant stageGrant stage Each thread is granted a lock on its resourceEach thread is granted a lock on its resource

Blocked Request stageBlocked Request stage Each thread requests an incompatible lock on the other Each thread requests an incompatible lock on the other

thread's resourcethread's resource Each thread waits on the other to release its locks before it Each thread waits on the other to release its locks before it

can completecan complete

SQL Server detects the deadlocked state and SQL Server detects the deadlocked state and kills one of the threadskills one of the threads

Page 10: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

1010

DeadlockingDeadlockingis more than Blockingis more than Blocking

Blocking occurs when one thread is waiting on Blocking occurs when one thread is waiting on anotheranother

Some blocking is normal Some blocking is normal Expected in an RDBMS that uses locking to maintain Expected in an RDBMS that uses locking to maintain

transaction isolation, and not versioningtransaction isolation, and not versioning Only blocks with long durations should be Only blocks with long durations should be

considered a problemconsidered a problem

Lock-based deadlocking is a special type of Lock-based deadlocking is a special type of blockingblocking Two or more threads mutually block each otherTwo or more threads mutually block each other

Page 11: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

1111

How SQL ServerHow SQL Serverhandles a Deadlockhandles a Deadlock The Lock Monitor thread detects the deadlockThe Lock Monitor thread detects the deadlock

Uses periodic detection (~5 seconds)Uses periodic detection (~5 seconds)

Automatically chooses one thread as the Automatically chooses one thread as the deadlock victimdeadlock victim Rolls back its transactionRolls back its transaction Returns error 1205 to the clientReturns error 1205 to the client Cancels its current queryCancels its current query

Generally chooses the least expensive Generally chooses the least expensive transaction to roll backtransaction to roll back You can override this somewhat using SET You can override this somewhat using SET

DEADLOCK_PRIORITY to LOW for a sessionDEADLOCK_PRIORITY to LOW for a session

Page 12: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

1212

Error 1205 NotesError 1205 Notes Text of the message:Text of the message:

"Server: Msg 1205, Level 13, State 50, Line 1"Server: Msg 1205, Level 13, State 50, Line 1

Transaction (Process ID 54) was deadlocked on lock Transaction (Process ID 54) was deadlocked on lock resources with another process and has been chosen as resources with another process and has been chosen as the deadlock victim. Rerun the transaction."the deadlock victim. Rerun the transaction."

Error 1205 is non-fatal - connection not Error 1205 is non-fatal - connection not terminatedterminated

SQL Server aborts the batch and cancels the querySQL Server aborts the batch and cancels the query Error 1205 cannot be detected by @@ERROR within the Error 1205 cannot be detected by @@ERROR within the

batch or from calling a stored procedurebatch or from calling a stored procedure Severity level 13 – correctable by the userSeverity level 13 – correctable by the user

Error 1205 does not revealError 1205 does not reveal Statements at the Grant stageStatements at the Grant stage Statements at the Blocked Request stageStatements at the Blocked Request stage The identity of the other participating spid or spidsThe identity of the other participating spid or spids

Page 13: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

1313

SQL Server Deadlocking SQL Server Deadlocking FactorsFactors

1.1. Lock compatibilityLock compatibility

2.2. Transaction timingTransaction timing

3.3. Order of lock requestsOrder of lock requests

4.4. Isolation levelIsolation level

Page 14: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

1414

1. Lock Modes1. Lock ModesLock Mode Abbreviation Description

Schema-Stability Sch-S Used when compiling queries

Schema Modification

Sch-M Used for DDL operations (ALTER or DROP) on a table schema

Shared S Used for reading (read lock)

Update U Used to evaluate prior to writing (may become exclusive)

Exclusive X Used for writing (insert, update, delete)

Intent Shared IS Have or intend to request shared lock(s) at a finer level

Intent Update IU Have or intend to request update lock(s) at a finer level

Intent Exclusive IX Have or intend to request exclusive lock(s) at a finer level

Shared Intent Update

SIU Have shared lock with intent to acquire update lock at a finer level

Shared Intent Exclusive

SIX Have shared lock with intent to acquire exclusive lock at a finer level

Update Intent Exclusive

UIX Have update lock with intent to acquire exclusive lock at a finer level

Bulk Update BU Used with bulk copy into a table with either TABLOCK hint or table lock option is set

Page 15: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

1515

Lock Compatibility MatrixLock Compatibility Matrix(default isolation level)(default isolation level)

Lock Mode Requested

ISIS SS UU IXIX SIXSIX XX Sch-SSch-S Sch-MSch-M BUBU

ISIS YesYes YesYes YesYes YesYes YesYes No YesYes No No

SS YesYes YesYes YesYes No No No YesYes No No

UU YesYes YesYes No No No No YesYes No No

IXIX YesYes NoNo NoNo YesYes No No YesYes No No

SIXSIX YesYes NoNo NoNo NoNo No No YesYes No No

XX No No No No No No YesYes No No

Sch-SSch-S YesYes YesYes YesYes YesYes YesYes YesYes YesYes No YesYes

Sch-MSch-M No No No No No No No No No

BUBU No No No No No No YesYes No YesYes

Lock Mode Already Granted

Page 16: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

1616

2. Transaction Timing2. Transaction Timing

Transactions must overlap in time for Transactions must overlap in time for blocking and deadlocking to occurblocking and deadlocking to occur

Transactions must have sufficiently Transactions must have sufficiently long duration to make blocking and long duration to make blocking and deadlocking likelydeadlocking likely

Some deadlocks require a large Some deadlocks require a large number of concurrent transactions to number of concurrent transactions to surfacesurface

Page 17: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

1717

3. Order of Lock Requests3. Order of Lock Requests

Initial locks must be granted before Initial locks must be granted before incompatible locks are requestedincompatible locks are requested

Changing the order of lock requests Changing the order of lock requests may release locks early or remove may release locks early or remove incompatible locksincompatible locks

Page 18: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

1818

4. Isolation Level4. Isolation Level Higher isolation levels make deadlocks Higher isolation levels make deadlocks

more likelymore likely Deadlocks involving all exclusive locks are Deadlocks involving all exclusive locks are

independent of isolation levelindependent of isolation level Some deadlocks involving shared locks can Some deadlocks involving shared locks can

occur with the READ COMMITTED isolation occur with the READ COMMITTED isolation levellevel

Other deadlocks involving shared locks Other deadlocks involving shared locks require isolation levels higher than READ require isolation levels higher than READ COMMITTEDCOMMITTED

Page 19: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

1919

Isolation Level and Lock Isolation Level and Lock AccumulationAccumulationLock Lock ModeMode

Read Un-Read Un-committedcommitted

Read Read CommittedCommitted

Repeatable Repeatable ReadRead

SerializableSerializable

Shared Held until data read and processed

Held until data read and processed

Held until end of transaction

Held until end of transaction

Update Held until end of transaction unless promoted to exclusive or released

Held until end of transaction unless promoted to exclusive or released

Held until end of transaction unless promoted to exclusive

Held until end of transaction unless promoted to exclusive

Exclusive Held until end of transaction

Held until end of transaction

Held until end of transaction

Held until end of transaction

Page 20: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

2020

Troubleshooting DeadlocksTroubleshooting Deadlocksin SQL Server 2000in SQL Server 2000 I. Deadlocking ConceptsI. Deadlocking Concepts II. Types of Lock DeadlocksII. Types of Lock Deadlocks III. Gathering Deadlock InformationIII. Gathering Deadlock Information IV. Finding CausesIV. Finding CausesV. Resolution TechniquesV. Resolution Techniques

Page 21: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

2121

Types of Lock DeadlocksTypes of Lock Deadlocks 1. Single-Resource Deadlock1. Single-Resource Deadlock

Sometimes called 'conversion' deadlockSometimes called 'conversion' deadlock Involves a single resourceInvolves a single resource Deadlock occurs when attempting a conversion Deadlock occurs when attempting a conversion

from compatible to incompatible locksfrom compatible to incompatible locks

2. Multi-Resource Deadlock2. Multi-Resource Deadlock Sometimes called ‘cycle’ deadlockSometimes called ‘cycle’ deadlock Involves distinct resources for each threadInvolves distinct resources for each thread Deadlock occurs when each thread requests an Deadlock occurs when each thread requests an

incompatible lock on the other’s resourceincompatible lock on the other’s resource

Page 22: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

2222

1.1. Single-Resource Single-Resource Deadlock OverviewDeadlock Overview

Involves only one resourceInvolves only one resource Initial Grant stage requires shared locks, Initial Grant stage requires shared locks,

because both threads must obtain a because both threads must obtain a lock on a single resourcelock on a single resource Requires an isolation level higher than Requires an isolation level higher than

READ COMMITTED to hold share locksREAD COMMITTED to hold share locks

Blocked Request stage requires Blocked Request stage requires exclusive locksexclusive locks

Page 23: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

2323

Single-ResourceSingle-Resource Deadlock Deadlock Illustrated Illustrated

ResourceResourceThread 1Thread 1 Thread 2Thread 2

GrantGrant

WaitWait

BlockedBlockedrequestrequest

Page 24: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

2424

Single-Resource DeadlockSingle-Resource Deadlock TimeTime Tran 1Tran 1 Tran 2Tran 2

T1T1 Begin TranBegin Tran Begin TranBegin Tran

T2T2

GRANTGRANT

Select * Select *

From Authors With (HOLDLOCK) From Authors With (HOLDLOCK)

Where au_id = '172-32-1176'Where au_id = '172-32-1176'

T3T3

GRANTGRANT

Select * Select *

From Authors With (HOLDLOCK) From Authors With (HOLDLOCK)

Where au_id = '172-32-1176'Where au_id = '172-32-1176'

T4T4

BlockedBlocked

Update Authors Update Authors

Set contract = 0 Set contract = 0

Where au_id = '172-32-1176'Where au_id = '172-32-1176'

T5T5

BlockedBlocked

Update Authors Update Authors

Set contract = 1 Set contract = 1

Where au_id = '172-32-1176'Where au_id = '172-32-1176'

T6T6 Deadlock VictimDeadlock Victim (blocking removed)(blocking removed)

T7T7 CommitCommit

Page 25: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

2525

Single-Resource Deadlock Single-Resource Deadlock NotesNotes

Often called 'conversion' deadlocksOften called 'conversion' deadlocks Attempt is made to 'convert' a shared lock to an exclusive Attempt is made to 'convert' a shared lock to an exclusive

locklock Require shared locks at the Grant stageRequire shared locks at the Grant stage

Are comparatively rare because they require a Are comparatively rare because they require a higher than default isolation level:higher than default isolation level:

SQL Server’s default isolation level is READ COMMITTEDSQL Server’s default isolation level is READ COMMITTED Conversion deadlocks require REPEATABLE READ or Conversion deadlocks require REPEATABLE READ or

SERIALIZABLE isolation levelsSERIALIZABLE isolation levels HOLDLOCK hint is effectively the SERIALIZABLE isolation HOLDLOCK hint is effectively the SERIALIZABLE isolation

levellevel

Require exclusive locks at the Blocked Request Require exclusive locks at the Blocked Request stagestage

Requests must be blocked by existing share locksRequests must be blocked by existing share locks

Page 26: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

2626

2. Multi-Resource Deadlocks 2. Multi-Resource Deadlocks Overview Overview Involve more than one resourceInvolve more than one resource Grant stage normally requires exclusive locksGrant stage normally requires exclusive locks There are many variations; here are two:There are many variations; here are two:

Exclusive only (X-only): threads only make Exclusive only (X-only): threads only make exclusive lock requestsexclusive lock requests

Mixed exclusive and shared (X-S): Mixed exclusive and shared (X-S): First, threads are granted exclusive locksFirst, threads are granted exclusive locks Then they request shared locks and are blockedThen they request shared locks and are blocked Mixed X-S deadlocks can be more subtle and challengingMixed X-S deadlocks can be more subtle and challenging

Page 27: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

2727

MRMR Deadlock Illustrated Deadlock Illustrated

Res 1Res 1Thread 1Thread 1 Thread 2Thread 2Res 2Res 2

GrantGrant

WaitWait

BlockedBlockedrequestrequest

Page 28: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

2828

X-only MR Deadlock X-only MR Deadlock VariationVariation Involves only exclusive locksInvolves only exclusive locksSimplest type of MR deadlockSimplest type of MR deadlockWorks at any isolation levelWorks at any isolation level

Exclusive locks are always held to the end of a Exclusive locks are always held to the end of a transactiontransaction

Page 29: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

2929

X-only MR DeadlockX-only MR DeadlockTimeTime Tran 1Tran 1 Tran 2Tran 2

T1T1 Begin TranBegin Tran Begin TranBegin Tran

T2T2

GRANTGRANT

Update Authors Update Authors

Set contract = 0 Set contract = 0

Where au_id = '172-32-1176'Where au_id = '172-32-1176'

T3T3

GRANTGRANT

Update Titles Update Titles

Set ytd_sales = 0 Set ytd_sales = 0

Where title_id = ‘BU1032'Where title_id = ‘BU1032'

T4T4

BlockedBlocked

Update Titles Update Titles

Set ytd_sales = 0 Set ytd_sales = 0

Where title_id = ‘BU1032'Where title_id = ‘BU1032'

T5T5

BlockedBlocked

Update Authors Update Authors

Set contract = 0 Set contract = 0

Where au_id = '172-32-1176'Where au_id = '172-32-1176'

T6T6 Deadlock VictimDeadlock Victim (blocking removed)(blocking removed)

T7T7 CommitCommit

Page 30: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

3030

X-only MR Deadlock NotesX-only MR Deadlock Notes Relatively easy to diagnoseRelatively easy to diagnose

Involves two threads or transactions accessing the Involves two threads or transactions accessing the same resources in inverse ordersame resources in inverse order

Could use Update locks in place of Exclusive Could use Update locks in place of Exclusive lockslocks Like an exclusive lock, only one update lock can be Like an exclusive lock, only one update lock can be

placed on a resource at one timeplaced on a resource at one time

Often used as an example of 'cycle' deadlockOften used as an example of 'cycle' deadlock Easy to prevent if all resources are accessed Easy to prevent if all resources are accessed

in the same orderin the same order

Page 31: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

3131

Mixed X-S MR Deadlock Mixed X-S MR Deadlock VariationVariation

Is a more complex and subtle type of deadlockIs a more complex and subtle type of deadlock Many variations:Many variations:

X-X:S-S, X-X:S-S, X-X-X:S-S-S,X-X-X:S-S-S, X-IS:IS-X, X-IS:IS-X, S-IX:IX-S,S-IX:IX-S, X-X:U-U, etc.X-X:U-U, etc.

Can occur with the READ COMMITTED Can occur with the READ COMMITTED isolation levelisolation level

Page 32: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

3232

Mixed X-S MR Example #1Mixed X-S MR Example #1TimeTime Tran 1Tran 1 Tran 2Tran 2

T1T1 Begin TranBegin Tran Begin TranBegin Tran

T2T2

GRANTGRANT

Update Authors Update Authors

Set contract = 0 Set contract = 0

Where au_id = '172-32-1176'Where au_id = '172-32-1176'

T3T3

GRANTGRANT

Update Titles Update Titles

Set ytd_sales = 0 Set ytd_sales = 0

Where title_id = ‘BU1032'Where title_id = ‘BU1032'

T4T4

BlockedBlocked

Select *Select *

From Titles From Titles

Where title_id = ‘BU1032'Where title_id = ‘BU1032'

T5T5

BlockedBlocked

Select *Select *

From Authors From Authors

Where au_id = '172-32-1176'Where au_id = '172-32-1176'

T6T6 Deadlock VictimDeadlock Victim (blocking removed)(blocking removed)

T7T7 CommitCommit

Page 33: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

3333

Mixed X-S MR Example #2Mixed X-S MR Example #2TimeTime Tran 1Tran 1 Tran 2Tran 2

T1T1 Begin TranBegin Tran Begin TranBegin Tran

T2T2

GRANTGRANT

Insert Authors Values Insert Authors Values

('111-11-1111', 'test1', '', '', '', '', ('111-11-1111', 'test1', '', '', '', '', '', '11111', 0)'', '11111', 0)

T3T3

GRANTGRANT

Insert Authors Values Insert Authors Values

('111-11-1112', 'test2', '', '', '', '', '', ('111-11-1112', 'test2', '', '', '', '', '', '11111', 0)'11111', 0)

T4T4

BlockedBlocked

Select * Select *

From Authors From Authors

T5T5

BlockedBlocked

Select *Select *

From Authors From Authors

T6T6 Deadlock VictimDeadlock Victim (blocking removed)(blocking removed)

T7T7 CommitCommit

Page 34: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

3434

Mixed X-S MR Deadlock Mixed X-S MR Deadlock NotesNotes

Can be much more difficult to diagnoseCan be much more difficult to diagnose Can take place with the default isolation level Can take place with the default isolation level

of READ COMMITTEDof READ COMMITTED SELECT statements can also participate with SELECT statements can also participate with

READ COMMITTED or higher isolation levelREAD COMMITTED or higher isolation level Requires exclusive locks at the initial stageRequires exclusive locks at the initial stage

An update lock is compatible with the subsequent An update lock is compatible with the subsequent shared locksshared locks

Page 35: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

3535

Troubleshooting DeadlocksTroubleshooting Deadlocksin SQL Server 2000in SQL Server 2000 I. Deadlocking ConceptsI. Deadlocking Concepts II. Types of Lock DeadlocksII. Types of Lock Deadlocks III. Gathering Deadlock InformationIII. Gathering Deadlock Information IV. Finding CausesIV. Finding CausesV. Resolution TechniquesV. Resolution Techniques

Page 36: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

3636

Gathering Deadlock Gathering Deadlock InformationInformation1. Use the Perfmon Deadlocks/sec 1. Use the Perfmon Deadlocks/sec

countercounter2. Use the 1204 trace flag2. Use the 1204 trace flag

Redirect deadlock event information to the Redirect deadlock event information to the SQL Server Error Log SQL Server Error Log

3. Use SQL Trace/Profiler 3. Use SQL Trace/Profiler

Page 37: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

3737

1. Use the Deadlocks/sec 1. Use the Deadlocks/sec Sysmon (Perfmon) counterSysmon (Perfmon) counter

Useful in measuring frequent Useful in measuring frequent deadlockingdeadlocking

Useful when combined with multiuser Useful when combined with multiuser testing to determine the minimum testing to determine the minimum number of users required to reproduce number of users required to reproduce deadlocksdeadlocks

Page 38: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

3838

Adding the Deadlock Adding the Deadlock CounterCounter

Page 39: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

3939

2. Use the 1204 Trace Flag2. Use the 1204 Trace Flag

Used in combination with the 3605 flag:Used in combination with the 3605 flag:DBCC TRACEON(3605)DBCC TRACEON(3605)

DBCC TRACEON(1204)DBCC TRACEON(1204)

Output will be sent to the SQL Server Output will be sent to the SQL Server error logerror log

It can be useful to make a SQL Agent job It can be useful to make a SQL Agent job execute these statements at startupexecute these statements at startup

Page 40: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

4040

Retrieving 1204 OutputRetrieving 1204 Output

When using Enterprise Manager to read When using Enterprise Manager to read the 1204 output, statements occur in the 1204 output, statements occur in unusual orderunusual order Do not export the logDo not export the log

Instead, use a text editor like Notepad to Instead, use a text editor like Notepad to read the statements in correct orderread the statements in correct order You can strip out the datetime and spid data You can strip out the datetime and spid data

to give more readable outputto give more readable output

Page 41: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

4141

Sample Trace Flag 1204 Sample Trace Flag 1204 OutputOutputDeadlock encountered .... Printing deadlock informationDeadlock encountered .... Printing deadlock information

Wait-for graphWait-for graph

Node:1Node:1

KEY: 7:1977058079:1 (f600d547433a) CleanCnt:1 Mode: X Flags: 0x0KEY: 7:1977058079:1 (f600d547433a) CleanCnt:1 Mode: X Flags: 0x0

Grant List::Grant List::

Owner:0x19090e20 Mode: X Flg:0x0 Ref:0 Life:02000000 SPID:54 ECID:0Owner:0x19090e20 Mode: X Flg:0x0 Ref:0 Life:02000000 SPID:54 ECID:0

SPID: 54 ECID: 0 Statement Type: SELECT Line #: 1SPID: 54 ECID: 0 Statement Type: SELECT Line #: 1

Input Buf: Language Event: Select * From Authors Input Buf: Language Event: Select * From Authors

Requested By: Requested By:

ResType:LockOwner Stype:'OR' Mode: S SPID:53 ECID:0 Ec:(0x19c5d530) Value:0x19090dc0 Cost:(0/114)ResType:LockOwner Stype:'OR' Mode: S SPID:53 ECID:0 Ec:(0x19c5d530) Value:0x19090dc0 Cost:(0/114)

Node:2Node:2

KEY: 7:1977058079:1 (f5006f164aa3) CleanCnt:1 Mode: X Flags: 0x0KEY: 7:1977058079:1 (f5006f164aa3) CleanCnt:1 Mode: X Flags: 0x0

Grant List::Grant List::

Owner:0x19091000 Mode: X Flg:0x0 Ref:0 Life:02000000 SPID:53 ECID:0Owner:0x19091000 Mode: X Flg:0x0 Ref:0 Life:02000000 SPID:53 ECID:0

SPID: 53 ECID: 0 Statement Type: SELECT Line #: 1SPID: 53 ECID: 0 Statement Type: SELECT Line #: 1

Input Buf: Language Event: Select * From Authors Input Buf: Language Event: Select * From Authors

Requested By: Requested By:

ResType:LockOwner Stype:'OR' Mode: S SPID:54 ECID:0 Ec:(0x19dc3530) Value:0x19091180 Cost:(0/114)ResType:LockOwner Stype:'OR' Mode: S SPID:54 ECID:0 Ec:(0x19dc3530) Value:0x19091180 Cost:(0/114)

Victim Resource Owner:Victim Resource Owner:

ResType:LockOwner Stype:'OR' Mode: S SPID:54 ECID:0 Ec:(0x19dc3530) Value:0x19091180 Cost:(0/114)ResType:LockOwner Stype:'OR' Mode: S SPID:54 ECID:0 Ec:(0x19dc3530) Value:0x19091180 Cost:(0/114)

Locked resource (leaf node Locked resource (leaf node of clustered key, I.e. row)of clustered key, I.e. row)

Spid owning the lockSpid owning the lock

Current input bufferCurrent input buffer

Type of lock grantedType of lock granted

Requesting spid and Requesting spid and lock type requestedlock type requested

Deadlock victim spidDeadlock victim spid

Hash of key valueHash of key value

Page 42: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

4242

InterpretingInterpretingTrace Flag 1204 OutputTrace Flag 1204 Output

KEY: 7:1977058079:1 (f600d547433a)KEY: 7:1977058079:1 (f600d547433a) Database 7, Object 1977058079, Indid 1Database 7, Object 1977058079, Indid 1

Reflects a row lock on a clustered indexReflects a row lock on a clustered index

Hash of key value: f600d547433aHash of key value: f600d547433a

Spid numbers are important when Spid numbers are important when cross-referencing to SQL Trace datacross-referencing to SQL Trace data See 'Displaying Locking Information' and See 'Displaying Locking Information' and

'Troubleshooting Deadlocks' in BOL for 'Troubleshooting Deadlocks' in BOL for more information more information

Page 43: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

4343

Trace Flag 1204 Output Trace Flag 1204 Output NotesNotes What 1204 shows:What 1204 shows:

The input buffer of the Blocked Request The input buffer of the Blocked Request statementsstatements

The locks held at the Grant stageThe locks held at the Grant stage

What it does not show:What it does not show: The statements initiating the Grant locksThe statements initiating the Grant locks The transaction history for each spidThe transaction history for each spid

Input buffer may be incompleteInput buffer may be incomplete Some system deadlocks provide incomplete Some system deadlocks provide incomplete

informationinformation

Page 44: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

4444

3. Use SQL Trace with 3. Use SQL Trace with ProfilerProfilerAdd Deadlock eventsAdd Deadlock events

Lock:DeadlockLock:Deadlock Lock:Deadlock ChainLock:Deadlock Chain

Save trace data to a tableSave trace data to a tableReproduce the transaction history of the Reproduce the transaction history of the

deadlockdeadlock

Page 45: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

4545

Configuring SQL Configuring SQL Trace/ProfilerTrace/Profiler

When tracing specifically for deadlocks, includeWhen tracing specifically for deadlocks, include EventsEvents

Cursors: CursorExecute, CursorPrepareCursors: CursorExecute, CursorPrepare Errors and Warnings: Attentions and ExceptionsErrors and Warnings: Attentions and Exceptions Locks: Lock:Deadlock and Lock:Deadlock ChainLocks: Lock:Deadlock and Lock:Deadlock Chain Stored Procedures: RPC:Completed, RPC:Starting, Stored Procedures: RPC:Completed, RPC:Starting,

SP:StmtCompleted, SP:StmtStartedSP:StmtCompleted, SP:StmtStarted Transactions: SQL TransactionTransactions: SQL Transaction TSQL: SQL:BatchCompleted, SQL:BatchStarting, TSQL: SQL:BatchCompleted, SQL:BatchStarting,

SQL:StmtCompleted, SQL:StmtStartingSQL:StmtCompleted, SQL:StmtStarting

Data Columns:Data Columns: EventClass, EventSubclass, Spid, IntegerData, Mode, EventClass, EventSubclass, Spid, IntegerData, Mode,

StartTime, TextData, DatabaseID, IndexID, HostName, StartTime, TextData, DatabaseID, IndexID, HostName, ObjectID, TransactionID, SeverityObjectID, TransactionID, Severity

Page 46: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

4646

SQL Trace and SP2SQL Trace and SP2

Make sure you apply SQL Server 2000 Make sure you apply SQL Server 2000 Service Pack 2 for deadlock tracingService Pack 2 for deadlock tracing

See "FIX: Deadlock Information See "FIX: Deadlock Information Reported with SQL Server 2000 Profiler Reported with SQL Server 2000 Profiler Is Incorrect" (Q282749)Is Incorrect" (Q282749)

Page 47: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

4747

Viewing Deadlocks in ProfilerViewing Deadlocks in Profiler

Deadlock spids now Deadlock spids now cross-referenced in cross-referenced in SP2SP2

Page 48: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

4848

SQL Trace Output NotesSQL Trace Output Notes

What SQL Trace shows: What SQL Trace shows: The statements involved in the deadlockThe statements involved in the deadlock

The deadlock victim will have a statement starting but The deadlock victim will have a statement starting but not endingnot ending

Shows the beginning and end of each spid's Shows the beginning and end of each spid's transactiontransaction

Can be used to reproduce each deadlocking Can be used to reproduce each deadlocking spid's transaction historyspid's transaction history

What it does not show:What it does not show: The statements gaining the initial locks at the The statements gaining the initial locks at the

Grant stageGrant stage

Page 49: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

4949

Troubleshooting Deadlocks Troubleshooting Deadlocks in SQL Server 2000in SQL Server 2000

I. Deadlocking ConceptsI. Deadlocking Concepts II. Types of Lock DeadlocksII. Types of Lock Deadlocks III. Gathering Deadlock InformationIII. Gathering Deadlock Information IV. Finding CausesIV. Finding CausesV. Resolution TechniquesV. Resolution Techniques

Page 50: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

5050

Isolating the CauseIsolating the Cause Find a method to reproduce the deadlockFind a method to reproduce the deadlock

Stress test, load testStress test, load test Minimal conditions possibleMinimal conditions possible

Reproduce with both 1204 Trace Flag and SQL Reproduce with both 1204 Trace Flag and SQL Trace activeTrace active

Examine Trace Flag 1204 outputExamine Trace Flag 1204 output Determine which spids are involvedDetermine which spids are involved Determine what types of locks are involvedDetermine what types of locks are involved

Get each spid’s transaction historyGet each spid’s transaction history Save SQL Trace data output to SQL Server tableSave SQL Trace data output to SQL Server table Reproduce the deadlock under controlled Reproduce the deadlock under controlled

conditionsconditions

Page 51: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

5151

Steps for Reproducing a Steps for Reproducing a Complex DeadlockComplex Deadlock Save the Trace output to a tableSave the Trace output to a table Find the beginning and ending row numbers Find the beginning and ending row numbers

of each spid's transactionof each spid's transaction Select the StartTime and TextData for a given Select the StartTime and TextData for a given

spid the transaction in questionspid the transaction in question Arrange the transaction histories according to Arrange the transaction histories according to

StartTimeStartTime Remove extraneous commandsRemove extraneous commands Observe locks held using sp_lockObserve locks held using sp_lock

Page 52: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

5252

Extracting a Transaction Extracting a Transaction HistoryHistory

Page 53: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

5353

Reproducing the DeadlockReproducing the Deadlock

Page 54: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

5454

Observing LocksObserving Locks

• Use sp_lock to detect locks per spidUse sp_lock to detect locks per spid• Additional tools:Additional tools:

• Both sp_lock2 and sp_blockinglocks from Both sp_lock2 and sp_blockinglocks from Kalen Delaney, Kalen Delaney, Inside SQL Server 2000Inside SQL Server 2000

• Identify the statements resulting in the Identify the statements resulting in the Grant stage locksGrant stage locks• Find the statement causing the lock found in Find the statement causing the lock found in

the 1204 outputthe 1204 output

Page 55: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

5555

Identifying the query Identifying the query

Page 56: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

5656

Identifying the locksIdentifying the locks

KEY: 7:1977058079:1 (f600d547433a)KEY: 7:1977058079:1 (f600d547433a)(from trace flag 1204 output)(from trace flag 1204 output)

Page 57: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

5757

Troubleshooting DeadlocksTroubleshooting Deadlocksin SQL Server 2000in SQL Server 2000 I. Deadlocking ConceptsI. Deadlocking Concepts II. Types of Lock DeadlocksII. Types of Lock Deadlocks III. Gathering Deadlock InformationIII. Gathering Deadlock Information IV. Finding CausesIV. Finding CausesV. Resolution TechniquesV. Resolution Techniques

Page 58: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

5858

Resolution Techniques Resolution Techniques

1. Remove incompatible lock requests1. Remove incompatible lock requests2. Change the timing of transactions2. Change the timing of transactions3. Change the order of resource requests3. Change the order of resource requests4. Change the isolation level 4. Change the isolation level

Page 59: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

5959

1. Remove incompatible 1. Remove incompatible lock requestslock requests

Queries may be obtaining too many Queries may be obtaining too many lockslocks WHERE clauses may be too broadWHERE clauses may be too broad

I.e., reading a whole table or range of rows I.e., reading a whole table or range of rows where only one row's data is actually desiredwhere only one row's data is actually desired

Queries may be obtaining the wrong Queries may be obtaining the wrong kinds of lockskinds of locks Using hints, such as HOLDLOCK or Using hints, such as HOLDLOCK or

UPDLOCK, unnecessarilyUPDLOCK, unnecessarily

Page 60: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

6060

2. Change the timing of 2. Change the timing of transactionstransactions

Make transactions shorterMake transactions shorter Long transactions increase the likelihood of Long transactions increase the likelihood of

deadlocksdeadlocks

Transactions may not need to overlapTransactions may not need to overlap Run batch processes sequentially rather Run batch processes sequentially rather

than in parallelthan in parallel

Page 61: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

6161

3. Change the order of 3. Change the order of resource requestsresource requests

For X-only Multi-Resource deadlocks, For X-only Multi-Resource deadlocks, ensure that resources are accessed in ensure that resources are accessed in the same orderthe same order

May not work for Mixed X-S MR May not work for Mixed X-S MR deadlocksdeadlocks

Page 62: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

6262

4. Change the Isolation 4. Change the Isolation LevelLevelFor single-resource 'conversion' For single-resource 'conversion'

deadlocksdeadlocks lower the isolation levellower the isolation level

For Mixed X-S MR deadlocks, use a For Mixed X-S MR deadlocks, use a locking hint on the Share lock queries:locking hint on the Share lock queries: Use READPAST when it is not important to Use READPAST when it is not important to

read uncommitted dataread uncommitted data Use NOLOCK when uncommited data must Use NOLOCK when uncommited data must

also be readalso be read

Page 63: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

6363

Additional ResourcesAdditional Resources

SQL Server Locking InternalsSQL Server Locking Internals Inside SQL Server 2000Inside SQL Server 2000, Kalen Delaney (MS Press), Kalen Delaney (MS Press)

Transaction Isolation Level ConceptsTransaction Isolation Level Concepts Transaction Processing: Concepts Transaction Processing: Concepts

and Techniques,and Techniques, Gray and Reuter (Morgan Kaufmann) Gray and Reuter (Morgan Kaufmann) http://http://research.microsoft.com/~gray/Isolation.docresearch.microsoft.com/~gray/Isolation.doc

Relevant Knowledge Base ArticlesRelevant Knowledge Base Articles Q224453 - INF: Understanding and Resolving SQL Server Q224453 - INF: Understanding and Resolving SQL Server

7.0 7.0 or 2000 Blocking Problems or 2000 Blocking Problems Q271509 - INF: How to Monitor SQL Server 2000 BlockingQ271509 - INF: How to Monitor SQL Server 2000 Blocking Q282749 - FIX: Deadlock Information Reported with SQL Q282749 - FIX: Deadlock Information Reported with SQL

Server 2000 Profiler Is Server 2000 Profiler Is IncorrectIncorrect

Page 64: Troubleshooting Deadlocks in SQL Server 2000

S231/S348 - Resolving Deadlocks in SQL Server 2000S231/S348 - Resolving Deadlocks in SQL Server 200011/22/2002 11/22/2002 16:00 - 17:30 16:00 - 17:30

6464

Thank you!Thank you!

Thank you for attending this session and Thank you for attending this session and PASS Community Summit - Seattle!PASS Community Summit - Seattle!

Please help us improve the quality of our Please help us improve the quality of our conference by completing your session conference by completing your session evaluation form. evaluation form.

Completed evaluation forms may be Completed evaluation forms may be given to the room monitor as you exit or given to the room monitor as you exit or to staff at the registration desk.to staff at the registration desk.

Page 65: Troubleshooting Deadlocks in SQL Server 2000

In association withIn association with

Produced byProduced by