SQL进程死锁排查

时间:2022-01-18 06:56:51

 1 --进程执行状态
 2 SELECT  
 3 der.[session_id],der.[blocking_session_id],  
 4 sp.lastwaittype,sp.hostname,sp.program_name,sp.loginame,  
 5 der.[start_time] AS 开始时间,  
 6 der.[status] AS 状态,  
 7 dest.[text] AS sql语句,  
 8 DB_NAME(der.[database_id]) AS 数据库名,  
 9 der.[wait_type] AS 等待资源类型,  
10 der.[wait_time] AS 等待时间,  
11 der.[wait_resource] AS 等待的资源,  
12 der.[logical_reads] AS 逻辑读次数  
13 FROM sys.[dm_exec_requests] AS der  
14 INNER JOIN master.dbo.sysprocesses AS sp ON der.session_id=sp.spid  
15 CROSS APPLY  sys.[dm_exec_sql_text](der.[sql_handle]) AS dest  
16 WHERE [session_id]>50 AND session_id<>@@SPID  
17 ORDER BY der.[session_id]  
18 GO  
19 
20 
21 
22 --是否堵塞  
23 SELECT spid,blocked,waittime,waittype,waitresource,p.dbid,cpu,physical_io,memusage,open_tran  
24 ,status,login_time,last_batch,hostname,program_name,hostprocess,loginame,cmd,text  
25 FROM master.dbo.sysprocesses p CROSS APPLY sys.dm_exec_sql_text(p.sql_handle) s  
26 WHERE blocked > 0 OR spid IN(SELECT blocked FROM master.dbo.sysprocesses WHERE blocked > 0)  
27 go   
28 
29 
30 -- 是否有未提交进程
31 select spid,blocked,waittime,waittype,waitresource,p.dbid,cpu,physical_io,memusage,open_tran    
32 ,status,login_time,last_batch,hostname,program_name,hostprocess,loginame,cmd,text    
33 from master.dbo.sysprocesses p cross apply sys.dm_exec_sql_text(p.sql_handle) s    
34 where open_tran <> 0    
35 go
36 
37 
38 -- 在执行进程状态
39 SELECT   DB_NAME(er.[database_id]) [DatabaseName]  
40 ,er.[command] AS [CommandType]
41 ,CONVERT(DECIMAL(5, 2), er.[percent_complete]) AS [Complete_Percent]
42 ,er.start_time
43 ,CONVERT(DECIMAL(38, 2), er.[total_elapsed_time] / 60000.00) AS [ElapsedTime_m]  
44 ,CONVERT(DECIMAL(38, 2), er.[estimated_completion_time] / 60000.00) AS [EstimatedCompletionTime_m]
45 ,CONVERT(DECIMAL(38, 2), (er.[total_elapsed_time] / 60000.00 * ((100-er.[percent_complete])/er.percent_complete))) AS [EstimatedCompletionTime_m2]
46 FROM sys.dm_exec_requests AS er  
47 WHERE 1=1
48 and er.[percent_complete] >0
49 --and er.[command] in (‘RESTORE DATABASE‘ ,‘BACKUP DATABASE‘,‘BACKUP LOG‘,‘RESTORE LOG‘)
50 order by er.start_time desc