原文地址:http://blog.csdn.net/kk185800961/article/details/42504857
方法一:Windows 性能计数器监控
命令行输入:perfmon 或者 perfmon /sys
选择实例:SQL Server :Locks \ Number of DeadLocks/sec \ _Total
(只能知道什么时候发生死锁,看不得具体死锁信息)
实时查看:
作业记录到文件:(可跟踪一段时间,耗系统性能很小)
方法二:打开profiler跟踪事件 locks: deadlock graph
(生成的死锁图可以拷贝出来XML格式的死锁信息)
或者导出上面为脚本在后台执行跟踪
-- 后台跟踪 declare @rc int declare @traceid int --跟踪分配的编号 declare @options int --TRACE_FILE_ROLLOVER=2/SHUTDOWN_ON_ERROR=4/TRACE_PRODUCE_BLACKBOX=8 declare @tracefile nvarchar(500)--跟踪文件的存储路径 declare @maxfilesize bigint --跟踪文件的大小,单位是mb,默认5mb declare @endtime datetime --停止跟踪的日期和时间,为NULL则表示一直跟踪 declare @filecount int --跟踪文件的数量,其值大于1,TRACE_FILE_ROLLOVER=2 时有效 set @options = 2 set @tracefile = N'E:BlockedTraceName' set @maxfilesize = 20 set @endtime = NULL set @filecount = 5 -- exec @rc = sp_trace_create @TraceID output, 0, N'InsertFileNameHere', @maxfilesize, NULL exec @rc = sp_trace_Create @TraceID output,@options,@tracefile,@maxfilesize,@endtime,@filecount if (@rc != 0) goto error -- Set the events declare @on bit set @on = 1 exec sp_trace_setevent @TraceID, 148, 1, @on exec sp_trace_setevent @TraceID, 148, 12, @on exec sp_trace_setevent @TraceID, 148, 14, @on exec sp_trace_setevent @TraceID, 148, 26, @on -- Set the Filters declare @intfilter int declare @bigintfilter bigint -- Set the trace status to start exec sp_trace_setstatus @TraceID, 1 -- display trace id for future references select TraceID=@TraceID goto finish error: select ErrorCode=@rc finish: go --查看跟踪记录 select ServerName,EventClass,StartTime,TextData,CONVERT(XML,TextData) XMLTextData from ::fn_trace_gettable('E:BlockedTraceName.trc',default)
方法三:打开1222或者1204标志记录死锁,在sqlserver日志查看
DBCC TRACEON(1222,-1) DBCC TRACEON(1204,-1)
如果日志太多就不好找了,这时可以用系统扩展存储过程筛选!
--查看是否死锁,确定死锁的的时间 exec xp_readerrorlog 0,1,'deadlock victim',NULL,'2015-01-01','2015-01-10','ASC' --按时间查看sqlserver日志,即为死锁信息 exec xp_readerrorlog 0,1,NULL,NULL,'2015-01-07 22:13:10','2015-01-07 22:13:11','ASC'
方法四:扩展事件
IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name = 'deadlock') DROP EVENT SESSION [deadlock] ON SERVER GO -- 跟踪的事件:database_xml_deadlock_report,xml_deadlock_report (跟踪一个即可,这里跟踪俩个) -- 选择跟踪列:client_hostname,database_name,nt_username,plan_handle,sql_text CREATE EVENT SESSION [deadlock] ON SERVER ADD EVENT sqlserver.database_xml_deadlock_report( ACTION(sqlserver.client_hostname,sqlserver.database_name,sqlserver.nt_username,sqlserver.plan_handle,sqlserver.sql_text)), ADD EVENT sqlserver.xml_deadlock_report( ACTION(sqlserver.client_hostname,sqlserver.database_name,sqlserver.nt_username,sqlserver.plan_handle,sqlserver.sql_text)) ADD TARGET package0.event_file( SET filename=N'D:Program FilesMicrosoft SQL Serverdeadlock', --存储路径及文件名前缀 max_file_size=(10), --最大文件大小,单位MB max_rollover_files=(4)) --启用文件滚动存储的最大文件数 GO ALTER EVENT SESSION [deadlock] ON SERVER STATE=START GO --查询跟踪 SELECT object_name as event,convert(xml, event_data) as xml_data FROM master.sys.fn_xe_file_target_read_file('D:Program FilesMicrosoft SQL Serverdeadlock*', NULL, NULL, NULL)
方法五:系统扩展事件会话system_health自动记录
--扩展事件会话的信息 select * from sys.dm_xe_sessions where name = 'system_health' SELECT xed.value('@timestamp','datetime')as Creation_Date, xed.query('.')AS Extend_Event FROM ( SELECT CAST([target_data] AS XML)AS Target_Data FROM sys.dm_xe_session_targets AS xt INNER 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
方法六:Service Broker Event Notifications
USE msdb; -- 队列用来保存数据 -- DROP QUEUE DeadlockQueue CREATE QUEUE DeadlockQueue WITH STATUS=ON GO -- 将消息传递到数据库中的正确队列 -- DROP SERVICE DeadlockService CREATE SERVICE DeadlockService ON QUEUE DeadlockQueue([http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]) GO -- 创建可向服务发送有关数据库或服务器事件的信息的对象 -- DROP EVENT NOTIFICATION CaptureDeadlocks ON SERVER CREATE EVENT NOTIFICATION CaptureDeadlocks ON SERVER WITH FAN_IN FOR DEADLOCK_GRAPH TO SERVICE 'DeadlockService','current database' ; GO --[此时发生死锁] -- 查询XML格式的死锁信息 SELECT CAST(message_body AS XML) AS message_body FROM DeadlockQueue