Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

--https://www.dbrnd.

com/2016/12/sql-server-script-to-find-waiting-queries-which-
are-block-by-other-running-queries-find-block-transaction/

SELECT dowt.session_id ,dese.last_request_start_time ,dese.status,


dese.program_name ,dese.host_name as HostName, DB_NAME(dest.dbid) 'Database' ,
dowt.wait_duration_ms ,dowt.wait_type ,dowt.blocking_session_id ,
der.command ,der.percent_complete ,der.cpu_time ,der.total_elapsed_time ,
der.reads ,der.writes ,der.logical_reads ,der.row_count ,dest.text AS
QueryText, deqp.query_plan ,der.plan_handle ,dowt.resource_description
FROM sys.dm_os_waiting_tasks as dowt
INNER JOIN sys.dm_exec_sessions as dese ON dowt.session_id = dese.session_id
INNER JOIN sys.dm_exec_requests as der ON dese.session_id = der.session_id
CROSS APPLY sys.dm_exec_sql_text(der.plan_handle) as dest
CROSS APPLY sys.dm_exec_query_plan(der.plan_handle) as deqp
WHERE dowt.session_id > 50

===================================================================================
===================================================================================
========================

--BLOCKING TREE http://blog.sqlauthority.com/2015/07/07/sql-server-identifying-


blocking-chain-using-sql-scripts/
SET NOCOUNT ON;
SET CONCAT_NULL_YIELDS_NULL OFF
GO

--BLOCKING TREE http://blog.sqlauthority.com/2015/07/07/sql-server-identifying-


blocking-chain-using-sql-scripts/
SET NOCOUNT ON;
SET CONCAT_NULL_YIELDS_NULL OFF
GO
--spid, blocked, waittype, waittime, lastwaittype, dbid, cpu, physical_io,
memusage, last_batch, status, hostname, program_name, cmd, loginame
WITH BLOCKERS (SPID, BLOCKED, LEVEL, BATCH,
waittype, Status, lastwaittype, waittime, Hostname, Loginame,
Applicationname, DBname, Command, cpu, memusage, physical_io, lastbatch)
AS
(
SELECT SPID, BLOCKED,
CAST (REPLICATE ('0', 4-LEN (CAST (SPID AS VARCHAR))) + CAST (SPID AS VARCHAR)
AS VARCHAR (1000)) AS LEVEL,
REPLACE (REPLACE (T.TEXT, CHAR(10), ' '), CHAR (13), ' ' ) AS BATCH,
R.waittype, R.status, R.lastwaittype, R.waittime, R.hostname, R.loginame,
R.program_name, db_name(r.dbid) DBname, cmd, R.cpu, R.memusage,
R.physical_io,R.last_batch
FROM sys.sysprocesses R with (nolock)
CROSS APPLY SYS.DM_EXEC_SQL_TEXT(R.SQL_HANDLE) T WHERE (BLOCKED = 0 OR BLOCKED =
SPID)
AND EXISTS (SELECT SPID,BLOCKED,CAST (REPLICATE ('0', 4-LEN (CAST (SPID AS
VARCHAR))) + CAST (SPID AS VARCHAR) AS VARCHAR (1000)) AS LEVEL,
BLOCKED, REPLACE (REPLACE (T.TEXT, CHAR(10), ' '), CHAR (13), ' ' ) AS
BATCH,R.waittype,R.lastwaittype FROM sys.sysprocesses R2 with (nolock)
CROSS APPLY SYS.DM_EXEC_SQL_TEXT(R.SQL_HANDLE) T WHERE R2.BLOCKED = R.SPID AND
R2.BLOCKED <> R2.SPID)
UNION ALL
SELECT R.SPID, R.BLOCKED,
CAST (BLOCKERS.LEVEL + RIGHT (CAST ((1000 + R.SPID) AS VARCHAR (100)), 4) AS
VARCHAR (1000)) AS LEVEL,
REPLACE (REPLACE (T.TEXT, CHAR(10), ' '), CHAR (13), ' ' ) AS BATCH,
R.waittype, R.status, R.lastwaittype, R.waittime, R.hostname, R.loginame,
R.program_name, db_name(r.dbid) DBname, cmd, R.cpu, R.memusage,
R.physical_io,R.last_batch
FROM sys.sysprocesses AS R with (nolock)
CROSS APPLY SYS.DM_EXEC_SQL_TEXT(R.SQL_HANDLE) T
INNER JOIN BLOCKERS ON R.BLOCKED = BLOCKERS.SPID WHERE R.BLOCKED > 0 AND
R.BLOCKED <> R.SPID
)

SELECT N' ' + REPLICATE (N'| ', LEN (LEVEL)/4 - 2) + CASE WHEN (LEN
(LEVEL)/4 - 1) = 0 THEN 'HEAD - ' ELSE '|------ ' END + CAST (SPID AS VARCHAR (10))
+ ' ' +
BATCH AS BLOCKING_TREE , --waittype ,
status, lastwaittype, waittime, Hostname, Loginame, Applicationname, DBname,
Command, CPU, MemUsage, physical_io, LastBatch, GETDATE() as CurrentTime
FROM BLOCKERS with (nolock) ORDER BY LEVEL ASC

===================================================================================
===================================================================================
========================

--DETECT BLOCKING
SELECT wt.session_id,
ot.task_state,
wt.wait_type,
wt.wait_duration_ms,
wt.blocking_session_id,
wt.resource_description,
es.[host_name],
es.[program_name]
FROM sys.dm_os_waiting_tasks wt
INNER JOIN sys.dm_os_tasks ot ON ot.task_address = wt.waiting_task_address
INNER JOIN sys.dm_exec_sessions es ON es.session_id = wt.session_id
WHERE es.is_user_process = 1

-----------------------------------------------------------------------------------
-----------------------

SELECT BLOCKER.spid Blocker_SPID, Blocked.spid Blocked_SPID,


--RTRIM(convert(varchar(128),BLOCKER.context_info)) BLOCKER_CONTEXT, CASE
BLOCKER.blocked WHEN 0 THEN 'Lead Blocker' ELSE 'In Blocking Chain' END
Blocker_Status,
--RTRIM(convert(varchar(128),BLOCKED.context_info)) BLOCKED_CONTEXT,
BLOCKED.waittime,
CASE CONVERT(TINYINT, BLOCKED.waittype)
WHEN 1 THEN 'SCH-ST' WHEN 2 THEN 'SCH-MOD' WHEN 3 THEN 'S' WHEN 4 THEN 'U'
WHEN 5 THEN 'X'
WHEN 6 THEN 'IS' WHEN 7 THEN 'IU' WHEN 8 THEN 'IX' WHEN 9 THEN 'SIU' WHEN 10 THEN
'SIX'
WHEN 11 THEN 'UIX' WHEN 12 THEN 'BU' WHEN 13 THEN 'RangeS-S' WHEN 14 THEN
'RangeS-U' WHEN 15 THEN 'RangeIn-Null'
WHEN 16 THEN 'RangeIn-S' WHEN 17 THEN 'RangeIn-U' WHEN 18 THEN 'RangeIn-X'
WHEN 19 THEN 'RangeX-S' WHEN 20 THEN 'RangeX-U' WHEN 21 THEN 'RangeX-X'
ELSE 'UNKNOWN' END Lock_Mode,
SUBSTRING(BLOCKED.waitresource,1,3) Lock_Resource_Type, DB_NAME(BLOCKED.dbid)
'DBName',
--SUBSTRING(BLOCKED.waitresource,6,30) LOCK_RESOURCE, BLOCKER.sql_handle
sql_handle1, BLOCKED.cmd 'BLOCKED cmd',
--,BLOCKED.physical_io 'BLOCKED physical_io'
BLOCKER.hostname 'Blocker Hostname',
LTRIM(RTRIM(BLOCKER.program_name)) 'Blocker_AppName',
LTRIM(RTRIM(BLOCKER.loginame)) 'Blocker Loginame',
LTRIM(RTRIM(BLOCKER.cpu)) 'Blocker CPU',
--BLOCKER.physical_io 'BLOCKER physical_io',
LTRIM(RTRIM(BLOCKED.hostname)) 'Blocked Hostname',
LTRIM(RTRIM(BLOCKED.program_name)) 'Blocked_AppName',
LTRIM(RTRIM(BLOCKED.loginame)) 'Blocked LoginName',
LTRIM(RTRIM(BLOCKED.cpu)) 'Blocked CPU',
Getdate() 'Captured time',
h1.text 'Blocker_SQL', -- You can get SP Name here
SUBSTRING (h1.text,BLOCKER.stmt_start/2, (CASE WHEN BLOCKER.stmt_end = -1
THEN LEN(CONVERT(NVARCHAR(MAX), h1.text)) * 2
ELSE BLOCKER.stmt_end END - BLOCKER.stmt_start)/2) AS
'Blocker_indivudual_query',
--BLOCKER.cmd 'BLOCKER cmd',
--BLOCKED.sql_handle,
h2.text 'Blocked_SQL',-- -- You can get SP Name here
SUBSTRING (h2.text,BLOCKED.stmt_start/2, (CASE WHEN BLOCKED.stmt_end = -1
THEN LEN(CONVERT(NVARCHAR(MAX), h2.text)) * 2
ELSE BLOCKED.stmt_end END - BLOCKED.stmt_start)/2) AS
'Blocked_indivudual_query'

FROM master..sysprocesses BLOCKER


JOIN master..sysprocesses BLOCKED ON BLOCKER.spid = BLOCKED.blocked
CROSS APPLY sys.dm_exec_sql_text(BLOCKER.sql_handle) AS h1
CROSS APPLY sys.dm_exec_sql_text(BLOCKED.sql_handle) AS h2
WHERE BLOCKED.blocked <> 0

===================================================================================
===================================================================================
========================

Recommendation:
---------------

Use NOLOCK hint in SELECT query to avoid this blocking scenario in future.
- The NOLOCK hint allows SQL to read data from tables by ignoring any locks and
therefore not being blocked by other processes. This can improve query performance,
but also introduces the possibility of dirty reads.

OR

Use ROWLOCK hint in UPDATE/INSERT/DELETE Statements to minimize the blockings.


- The ROWLOCK table hint can be used with either INSERT, UPDATE, and DELETE
statements, to instruct the server to only apply a Range-Lock(s) on the rows being
modified or added,
and to avoid escalating the lock to page or table level. The rest of rows are not
locked and can be accessed by another query.

You might also like