1. 程式人生 > >死鎖跟蹤:6種跟蹤死鎖的方法總結

死鎖跟蹤:6種跟蹤死鎖的方法總結

時間 mda msd count avi filesize microsoft win int

原文地址: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 = NE:\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=ND:\Program Files\Microsoft SQL Server\deadlock, --存儲路徑及文件名前綴  
    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 Files\Microsoft SQL Server\deadlock*, 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=Nsystem_health    
    AND xt.target_name=Nring_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  

技術分享
技術分享

死鎖跟蹤:6種跟蹤死鎖的方法總結