|
|||||||||||||||||||
Lock Compatibility (Database Engine)
The following table shows the compatibility of
the most commonly encountered lock modes.
|
||||||
Existing granted mode
|
||||||
Requested
mode
|
IS
|
S
|
U
|
IX
|
SIX
|
X
|
Intent
shared (IS)
|
Yes
|
Yes
|
Yes
|
Yes
|
Yes
|
No
|
Shared
(S)
|
Yes
|
Yes
|
Yes
|
No
|
No
|
No
|
Update
(U)
|
Yes
|
Yes
|
No
|
No
|
No
|
No
|
Intent
exclusive (IX)
|
Yes
|
No
|
No
|
Yes
|
No
|
No
|
Shared
with intent exclusive (SIX)
|
Yes
|
No
|
No
|
No
|
No
|
No
|
Exclusive
(X)
|
No
|
No
|
No
|
No
|
No
|
No
|
Minimizing Deadlocks
Although deadlocks cannot be completely avoided, following certain coding conventions can minimize the chance of generating a deadlock. Minimizing deadlocks can increase transaction throughput and reduce system overhead because fewer transactions are:
- Rolled
back, undoing all the work performed by the transaction.
- Resubmitted
by applications because they were rolled back when deadlocked.
- Access
objects in the same order.
- Avoid
user interaction in transactions.
- Keep
transactions short and in one batch.
- Use
a lower isolation level.
- Use
a row versioning-based isolation level.
- Set READ_COMMITTED_SNAPSHOT database option ON to enable
read-committed transactions to use row versioning.
- Use snapshot isolation.
- Use
bound connections.
What is termed as a deadlock victim?
Process |
Comments |
Windows
Performance Monitor |
This provides all the deadlocks that have happened on
your server since the last restart. We can look at this counter using
the following SQL Statement: |
Object: SQLServer:
Locks |
SELECT
cntr_value AS NumOfDeadLocks |
Counter: Number
of Deadlocks/sec |
FROM sys.dm_os_performance_counters |
Instance: _Total |
WHERE
object_name = 'SQLServer:Locks' |
|
AND
counter_name = 'Number of Deadlocks/sec' |
|
AND
instance_name = '_Total' |
Trace
Flags1204 and 1222 |
Trace flag 1204 has existed since at least SQL Server
2000. And Trace Flag 1222 was introduced in SQL Server 2005.Both output
Deadlock Information to the SQL Server ERRORLOG. |
SQL
Server Profiler/Server Side Trace Event Class: Locks Event
Name: Deadlock Graph |
Gives
an XML Graph like the example above. Very easy to read and figure out
what is going on. |
Extended
Events |
The new way to do monitoring in SQL Server.
Extended Events are eventually going to replace the SQL Server Profiler all
together (i.e. SQL Server Profiler is on the Deprecated Feature list).
It produces the same XML graph as SQL Server Profiler, and is lighter in
performance impact |
System
Health |
This is a “new” default trace, but it’s unlike the
default trace that had limited the amount of information being tracked and
could not be modified. We can modify a system health definition, which
is built on a Extended Events. But unlike the default trace, the System
Health tracks the Deadlocks that have occurred recently. So we can pull
this information from the System Health for analysis instead of implementing
our own Extended Events monitoring. |
Deadlocks can be traced by turning on two specific flags:
-- Activate deadlock tracing
No comments:
Post a Comment