我目前正在使用这场灾难通过读取system_health
扩展事件环形缓冲区来定位最近的死锁。
select top 2000000000
XEvent.value('@timestamp', 'datetime2(3)') as CreationDateUtc,
--
-- Extract the <deadlock>...</deadlock> tag from within the event
-- Todo: Surely there is a better (xml) way to do this.
--
substring(convert(varchar(max), XEvent.query('.')),
-- start
patindex('%<deadlock%', convert(varchar(max), XEvent.query('.'))),
-- end
patindex('%</deadlock%', convert(varchar(max), XEvent.query('.'))) -
patindex('%<deadlock%', convert(varchar(max), XEvent.query('.'))) + 11 -- 11 to include for '</deadlock>'
) AS XdlFile
from
(
select cast (target_data as xml) as TargetData
from sys.dm_xe_session_targets st with (nolock)
join sys.dm_xe_sessions s with (nolock)
on s.address = st.event_session_address
where [name] = 'system_health'
and st.target_name = N'ring_buffer'
) as Data
cross apply TargetData.nodes('RingBufferTarget/event[@name="xml_deadlock_report"]') AS XEventData (XEvent)
order by CreationDateUtc desc
它工作正常,但是事件似乎不会持续很长时间(比如 24 小时?)我猜这是它的“环形缓冲区”部分。现在我偶然发现了一个链接,该链接正在读取system_health
具有类似信息的“文件”:
select event_data = CONVERT(XML, event_data)
from sys.fn_xe_file_target_read_file(N'system_health*.xel', NULL, NULL, NULL)
where event_data like '%xml_deadlock%'
该文件是否与环形缓冲区相同,但停留时间更长?使用文件有什么缺点吗?有一些 XML 技能的人想要转换顶级脚本吗?
目标是将 XdlFile 字段复制/粘贴到新文件中,并使用“文件打开”将其直接读入 SSMS 或 Sql Sentry Plan Explorer。
环形缓冲区链接:
https://www.sqlskills.com/blogs/jonathan/why-i-hate-the-ring_buffer-target-in-extended-events/
http://www.sqlskills.com/blogs/jonathan/multi-victim-deadlocks/
https://www.sqlskills.com/blogs/jonathan/graphically-viewing-extended-events-deadlock-graphs/
http://www.mssqltips.com/sqlservertip/1234/capturing-sql-server-deadlock-information-in-xml-format/
文件:
https://www.mssqltips.com/sqlservertip/3636/query-data-from-extended-events-in-sql-server/
@@version = Microsoft SQL Server 2012 (SP3-CU5) (KB3180915) - 11.0.6544.0 (X64)
这似乎有效: