Advertisement
Pandaaaa906

blocking_query

Jan 10th, 2022 (edited)
5,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.46 KB | None | 0 0
  1. WITH cteHead (client_net_address, program_name, session_id,request_id,wait_type,wait_resource,last_wait_type,is_user_process,request_cpu_time
  2. ,request_logical_reads,request_reads,request_writes,wait_time,blocking_session_id,memory_usage
  3. ,session_cpu_time,session_reads,session_writes,session_logical_reads
  4. ,percent_complete,est_completion_time,request_start_time,request_status,command
  5. ,plan_handle,sql_handle,statement_start_offset,statement_end_offset,most_recent_sql_handle
  6. ,session_status,group_id,query_hash,query_plan_hash)
  7. AS ( SELECT conn.client_net_address, sess.program_name, sess.session_id, req.request_id, LEFT(ISNULL (req.wait_type, ''), 50) AS 'wait_type'
  8.     , LEFT (ISNULL (req.wait_resource, ''), 40) AS 'wait_resource', LEFT (req.last_wait_type, 50) AS 'last_wait_type'
  9.     , sess.is_user_process, req.cpu_time AS 'request_cpu_time', req.logical_reads AS 'request_logical_reads'
  10.     , req.reads AS 'request_reads', req.writes AS 'request_writes', req.wait_time, req.blocking_session_id,sess.memory_usage
  11.     , sess.cpu_time AS 'session_cpu_time', sess.reads AS 'session_reads', sess.writes AS 'session_writes', sess.logical_reads AS 'session_logical_reads'
  12.     , CONVERT (DECIMAL(5,2), req.percent_complete) AS 'percent_complete', req.estimated_completion_time AS 'est_completion_time'
  13.     , req.start_time AS 'request_start_time', LEFT (req.STATUS, 15) AS 'request_status', req.command
  14.     , req.plan_handle, req.[sql_handle], req.statement_start_offset, req.statement_end_offset, conn.most_recent_sql_handle
  15.     , LEFT(sess.STATUS, 15) AS 'session_status', sess.group_id, req.query_hash, req.query_plan_hash
  16.     FROM sys.dm_exec_sessions AS sess
  17.     LEFT OUTER JOIN sys.dm_exec_requests AS req ON sess.session_id = req.session_id
  18.     LEFT OUTER JOIN sys.dm_exec_connections AS conn ON conn.session_id = sess.session_id
  19.     )
  20. , cteBlockingHierarchy (client_net_address, program_name, head_blocker_session_id, session_id, blocking_session_id, wait_type, wait_duration_ms,
  21. wait_resource, statement_start_offset, statement_end_offset, plan_handle, sql_handle, most_recent_sql_handle, [Level])
  22. AS ( SELECT head.client_net_address, head.program_name, head.session_id AS head_blocker_session_id, head.session_id AS session_id, head.blocking_session_id
  23.     , head.wait_type, head.wait_time, head.wait_resource, head.statement_start_offset, head.statement_end_offset
  24.     , head.plan_handle, head.sql_handle, head.most_recent_sql_handle, 0 AS [Level]
  25.     FROM cteHead AS head
  26.     WHERE (head.blocking_session_id IS NULL OR head.blocking_session_id = 0)
  27.     AND head.session_id IN (SELECT DISTINCT blocking_session_id FROM cteHead WHERE blocking_session_id != 0)
  28.     UNION ALL
  29.     SELECT blocked.client_net_address, blocked.program_name, h.head_blocker_session_id, blocked.session_id, blocked.blocking_session_id, blocked.wait_type,
  30.     blocked.wait_time, blocked.wait_resource, h.statement_start_offset, h.statement_end_offset,
  31.     h.plan_handle, h.sql_handle, h.most_recent_sql_handle, [Level] + 1
  32.     FROM cteHead AS blocked
  33.     INNER JOIN cteBlockingHierarchy AS h ON h.session_id = blocked.blocking_session_id AND h.session_id!=blocked.session_id --avoid infinite recursion for latch type of blocking
  34.     WHERE h.wait_type COLLATE Latin1_General_BIN NOT IN ('EXCHANGE', 'CXPACKET') OR h.wait_type IS NULL
  35.     )
  36. SELECT bh.*, txt.text AS blocker_query_or_most_recent_query
  37. FROM cteBlockingHierarchy AS bh
  38. OUTER APPLY sys.dm_exec_sql_text (ISNULL ([sql_handle], most_recent_sql_handle)) AS txt;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement