
Tag : SQL,Waits,SQL Server
SET nocount ON;
WHILE 1 = 1
BEGIN
DECLARE @results TABLE
(
[id] INT IDENTITY NOT NULL PRIMARY KEY,
[blocked module] NVARCHAR(776) NOT NULL,
[blocked statement] NVARCHAR(max) NOT NULL,
[blocking module] NVARCHAR(776) NULL,
[blocking statement] NVARCHAR(max) NULL,
[wait type] NVARCHAR(60) NOT NULL,
[total wait time (ms)] BIGINT NOT NULL,
[sessions waiting (count)] INT NOT NULL
);
DELETE FROM @results;
WITH statements ([blocked module], [blocked statement start],
[blocked statement end]
, [blocking module], [blocking statement start],
[blocking statement end],
[wait type], [total wait time (ms)], [sessions waiting (count)])
AS (SELECT BlockedR.sql_handle AS
[Blocked Module],
BlockedR.statement_start_offset AS
[Blocked Statement start],
BlockedR.statement_end_offset AS
[Blocked Statement end],
BlockingR.sql_handle AS
[Blocking Module],
BlockingR.statement_start_offset AS
[Blocking statement start],
BlockingR.statement_end_offset AS
[Blocking statement end]
,
WT.wait_type AS [Wait type],
Sum(CONVERT(BIGINT, WT.wait_duration_ms)) AS
[Total wait time (MS)],
Count(*) AS
[Sessions waiting (count)]
FROM sys.dm_os_waiting_tasks AS WT
INNER JOIN sys.dm_exec_requests AS BlockedR
ON BlockedR.session_id = WT.session_id
LEFT OUTER JOIN sys.dm_exec_requests AS BlockingR
ON BlockingR.session_id =
WT.blocking_session_id
GROUP BY BlockedR.sql_handle,
BlockedR.statement_start_offset,
BlockedR.statement_end_offset,
BlockingR.sql_handle,
BlockingR.statement_start_offset,
BlockingR.statement_end_offset,
WT.wait_type)
INSERT INTO @results
([blocked module],
[blocked statement],
[blocking module],
[blocking statement],
[wait type],
[total wait time (ms)],
[sessions waiting (count)])
SELECT COALESCE(Quotename(Db_name(BlockedText.dbid)), N'-')
+ N'.'
+ COALESCE(Quotename(Object_schema_name(BlockedText.objectid,
BlockedText.dbid)), N'-')
+ N'.'
+ COALESCE(Quotename(Object_name(BlockedText.objectid,
BlockedText.dbid)), N'-')
AS
[Blocked module],
Substring(BlockedText.text, ( S.[blocked statement start] / 2 ) + 1
,
( ( CASE S.[blocked statement end]
WHEN -1 THEN Datalength(BlockingText.text)
ELSE S.[blocked statement end]
END - S.[blocked statement start] ) / 2 ) + 1) AS
[Blocked statement]
,
COALESCE(Quotename(Db_name(BlockingText.dbid)), N'-')
+ N'.'
+ COALESCE(Quotename(Object_schema_name(BlockingText.objectid,
BlockingText.dbid)), N'-')
+ N'.'
+ COALESCE(Quotename(Object_name(BlockingText.objectid,
BlockingText.dbid)),
N'-') AS
[Blocking module],
Substring(BlockingText.text, ( S.[blocking statement start] / 2 ) +
1
, (
( CASE
S.[blocking statement end]
WHEN -1 THEN Datalength(BlockingText.text)
ELSE S.[blocking statement end]
END - S.[blocking statement start] ) / 2 ) + 1) AS
[Blocking statement],
S.[wait type] AS [Wait type],
S.[total wait time (ms)] AS
[Total wait time (MS)],
S.[sessions waiting (count)] AS
[Sessions waiting (count)]
FROM statements AS S
CROSS apply sys.Dm_exec_sql_text(S.[blocked module]) AS BlockedText
CROSS apply sys.Dm_exec_sql_text(S.[blocking module]) AS
BlockingText
--where S.[Total wait time (MS)] > 50
ORDER BY S.[total wait time (ms)] DESC
OPTION (force ORDER);
IF EXISTS (SELECT *
FROM @results)
BEGIN
SELECT Getdate(),
[blocked module],
[blocked statement],
[blocking module],
[blocking statement],
[wait type],
[total wait time (ms)],
[sessions waiting (count)]
FROM @results;
END;
WAITFOR delay '00:00:01'
END;