SQL Server扩展事件(Extended Events)-- 使用扩展事件跟踪监控死锁脚本实现

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介:

SQL Server扩展事件(Extended Events)-- 使用扩展事件跟踪监控死锁脚本实现

1
2
3
4
5
6
7
8
-- Create a new event session (it is better to create a new session and not modify the system’s built-in session “system_health”):
CREATE  EVENT SESSION [Deadlock_Monitor]  ON  SERVER
ADD  EVENT sqlserver.xml_deadlock_report
ADD  TARGET package0.asynchronous_file_target
( SET  filename= N 'C:\temp\deadlock.xel'  )
WITH
(MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=10 SECONDS,
MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY= OFF ,STARTUP_STATE= ON )

 

1
2
-- Enable the session to start capturing events:
ALTER  EVENT SESSION [Deadlock_Monitor]  ON  SERVER STATE = start;

 

1
2
-- To see how many deadlocks have been captured by the session since it started running, you can run this query:
select  COUNT (*)  from  sys.fn_xe_file_target_read_file ( 'c:\temp\deadlock*.xel' 'c:\temp\deadlock*.xem' null null )

 

1
2
3
4
5
6
-- To get a list of the captured deadlocks and their graphs you can execute this query:
select  xml_data.value( '(event[@name="xml_deadlock_report"]/@timestamp)[1]' , 'datetime' ) Execution_Time,
xml_data.value( '(event/data/value)[1]' , 'varchar(max)' ) Query
from  ( select  object_name  as  event,  CONVERT (xml, event_data)  as  xml_data
from  sys.fn_xe_file_target_read_file
( 'c:\temp\deadlock*.xel' 'c:\temp\deadlock*.xem' null null )) v  order  by  Execution_Time

 

1
2
-- If you want the session to stop capturing events (until you enable the session again), you can use this query:
ALTER  EVENT SESSION [Deadlock_Monitor]  ON  SERVER STATE = stop;

 

1
2
-- If you want to completely remove (delete) the session from the server, you can use this query:
DROP  EVENT SESSION [Deadlock_Monitor]  ON  SERVER

 

1
2
3
4
5
6
7
-- If you want to configure other events too, you can query these tables to find out what you can trace and how:
--Show the possible targets:
SELECT  xp.[ name ], xo.*
FROM  sys.dm_xe_objects xo, sys.dm_xe_packages xp
WHERE  xp.[guid] = xo.[package_guid]
AND  xo.[object_type] =  'target'
ORDER  BY  xp.[ name ];

 

1
2
3
4
5
6
--Show the possible actions
SELECT  xp.[ name ], xo.*
FROM  sys.dm_xe_objects xo, sys.dm_xe_packages xp
WHERE  xp.[guid] = xo.[package_guid]
AND  xo.[object_type] =  'action'
ORDER  BY  xp.[ name ], xo.[ name ];





















本文转自UltraSQL51CTO博客,原文链接:http://blog.51cto.com/ultrasql/1600520 ,如需转载请自行联系原作者


相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
相关文章
|
3月前
|
SQL IDE Java
Java连接SQL Server数据库的详细操作流程
Java连接SQL Server数据库的详细操作流程
|
2月前
|
SQL 存储 监控
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
49 13
|
2月前
|
SQL
解锁 SQL Server 2022的时间序列数据功能
【7月更文挑战第14天】要解锁SQL Server 2022的时间序列数据功能,可使用`generate_series`函数生成整数序列,例如:`SELECT value FROM generate_series(1, 10)。此外,`date_bucket`函数能按指定间隔(如周)对日期时间值分组,这些工具结合窗口函数和其他时间日期函数,能高效处理和分析时间序列数据。更多信息请参考官方文档和技术资料。
|
2月前
|
SQL 存储 网络安全
关系数据库SQLserver 安装 SQL Server
【7月更文挑战第26天】
40 6
|
17天前
|
SQL 安全 Java
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client
80 0
|
2月前
|
存储 SQL C++
对比 SQL Server中的VARCHAR(max) 与VARCHAR(n) 数据类型
【7月更文挑战7天】SQL Server 中的 VARCHAR(max) vs VARCHAR(n): - VARCHAR(n) 存储最多 n 个字符(1-8000),适合短文本。 - VARCHAR(max) 可存储约 21 亿个字符,适合大量文本。 - VARCHAR(n) 在处理小数据时性能更好,空间固定。 - VARCHAR(max) 对于大文本更合适,但可能影响性能。 - 选择取决于数据长度预期和业务需求。
|
2月前
|
SQL Oracle 关系型数据库
MySQL、SQL Server和Oracle数据库安装部署教程
数据库的安装部署教程因不同的数据库管理系统(DBMS)而异,以下将以MySQL、SQL Server和Oracle为例,分别概述其安装部署的基本步骤。请注意,由于软件版本和操作系统的不同,具体步骤可能会有所变化。
93 3
|
2月前
|
SQL 存储 安全
数据库数据恢复—SQL Server数据库出现逻辑错误的数据恢复案例
SQL Server数据库数据恢复环境: 某品牌服务器存储中有两组raid5磁盘阵列。操作系统层面跑着SQL Server数据库,SQL Server数据库存放在D盘分区中。 SQL Server数据库故障: 存放SQL Server数据库的D盘分区容量不足,管理员在E盘中生成了一个.ndf的文件并且将数据库路径指向E盘继续使用。数据库继续运行一段时间后出现故障并报错,连接失效,SqlServer数据库无法附加查询。管理员多次尝试恢复数据库数据但是没有成功。
|
2月前
|
SQL 存储 关系型数据库
关系型数据库SQL Server学习
【7月更文挑战第4天】
38 2
|
3月前
|
SQL 存储 测试技术
下一篇
云函数