SQL Server : Deadlocks

Deadlocks can be difficult to investigate and identify the causes for it as it does not happen often and can be difficult to reproduce it. Here are some ways to investigate the deadlock issues.

  1. You can run the SQL profiler with deadlock template but it is resource heavy and not a good idea to run on the production environments as it can cause the server to slow down. And you wouldn’t know when the next deadlock will happen.
  2. You can enable the xml_deadlock_report event in extended events. This will capture the information about the deadlocks. Then you can run the query below to get the information in XML format.

select xed.value('@timestamp', 'datetime') as Creation_Date
, xed.query('.') as Extend_Event
select cast([target_data] as xml) as Target_Data
from sys.dm_xe_session_targets as xt
join sys.dm_xe_sessions as xs on xs.address = xt.event_session_address
where xs.name = N'system_health'
and xt.target_name = N'ring_buffer'
) as XML_Data
cross apply Target_Data.nodes('RingBufferTarget/event[@name="xml_deadlock_report"]') as XEventData(xed)
order by Creation_Date desc

  1. You can turn the deadlock tracing on by running the following in query analyzer:

DBCC TRACEON (1204, -1)
DBCC TRACEON (1222, -1)

Then you can view the trace in SQL Server Agent –> Error Logs