当前位置:Gxlcms > 数据库问题 > SQL Server 日常维护--查询当前正在执行的语句、死锁、堵塞

SQL Server 日常维护--查询当前正在执行的语句、死锁、堵塞

时间:2021-07-01 10:21:17 帮助过:30人阅读

技术分享图片
 1 SELECT  
 2 der.[session_id],der.[blocking_session_id],  
 3 sp.lastwaittype,sp.hostname,sp.program_name,sp.loginame,  
 4 der.[start_time] AS 开始时间,  
 5 der.[status] AS 状态,  
 6 dest.[text] AS sql语句,  
 7 DB_NAME(der.[database_id]) AS 数据库名,  
 8 der.[wait_type] AS 等待资源类型,  
 9 der.[wait_time] AS 等待时间,  
10 der.[wait_resource] AS 等待的资源,  
11 der.[logical_reads] AS 逻辑读次数  
12 FROM sys.[dm_exec_requests] AS der  
13 INNER JOIN master.dbo.sysprocesses AS sp ON der.session_id=sp.spid  
14 CROSS APPLY  sys.[dm_exec_sql_text](der.[sql_handle]) AS dest  
15 --WHERE [session_id]>50 AND session_id<>@@SPID  
16 ORDER BY der.[session_id]  
17 GO  
View Code

是否堵塞:

技术分享图片
1 SELECT spid,blocked,waittime,waittype,waitresource,p.dbid,cpu,physical_io,memusage,open_tran 
2 ,status,login_time,last_batch,hostname,program_name,hostprocess,loginame,cmd,text 
3 FROM master.dbo.sysprocesses p CROSS APPLY sys.dm_exec_sql_text(p.sql_handle) s 
4 WHERE blocked > 0 OR spid IN(SELECT blocked FROM master.dbo.sysprocesses WHERE blocked > 0) 
5 go
View Code

是否有未提交事物:

技术分享图片
1 select spid,blocked,waittime,waittype,waitresource,p.dbid,cpu,physical_io,memusage,open_tran    
2 ,status,login_time,last_batch,hostname,program_name,hostprocess,loginame,cmd,text    
3 from master.dbo.sysprocesses p cross apply sys.dm_exec_sql_text(p.sql_handle) s    
4 where open_tran <> 0    
5 go 
View Code

 死锁: 

技术分享图片
1 --drop table #deadlock
2 
3 CREATE TABLE #deadlock(LogDate DATETIME,ProcessInfo VARCHAR(20),Text VARCHAR(2000))  
4 go  
5 INSERT INTO #deadlock EXEC xp_readerrorlog 0,1,deadlock victim,NULL,2016-07-20 14:00:00,2016-07-30,DESC    
6 go  
View Code

 

死锁加nolock优化

 

SQL Server 日常维护--查询当前正在执行的语句、死锁、堵塞

标签:master   _id   stat   proc   comment   hostname   deadlock   维护   gpo   

人气教程排行