随笔- 31  评论- 4  文章- 0 

sqlserver查询是否阻塞

查询当前正在执行的语句

SELECT 
der.[session_id],der.[blocking_session_id], 
sp.lastwaittype,sp.hostname,sp.program_name,sp.loginame, 
der.[start_time] AS '开始时间', 
der.[status] AS '状态', 
dest.[text] AS 'sql语句', 
DB_NAME(der.[database_id]) AS '数据库名', 
der.[wait_type] AS '等待资源类型', 
der.[wait_time] AS '等待时间', 
der.[wait_resource] AS '等待的资源', 
der.[logical_reads] AS '逻辑读次数' 
FROM sys.[dm_exec_requests] AS der 
INNER JOIN master.dbo.sysprocesses AS sp ON der.session_id=sp.spid 
CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest 
--WHERE [session_id]>50 AND session_id<>@@SPID 
ORDER BY der.[session_id] 
GO

 


是否堵塞

SELECT spid,blocked,waittime,waittype,waitresource,p.dbid,cpu,physical_io,memusage,open_tran 
,status,login_time,last_batch,hostname,program_name,hostprocess,loginame,cmd,text 
FROM master.dbo.sysprocesses p CROSS APPLY sys.dm_exec_sql_text(p.sql_handle) s 
WHERE blocked > 0 OR spid IN(SELECT blocked FROM master.dbo.sysprocesses WHERE blocked > 0) 
go

 检查锁表

select   request_session_id   spid,OBJECT_NAME(resource_associated_entity_id) tableName
from   sys.dm_tran_locks t where resource_type='OBJECT' order by spid asc;

查询导致死锁的sql语句

dbcc inputbuffer(spid);

解锁

declare @spid  int 
Set @spid  = 123--锁表进程
declare @sql varchar(1000)
set @sql='kill '+cast(@spid  as varchar)
exec(@sql)

杀掉进程

kill spid

 

posted on 2019-05-24 13:10 zyz1 阅读(...) 评论(...) 编辑 收藏