//
you're reading...
SQL Server

Lead Blockers

I use this query on a daily basis, it will quickly identify the lead blocking queries along with the blocking tree.

NJOY!

——————————
— Blocking Tree
— Get lead blockers
— LDGALLER (C) 2012
——————————
drop table #Processes
go
SELECT s.spid, BlockingSPID = s.blocked, DatabaseName =
DB_NAME(s.dbid), 
s.program_name, s.loginame, ObjectName =
OBJECT_NAME(objectid,s.dbid), Definition = CAST(text AS VARCHAR(MAX))
INTO #Processes 
FROM sys.sysprocesses s
CROSS APPLY sys.dm_exec_sql_text (sql_handle)
WHERE s.spid > 50            — avoid system processes
go
WITH Blocking(SPID, BlockingSPID, BlockingStatement, RowNo, LevelRow) AS (SELECT s.SPID, s.BlockingSPID, s.Definition, ROW_NUMBER() OVER(ORDER BY s.SPID),0 AS LevelRow FROM Processes s 
JOIN #Processes s1 ON s.SPID = s1.BlockingSPID 

WHERE s.BlockingSPID =
UNION ALL 

SELECT 
r.SPID, r.BlockingSPID, r.Definition,d.RowNo,d.LevelRow +
FROM 
#Processes r 
JOIN Blocking d ON r.BlockingSPID = d.SPID 

WHERE 
r.BlockingSPID >)
SELECT FROM Blocking 
ORDER BY RowNo, LevelRow

Advertisements

About ldgaller

Accomplished Data Warehouse Architect, DBA and Software Architect with over 15 years of professional experience and demonstrated success designing and implementing solutions that improve business functionality and productivity. Highly diverse technical background with proven ability to design, develop and implement technology on an enterprise level. I approach all projects with passion, diligence, integrity, and exceptional aptitude.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: