Skip to content

Commit e7405ca

Browse files
committed
chore: format three IF blocks
1 parent 3eb5c36 commit e7405ca

1 file changed

Lines changed: 77 additions & 48 deletions

File tree

sp_HumanEvents/sp_HumanEvents.sql

Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,19 @@ DECLARE
515515
@drop_holder nvarchar(max) = N'',
516516
@cleanup_views nvarchar(max) = N'',
517517
@nc10 nvarchar(2) = NCHAR(10),
518-
@inputbuf_bom nvarchar(1) = CONVERT(nvarchar(1), 0x0a00, 0);
518+
@inputbuf_bom nvarchar(1) = CONVERT(nvarchar(1), 0x0a00, 0),
519+
@wait_event_type_pattern nvarchar(6) = N'%wait%',
520+
@lock_event_type_pattern nvarchar(6) = N'%lock%',
521+
@query_event_type_pattern nvarchar(6) = N'%quer%',
522+
@compile_event_type_pattern nvarchar(6) = N'%comp%',
523+
@recompile_exclude_event_type_pattern nvarchar(4) = N'%re%',
524+
@recompile_event_type_pattern nvarchar(8) = N'%recomp%',
525+
@is_wait_event_type bit = 0,
526+
@is_lock_event_type bit = 0,
527+
@is_query_event_type bit = 0,
528+
@is_compile_event_type bit = 0,
529+
@is_compile_any_event_type bit = 0,
530+
@is_recompile_event_type bit = 0;
519531

520532
/*check to make sure we're on a usable version*/
521533
IF
@@ -854,6 +866,18 @@ What on earth is %s?', 11, 1, @event_type) WITH NOWAIT;
854866
RETURN;
855867
END;
856868

869+
SELECT
870+
@is_lock_event_type = CASE WHEN @event_type LIKE @lock_event_type_pattern THEN 1 ELSE 0 END,
871+
@is_query_event_type = CASE WHEN @event_type LIKE @query_event_type_pattern THEN 1 ELSE 0 END,
872+
@is_recompile_event_type = CASE WHEN @event_type LIKE @recompile_event_type_pattern THEN 1 ELSE 0 END,
873+
@is_compile_any_event_type = CASE WHEN @event_type LIKE @compile_event_type_pattern THEN 1 ELSE 0 END,
874+
@is_wait_event_type = CASE WHEN @event_type LIKE @wait_event_type_pattern THEN 1 ELSE 0 END,
875+
@is_compile_event_type = CASE
876+
WHEN @event_type LIKE @compile_event_type_pattern
877+
AND @event_type NOT LIKE @recompile_exclude_event_type_pattern THEN 1
878+
ELSE 0
879+
END;
880+
857881

858882
IF @debug = 1 BEGIN RAISERROR(N'Checking query sort order', 0, 1) WITH NOWAIT; END;
859883
IF @query_sort_order NOT IN
@@ -984,7 +1008,7 @@ IF
9841008
(
9851009
@wait_type <> N''
9861010
AND @wait_type <> N'ALL'
987-
AND LOWER(@event_type) NOT LIKE N'%wait%'
1011+
AND @is_wait_event_type = 0
9881012
)
9891013
BEGIN
9901014
RAISERROR(N'You can''t filter on wait stats unless you use the wait stats event.', 11, 1) WITH NOWAIT;
@@ -998,7 +1022,7 @@ IF @debug = 1 BEGIN RAISERROR(N'Are we trying to filter for a blocking session?'
9981022
/* blocking events need a database name to resolve objects */
9991023
IF
10001024
(
1001-
LOWER(@event_type) LIKE N'%lock%'
1025+
@is_lock_event_type = 1
10021026
AND DB_ID(@database_name) IS NULL
10031027
AND @object_name <> N''
10041028
)
@@ -1010,7 +1034,7 @@ END;
10101034
/* but could we resolve the object name? */
10111035
IF
10121036
(
1013-
LOWER(@event_type) LIKE N'%lock%'
1037+
@is_lock_event_type = 1
10141038
AND @object_name <> N''
10151039
AND OBJECT_ID(@fully_formed_babby) IS NULL
10161040
)
@@ -1021,7 +1045,7 @@ END;
10211045

10221046
/* no blocked process report, no love */
10231047
IF @debug = 1 BEGIN RAISERROR(N'Validating if the Blocked Process Report is on, if the session is for blocking', 0, 1) WITH NOWAIT; END;
1024-
IF @event_type LIKE N'%lock%'
1048+
IF @is_lock_event_type = 1
10251049
AND EXISTS
10261050
(
10271051
SELECT
@@ -1165,10 +1189,13 @@ END;
11651189
IF @debug = 1 BEGIN RAISERROR(N'Is output database OR schema filled in?', 0, 1) WITH NOWAIT; END;
11661190
IF
11671191
(
1168-
LEN(@output_database_name + @output_schema_name) > 0
1169-
AND @output_schema_name <> N'dbo'
1170-
AND (@output_database_name = N''
1171-
OR @output_schema_name = N'')
1192+
LEN(@output_database_name + @output_schema_name) > 0
1193+
AND @output_schema_name <> N'dbo'
1194+
AND
1195+
(
1196+
@output_database_name = N''
1197+
OR @output_schema_name = N''
1198+
)
11721199
)
11731200
BEGIN
11741201
IF @output_database_name = N''
@@ -1189,8 +1216,8 @@ END;
11891216
IF @debug = 1 BEGIN RAISERROR(N'Is custom name something stupid?', 0, 1) WITH NOWAIT; END;
11901217
IF
11911218
(
1192-
PATINDEX(N'%[^a-zA-Z0-9]%', @custom_name) > 0
1193-
OR @custom_name LIKE N'[0-9]%'
1219+
PATINDEX(N'%[^a-zA-Z0-9]%', @custom_name) > 0
1220+
OR @custom_name LIKE N'[0-9]%'
11941221
)
11951222
BEGIN
11961223
RAISERROR(N'
@@ -1234,7 +1261,7 @@ END;
12341261
IF @debug = 1 BEGIN RAISERROR(N'Setting up individual filters', 0, 1) WITH NOWAIT; END;
12351262
IF @query_duration_ms > 0
12361263
BEGIN
1237-
IF LOWER(@event_type) NOT LIKE N'%comp%' /* compile and recompile durations are tiny */
1264+
IF @is_compile_any_event_type <> 0 /* compile and recompile durations are tiny */
12381265
BEGIN
12391266
SET @query_duration_filter += N' AND duration >= ' + CONVERT(nvarchar(20), (@query_duration_ms * 1000)) + @nc10;
12401267
END;
@@ -1291,12 +1318,12 @@ END;
12911318

12921319
IF @object_name <> N''
12931320
BEGIN
1294-
IF @event_type LIKE N'%lock%'
1321+
IF @is_lock_event_type = 1
12951322
BEGIN
12961323
SET @object_id = OBJECT_ID(@fully_formed_babby);
12971324
SET @object_name_filter += N' AND object_id = ' + @object_id + @nc10;
12981325
END;
1299-
IF @event_type NOT LIKE N'%lock%'
1326+
IF @is_lock_event_type = 0
13001327
BEGIN
13011328
SET @object_name_filter += N' AND object_name = N' + QUOTENAME(@object_name, N'''') + @nc10;
13021329
END;
@@ -1311,7 +1338,7 @@ END;
13111338

13121339
/* At this point we'll either put my list of interesting waits in a temp table,
13131340
or a list of user defined waits */
1314-
IF LOWER(@event_type) LIKE N'%wait%'
1341+
IF @is_wait_event_type = 1
13151342
BEGIN
13161343
INSERT
13171344
#wait WITH(TABLOCK)
@@ -1571,11 +1598,11 @@ SET @session_filter_parameterization +=
15711598
IF @debug = 1 BEGIN RAISERROR(N'Setting up the event session', 0, 1) WITH NOWAIT; END;
15721599
SET @session_sql +=
15731600
CASE
1574-
WHEN LOWER(@event_type) LIKE N'%lock%'
1601+
WHEN @is_lock_event_type = 1
15751602
THEN N'
15761603
ADD EVENT sqlserver.blocked_process_report
15771604
(WHERE ( ' + @session_filter_blocking + N' ))'
1578-
WHEN LOWER(@event_type) LIKE N'%quer%'
1605+
WHEN @is_query_event_type = 1
15791606
THEN N'
15801607
ADD EVENT sqlserver.module_end
15811608
(SET collect_statement = 1
@@ -1602,21 +1629,21 @@ SET @session_sql +=
16021629
WHERE ( ' + @session_filter_query_plans + N' ))'
16031630
ELSE N''
16041631
END
1605-
WHEN LOWER(@event_type) LIKE N'%wait%'
1632+
WHEN @is_wait_event_type = 1
16061633
AND @v > 11
16071634
THEN N'
16081635
ADD EVENT sqlos.wait_completed
16091636
(SET collect_wait_resource = 1
16101637
ACTION (sqlserver.database_name, sqlserver.plan_handle, sqlserver.query_hash_signed, sqlserver.query_plan_hash_signed)
16111638
WHERE ( ' + @session_filter_waits + N' ))'
1612-
WHEN LOWER(@event_type) LIKE N'%wait%'
1639+
WHEN @is_wait_event_type = 1
16131640
AND @v = 11
16141641
THEN N'
16151642
ADD EVENT sqlos.wait_info
16161643
(
16171644
ACTION (sqlserver.database_name, sqlserver.plan_handle, sqlserver.query_hash_signed, sqlserver.query_plan_hash_signed)
16181645
WHERE ( ' + @session_filter_waits + N' ))'
1619-
WHEN LOWER(@event_type) LIKE N'%recomp%'
1646+
WHEN @is_recompile_event_type = 1
16201647
THEN CASE
16211648
WHEN @compile_events = 1
16221649
THEN N'
@@ -1630,8 +1657,7 @@ SET @session_sql +=
16301657
ACTION(sqlserver.database_name)
16311658
WHERE ( ' + @session_filter_recompile + N' ))'
16321659
END
1633-
WHEN (LOWER(@event_type) LIKE N'%comp%'
1634-
AND LOWER(@event_type) NOT LIKE N'%re%')
1660+
WHEN @is_compile_event_type = 1
16351661
THEN CASE
16361662
WHEN @compile_events = 1
16371663
THEN N'
@@ -1674,9 +1700,12 @@ IF @debug = 1 BEGIN RAISERROR(@start_sql, 0, 1) WITH NOWAIT; END;
16741700
EXECUTE (@start_sql);
16751701

16761702
/* bail out here if we want to keep the session and not log to tables*/
1677-
IF @keep_alive = 1
1678-
AND @output_database_name = N''
1679-
AND @output_schema_name IN (N'', N'dbo')
1703+
IF
1704+
(
1705+
@keep_alive = 1
1706+
AND @output_database_name = N''
1707+
AND @output_schema_name IN (N'', N'dbo')
1708+
)
16801709
BEGIN
16811710
IF @debug = 1
16821711
BEGIN
@@ -1784,7 +1813,7 @@ END;
17841813
/*
17851814
This is where magic will happen
17861815
*/
1787-
IF LOWER(@event_type) LIKE N'%quer%'
1816+
IF @is_query_event_type = 1
17881817
BEGIN
17891818
WITH
17901819
queries AS
@@ -2137,7 +2166,7 @@ BEGIN
21372166
END;
21382167

21392168

2140-
IF LOWER(@event_type) LIKE N'%comp%' AND LOWER(@event_type) NOT LIKE N'%re%'
2169+
IF @is_compile_event_type = 1
21412170
BEGIN
21422171
IF @compile_events = 1
21432172
BEGIN
@@ -2389,7 +2418,7 @@ BEGIN
23892418
END;
23902419
END;
23912420

2392-
IF LOWER(@event_type) LIKE N'%recomp%'
2421+
IF @is_recompile_event_type = 1
23932422
BEGIN
23942423
IF @compile_events = 1
23952424
BEGIN
@@ -2548,7 +2577,7 @@ IF @compile_events = 1
25482577
END;
25492578

25502579

2551-
IF LOWER(@event_type) LIKE N'%wait%'
2580+
IF @is_wait_event_type = 1
25522581
BEGIN
25532582
WITH
25542583
waits AS
@@ -2700,7 +2729,7 @@ BEGIN
27002729
END;
27012730

27022731

2703-
IF LOWER(@event_type) LIKE N'%lock%'
2732+
IF @is_lock_event_type = 1
27042733
BEGIN
27052734
SELECT
27062735
event_time =
@@ -3626,16 +3655,16 @@ BEGIN
36263655
SET
36273656
hew.event_type_short =
36283657
CASE
3629-
WHEN hew.event_type LIKE N'%block%'
3658+
WHEN hew.event_type LIKE @lock_event_type_pattern
36303659
THEN N'[_]Blocking'
3631-
WHEN ( hew.event_type LIKE N'%comp%'
3632-
AND hew.event_type NOT LIKE N'%re%' )
3660+
WHEN ( hew.event_type LIKE @compile_event_type_pattern
3661+
AND hew.event_type NOT LIKE @recompile_exclude_event_type_pattern )
36333662
THEN N'[_]Compiles'
3634-
WHEN hew.event_type LIKE N'%quer%'
3663+
WHEN hew.event_type LIKE @query_event_type_pattern
36353664
THEN N'[_]Queries'
3636-
WHEN hew.event_type LIKE N'%recomp%'
3665+
WHEN hew.event_type LIKE @recompile_event_type_pattern
36373666
THEN N'[_]Recompiles'
3638-
WHEN hew.event_type LIKE N'%wait%'
3667+
WHEN hew.event_type LIKE @wait_event_type_pattern
36393668
THEN N'[_]Waits'
36403669
ELSE N'?'
36413670
END
@@ -3687,20 +3716,20 @@ BEGIN
36873716
SELECT
36883717
@table_sql =
36893718
CASE
3690-
WHEN @event_type_check LIKE N'%wait%'
3719+
WHEN @event_type_check LIKE @wait_event_type_pattern
36913720
THEN N'CREATE TABLE ' + @object_name_check + @nc10 +
36923721
N'( id bigint PRIMARY KEY IDENTITY, server_name sysname NULL, event_time datetime2 NULL, event_type sysname NULL, ' + @nc10 +
36933722
N' database_name sysname NULL, wait_type nvarchar(60) NULL, duration_ms bigint NULL, signal_duration_ms bigint NULL, ' + @nc10 +
36943723
N' wait_resource sysname NULL, query_plan_hash_signed binary(8) NULL, query_hash_signed binary(8) NULL, plan_handle varbinary(64) NULL );'
3695-
WHEN @event_type_check LIKE N'%lock%'
3724+
WHEN @event_type_check LIKE @lock_event_type_pattern
36963725
THEN N'CREATE TABLE ' + @object_name_check + @nc10 +
36973726
N'( id bigint PRIMARY KEY IDENTITY, server_name sysname NULL, event_time datetime2 NULL, ' + @nc10 +
36983727
N' activity nvarchar(20) NULL, database_name sysname NULL, database_id integer NULL, object_id bigint NULL, contentious_object AS OBJECT_NAME(object_id, database_id), ' + @nc10 +
36993728
N' transaction_id bigint NULL, resource_owner_type sysname NULL, monitor_loop integer NULL, spid integer NULL, ecid integer NULL, query_text nvarchar(max) NULL, ' +
37003729
N' wait_time bigint NULL, transaction_name sysname NULL, last_transaction_started nvarchar(30) NULL, wait_resource nvarchar(100) NULL, ' + @nc10 +
37013730
N' lock_mode nvarchar(10) NULL, status nvarchar(10) NULL, priority integer NULL, transaction_count integer NULL, ' + @nc10 +
37023731
N' client_app sysname NULL, host_name sysname NULL, login_name sysname NULL, isolation_level nvarchar(30) NULL, sql_handle varbinary(64) NULL, blocked_process_report XML NULL );'
3703-
WHEN @event_type_check LIKE N'%quer%'
3732+
WHEN @event_type_check LIKE @query_event_type_pattern
37043733
THEN N'CREATE TABLE ' + @object_name_check + @nc10 +
37053734
N'( id bigint PRIMARY KEY IDENTITY, server_name sysname NULL, event_time datetime2 NULL, event_type sysname NULL, ' + @nc10 +
37063735
N' database_name sysname NULL, object_name nvarchar(512) NULL, sql_text nvarchar(max) NULL, statement nvarchar(max) NULL, ' + @nc10 +
@@ -3709,12 +3738,12 @@ BEGIN
37093738
N' spills_mb decimal(18,2) NULL, row_count decimal(18,2) NULL, estimated_rows decimal(18,2) NULL, dop integer NULL, ' + @nc10 +
37103739
N' serial_ideal_memory_mb decimal(18,2) NULL, requested_memory_mb decimal(18,2) NULL, used_memory_mb decimal(18,2) NULL, ideal_memory_mb decimal(18,2) NULL, ' + @nc10 +
37113740
N' granted_memory_mb decimal(18,2) NULL, query_plan_hash_signed binary(8) NULL, query_hash_signed binary(8) NULL, plan_handle varbinary(64) NULL );'
3712-
WHEN @event_type_check LIKE N'%recomp%'
3741+
WHEN @event_type_check LIKE @recompile_event_type_pattern
37133742
THEN N'CREATE TABLE ' + @object_name_check + @nc10 +
37143743
N'( id bigint PRIMARY KEY IDENTITY, server_name sysname NULL, event_time datetime2 NULL, event_type sysname NULL, ' + @nc10 +
37153744
N' database_name sysname NULL, object_name nvarchar(512) NULL, recompile_cause sysname NULL, statement_text nvarchar(max) NULL, statement_text_checksum AS CHECKSUM(database_name + statement_text) PERSISTED '
37163745
+ CASE WHEN @compile_events = 1 THEN N', compile_cpu_ms bigint NULL, compile_duration_ms bigint NULL );' ELSE N' );' END
3717-
WHEN @event_type_check LIKE N'%comp%' AND @event_type_check NOT LIKE N'%re%'
3746+
WHEN @event_type_check LIKE @compile_event_type_pattern AND @event_type_check NOT LIKE @recompile_exclude_event_type_pattern
37183747
THEN N'CREATE TABLE ' + @object_name_check + @nc10 +
37193748
N'( id bigint PRIMARY KEY IDENTITY, server_name sysname NULL, event_time datetime2 NULL, event_type sysname NULL, ' + @nc10 +
37203749
N' database_name sysname NULL, object_name nvarchar(512) NULL, statement_text nvarchar(max) NULL, statement_text_checksum AS CHECKSUM(database_name + statement_text) PERSISTED '
@@ -4052,7 +4081,7 @@ END;
40524081
(
40534082
nvarchar(max),
40544083
CASE
4055-
WHEN @event_type_check LIKE N'%wait%' /*Wait stats!*/
4084+
WHEN @event_type_check LIKE @wait_event_type_pattern /*Wait stats!*/
40564085
THEN CONVERT
40574086
(
40584087
nvarchar(max),
@@ -4104,9 +4133,9 @@ OUTER APPLY xet.human_events_xml.nodes(''//event'') AS oa(c)
41044133
WHERE c.exist(''(data[@name="duration"]/value/text()[. > 0])'') = 1
41054134
AND c.exist(''@timestamp[. > sql:variable("@date_filter")]'') = 1;')
41064135
)
4107-
WHEN @event_type_check LIKE N'%lock%' /*Blocking!*/
4108-
/*To cut down on nonsense, I'm only inserting new blocking scenarios*/
4109-
/*Any existing blocking scenarios will update the blocking duration*/
4136+
WHEN @event_type_check LIKE @lock_event_type_pattern /*Blocking!*/
4137+
/*To cut down on nonsense, I'm only inserting new blocking scenarios*/
4138+
/*Any existing blocking scenarios will update the blocking duration*/
41104139
THEN CONVERT
41114140
(
41124141
nvarchar(max),
@@ -4283,7 +4312,7 @@ JOIN
42834312
AND x.hostname = x2.host_name
42844313
AND x.loginname = x2.login_name;
42854314
' ))
4286-
WHEN @event_type_check LIKE N'%quer%' /*Queries!*/
4315+
WHEN @event_type_check LIKE @query_event_type_pattern /*Queries!*/
42874316
THEN
42884317
CONVERT
42894318
(
@@ -4346,7 +4375,7 @@ OUTER APPLY xet.human_events_xml.nodes(''//event'') AS oa(c)
43464375
WHERE oa.c.exist(''@timestamp[. > sql:variable("@date_filter")]'') = 1
43474376
AND oa.c.exist(''(action[@name="query_hash_signed"]/value[. != 0])'') = 1; '
43484377
))
4349-
WHEN @event_type_check LIKE N'%recomp%' /*Recompiles!*/
4378+
WHEN @event_type_check LIKE @recompile_event_type_pattern /*Recompiles!*/
43504379
THEN
43514380
CONVERT
43524381
(
@@ -4393,7 +4422,7 @@ AND oa.c.exist(''@timestamp[. > sql:variable("@date_filter")]'') = 1
43934422
ORDER BY
43944423
event_time;'
43954424
)))
4396-
WHEN @event_type_check LIKE N'%comp%' AND @event_type_check NOT LIKE N'%re%' /*Compiles!*/
4425+
WHEN @event_type_check LIKE @compile_event_type_pattern AND @event_type_check NOT LIKE @recompile_exclude_event_type_pattern /*Compiles!*/
43974426
THEN
43984427
CONVERT
43994428
(

0 commit comments

Comments
 (0)