展开对象资源管理器->管理->扩展事件->会话->新建会话向导:
2:简介
输入扩展事件会话名称(勾选‘在服务器启动时启动事件会话’,否则下次服务器启动时,会话目标数据都是空的):
选择事件xml_deadlock_report,如图:
选中,将其移到右边‘所选事件’中,
收集个人所需要的信息,如:
右击该事件会话名称->‘属性’
在‘数据存储’选项卡处,点击‘添加’按钮,
选择event file事件目标:
--在一个窗口输入:
BEGIN TRAN --ROLLBACK
USE TEST;
UPDATE EMP2
SET ename='JIAOJIAO'
WHERE empno=2
WAITFOR DELAY '0:0:5';
UPDATE EMP2
SET ename='SONGSONG'
WHERE empno=4
--SELECT@@SPID 66
--在另一个窗口输入并执行:
BEGIN TRAN --ROLLBACK
USE TEST;
UPDATE EMP2
SET ename='SONGSONG'
WHERE empno=4
WAITFOR DELAY '0:0:5';
UPDATE EMP2
SET ename='JIAOJIAO'
WHERE empno=2
--SELECT@@SPID 61
右击该扩展事件目标package0.event_file->‘查看目标数据’,如图:
单击事件‘xml_deadlock_report’,
可以在下面‘详细信息’的’xml_report’看到详细的xml报告。
双击上面这一行,查看详细报告:
<deadlock>
<victim-list>
<victimProcessid="process3756f0cf8" />
</victim-list>
<process-list>
<processid="process3756f0cf8"taskpriority="0"logused="144"waitresource="KEY: 7:72057594050510848 (9d6bf8154a2a)"waittime="3726"ownerId="29210246"transactionname="user_transaction"lasttranstarted="2014-06-07T19:18:08.613"XDES="0x365f826c8"lockMode="X"schedulerid="2"kpid="11468"status="suspended"spid="61"sbid="0"ecid="0"priority="0"trancount="2"lastbatchstarted="2014-06-07T19:18:08.613"lastbatchcompleted="2014-06-07T19:17:57.943"lastattention="1900-01-01T00:00:00.943"clientapp="Microsoft SQL ServerManagement Studio - 查询"hostname="ZB-BAIDD-PC"hostpid="3388"loginname="SINOOCEANLAND\v-baidd"isolationlevel="read committed (2)"xactid="29210246"currentdb="7"lockTimeout="4294967295"clientoption1="671090784"clientoption2="390200">
<executionStack>
<frameprocname="adhoc"line="10"stmtstart="58"sqlhandle="0x02000000d9418b10cbc871e6336b29994a63088a4ca33f5e0000000000000000000000000000000000000000">
UPDATE [EMP2] set [ename] =@1 WHERE [empno]=@2 </frame>
<frameprocname="adhoc"line="10"stmtstart="258"sqlhandle="0x020000003e693606e108809864639179c3d61975fef1aee30000000000000000000000000000000000000000">
UPDATE EMP2
SET ename='JIAOJIAO'
WHERE empno=2
--SELECT @@SPID 61 </frame>
</executionStack>
<inputbuf>
--在另一个窗口输入并执行:
BEGIN TRAN --ROLLBACK
USE TEST;
UPDATE EMP2
SET ename='SONGSONG'
WHERE empno=4
WAITFOR DELAY '0:0:5';
UPDATE EMP2
SET ename='JIAOJIAO'
WHERE empno=2
--SELECT @@SPID 61
</inputbuf>
</process>
<processid="process371a07868"taskpriority="0"logused="332"waitresource="KEY: 7:72057594050510848 (1a39e6095155)"waittime="2875"ownerId="29210254"transactionname="user_transaction"lasttranstarted="2014-06-07T19:18:09.467"XDES="0x372890d28"lockMode="X"schedulerid="3"kpid="16592"status="suspended"spid="66"sbid="0"ecid="0"priority="0"trancount="2"lastbatchstarted="2014-06-07T19:18:09.467"lastbatchcompleted="2014-06-07T19:18:03.280"lastattention="1900-01-01T00:00:00.280"clientapp="Microsoft SQL ServerManagement Studio - 查询"hostname="ZB-BAIDD-PC"hostpid="3388"loginname="SINOOCEANLAND\v-baidd"isolationlevel="read committed (2)"xactid="29210254"currentdb="7"lockTimeout="4294967295"clientoption1="671090784"clientoption2="390200">
<executionStack>
<frameprocname="adhoc"line="7"stmtstart="58"sqlhandle="0x02000000d9418b10cbc871e6336b29994a63088a4ca33f5e0000000000000000000000000000000000000000">
UPDATE [EMP2] set [ename] =@1 WHERE [empno]=@2 </frame>
<frameprocname="adhoc"line="7"stmtstart="218"sqlhandle="0x02000000f0a53a0a1faf8c95858039e14cf0bad5536708520000000000000000000000000000000000000000">
UPDATE EMP2
SET ename='SONGSONG'
WHERE empno=4
--SELECT @@SPID 66 </frame>
</executionStack>
<inputbuf>
BEGIN TRAN --ROLLBACK
USE TEST;
UPDATE EMP2
SET ename='JIAOJIAO'
WHERE empno=2
WAITFOR DELAY '0:0:5';
UPDATE EMP2
SET ename='SONGSONG'
WHERE empno=4
--SELECT @@SPID 66
</inputbuf>
</process>
</process-list>
<resource-list>
<keylockhobtid="72057594050510848"dbid="7"objectname="test.dbo.EMP2"indexname="1"id="lock37ab40480"mode="X"associatedObjectId="72057594050510848">
<owner-list>
<ownerid="process371a07868"mode="X" />
</owner-list>
<waiter-list>
<waiterid="process3756f0cf8"mode="X"requestType="wait" />
</waiter-list>
</keylock>
<keylockhobtid="72057594050510848"dbid="7"objectname="test.dbo.EMP2"indexname="1"id="lock3724bb900"mode="X"associatedObjectId="72057594050510848">
<owner-list>
<ownerid="process3756f0cf8"mode="X" />
</owner-list>
<waiter-list>
<waiterid="process371a07868"mode="X"requestType="wait" />
</waiter-list>
</keylock>
</resource-list>
</deadlock>
在‘死锁’选项里可以看到死锁graph.
把鼠标放在图表两个圆圈上面,查看具体的语句,如放在作为牺牲品的圆圈上面:
在这里利用event_file目标,能看到针对某个死锁的xml报告及图表。
可以结合ring_buffer目标,查看所有死锁报告历史。
备注:本篇文章翻译自文章‘How to monitor deadlock using extended events in SQL Server 2008 and later',并做了些许改动。