查找阻塞(锁)链
SQL Server会话命令
SELECT r.session_id AS waiting_spid,
r.blocking_session_id AS blocking_spid,
r.wait_type,
r.wait_time / 1000.0 AS wait_sec,
r.status, r.command,
DB_NAME(r.database_id) AS db,
t.text AS waiting_query
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE r.blocking_session_id <> 0
ORDER BY r.wait_time DESC;
-- To find the head (root) blocker of the chain, look for the SPID whose
-- own blocking_session_id is not set (it is not blocked itself).作用
列出等待中的会话及阻塞它们的会话,以及等待时间和正在运行的查询;使锁链一目了然。
适用场景
在出现“应用挂起/超时”投诉时查找锁争用的根源。
注意
终止根阻塞者(未被任何会话阻塞但阻塞其他会话的 SPID)之前,请与业务负责人确认。仅查询;无风险。