Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0af6587

Browse files
Updated BlockingTree Script
Updated BlockingTree Script
1 parent 77d0fc0 commit 0af6587

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

‎.vs/SQLDBA-SSMS Solution/v15/.ssms_suo

0 Bytes
Binary file not shown.

‎Blocking Alert/BlockingTree-Live-WhoIsActive.sql

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ USE tempdb;
22

33
DECLARE @s VARCHAR(MAX), @t VARCHAR(MAX);
44
DECLARE @tableName varchar(255);
5-
DECLARE @DeleteFlag bit = 0; /* 0 = drop, 1 = truncate, 2 = No Action */
5+
DECLARE @DeleteFlag tinyint; /* 0 = drop, 1 = truncate, 2 = No Action */
6+
SET @DeleteFlag = 2;
67
SET @tableName = 'tempdb.dbo.WhoIsActive_ResultSet';
78

89
EXEC sp_WhoIsActive @sort_order = '[start_time] ASC', @get_outer_command=1, @find_block_leaders=1 --,@get_full_inner_text=1
@@ -13,14 +14,15 @@ EXEC sp_WhoIsActive @sort_order = '[start_time] ASC', @get_outer_command=1, @fin
1314

1415
SET @s = REPLACE(@s, '<table_name>', @tableName)
1516

16-
IF(@DeleteFlag = 0)
17+
IF@DeleteFlag = 0
1718
SET @t = 'IF OBJECT_ID('''+@tableName+''') IS NOT NULL DROP TABLE '+@tableName;
18-
IF(@DeleteFlag = 1)
19+
IF@DeleteFlag = 1
1920
SET @t = 'IF EXISTS(SELECT * FROM '+@tableName+') TRUNCATE TABLE '+@tableName;
20-
IF(@DeleteFlag = 2)
21+
IF@DeleteFlag = 2
2122
SET @t = '-- ignore';
2223

2324
EXEC(@t);
25+
PRINT @t;
2426
SET @s = 'IF OBJECT_ID('''+@tableName+''') IS NULL '+@s;
2527
EXEC(@s);
2628

@@ -31,8 +33,7 @@ EXEC sp_WhoIsActive @sort_order = '[start_time] ASC', @get_outer_command=1, @fin
3133
--,@get_plans=1 /* 1 = current query, 2 = entire batch */
3234
,@destination_table = @tableName;
3335

34-
--select r.*
35-
--from dbo.WhoIsActive_ResultSet as r; -- @tableName
36+
select distinct [collection_time] from dbo.WhoIsActive_ResultSet as r; -- @tableName
3637
--go
3738

3839

@@ -66,15 +67,29 @@ EXEC sp_WhoIsActive @sort_order = '[start_time] ASC', @get_outer_command=1, @fin
6667
)
6768
--select * from T_BLOCKERS
6869

69-
SELECT [BLOCKING_TREE] = N' ' + REPLICATE (N'| ', LEN (LEVEL)/4 - 1)
70+
SELECT r.[collection_time],
71+
[BLOCKING_TREE] = N' ' + REPLICATE (N'| ', LEN (LEVEL)/4 - 1)
7072
+ CASE WHEN (LEN(LEVEL)/4 - 1) = 0
7173
THEN 'HEAD - '
7274
ELSE '|------ '
7375
END
7476
+ CAST (r.session_id AS NVARCHAR (10)) + N' ' + (CASE WHEN LEFT(r.[batch_text],1) = '(' THEN SUBSTRING(r.[batch_text],CHARINDEX('exec',r.[batch_text]),LEN(r.[batch_text])) ELSE r.[batch_text] END),
75-
r.[dd hh:mm:ss.mss], r.[session_id], r.[sql_text], r.[sql_command], r.[login_name], r.[wait_info], r.[tempdb_allocations], r.[tempdb_current],
76-
r.[blocking_session_id], r.[blocked_session_count], r.[reads], r.[writes], r.[physical_reads], r.[CPU], r.[used_memory], r.[status], r.[open_tran_count],
77-
r.[percent_complete], r.[host_name], r.[database_name], r.[program_name], r.[start_time], r.[login_time], r.[request_id], r.[collection_time]
77+
r.[dd hh:mm:ss.mss], r.[wait_info], r.[blocked_session_count], r.[blocking_session_id], --r.[sql_text], r.[sql_command],
78+
r.[login_name], r.[host_name], r.[database_name], r.[program_name], r.[tempdb_allocations], r.[tempdb_current],
79+
r.[reads], r.[writes], r.[physical_reads], r.[CPU], r.[used_memory], r.[status], r.[open_tran_count],
80+
r.[percent_complete], r.[start_time], r.[login_time], r.[request_id]
7881
FROM T_BLOCKERS AS r
7982
ORDER BY collection_time, LEVEL ASC;
8083

84+
select @@servername as srvName, r.login_name, r.program_name, r.database_name, count(r.session_id) as session_counts
85+
from tempdb.dbo.WhoIsActive_ResultSet AS r
86+
group by r.login_name, r.program_name, r.database_name
87+
having count(r.session_id) > (select count(distinct [collection_time]) from dbo.WhoIsActive_ResultSet as r)
88+
order by session_counts desc
89+
go
90+
91+
/*
92+
delete from tempdb.dbo.WhoIsActive_ResultSet
93+
where collection_time not in ('2020年07月10日 15:52:58.490')
94+
*/
95+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /