diff --git a/Install-All/DarlingData.sql b/Install-All/DarlingData.sql index 4d27e4ff..66eeba76 100644 --- a/Install-All/DarlingData.sql +++ b/Install-All/DarlingData.sql @@ -1,4 +1,4 @@ --- Compile Date: 05/23/2026 16:07:57 UTC +-- Compile Date: 7/4/2026 4:48:00 PM UTC SET ANSI_NULLS ON; SET ANSI_PADDING ON; SET ANSI_WARNINGS ON; @@ -74,7 +74,7 @@ BEGIN SELECT @version = '3.6', - @version_date = '20260501'; + @version_date = '20260701'; IF @help = 1 BEGIN @@ -6327,7 +6327,7 @@ ALTER PROCEDURE ( @event_type sysname = N'query', @query_duration_ms integer = 500, - @query_sort_order nvarchar(10) = N'cpu', + @query_sort_order nvarchar(20) = N'cpu', @skip_plans bit = 0, @blocking_duration_ms integer = 500, @wait_type nvarchar(4000) = N'ALL', @@ -6364,8 +6364,8 @@ SET XACT_ABORT ON; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SELECT - @version = '7.6', - @version_date = '20260501'; + @version = '7.7', + @version_date = '20260701'; IF @help = 1 BEGIN @@ -6449,7 +6449,7 @@ BEGIN CASE ap.name WHEN N'@event_type' THEN N'"blocking", "query", "waits", "recompiles", "compiles" and certain variations on those words' WHEN N'@query_duration_ms' THEN N'an integer' - WHEN N'@query_sort_order' THEN '"cpu", "reads", "writes", "duration", "memory", "spills", and you can add "avg" to sort by averages, e.g. "avg cpu"' + WHEN N'@query_sort_order' THEN '"cpu", "reads", "writes", "duration", "memory", or "spills" (any of which you can prefix with "avg" to sort by averages, e.g. "avg cpu"), or "event_time"' WHEN N'@skip_plans' THEN '1 or 0' WHEN N'@blocking_duration_ms' THEN N'an integer' WHEN N'@wait_type' THEN N'a single wait type, or a CSV list of wait types' @@ -6777,6 +6777,7 @@ DECLARE @requested_memory_mb_filter nvarchar(max) = N'', @compile_events bit = 0, @parameterization_events bit = 0, + @session_exists bit = 0, @fully_formed_babby nvarchar(1000) = N'', @s_out integer, @s_sql nvarchar(max) = N'', @@ -7200,7 +7201,8 @@ IF @query_sort_order NOT IN N'avg writes', N'avg duration', N'avg memory', - N'avg spills' + N'avg spills', + N'event_time' ) BEGIN RAISERROR(N'That sort order (%s) you chose is so out of this world that i''m ignoring it', 0, 1, @query_sort_order) WITH NOWAIT; @@ -8513,7 +8515,15 @@ BEGIN WHEN N'avg duration' THEN q.avg_duration_ms WHEN N'avg spills' THEN q.avg_spills_mb WHEN N'avg memory' THEN q.avg_granted_memory_mb - ELSE N'cpu' + WHEN N'event_time' + THEN + DATEDIFF_BIG + ( + MILLISECOND, + '20000101', + q.event_time + ) + ELSE q.total_cpu_ms END DESC OPTION(RECOMPILE); END; @@ -9127,7 +9137,7 @@ BEGIN currentdbname = bd.value('(process/@currentdbname)[1]', 'sysname'), currentdbid = bd.value('(process/@currentdb)[1]', 'integer'), blocking_level = 0, - sort_order = CAST('' AS varchar(400)), + sort_order = CONVERT(varchar(400), ''), activity = CASE WHEN oa.c.exist('//blocked-process-report/blocked-process') = 1 THEN 'blocked' END, blocked_process_report = oa.c.query('.') INTO #blocked @@ -9154,17 +9164,17 @@ BEGIN ISNULL ( '(' + - CAST(blocking_spid AS varchar(10)) + + CONVERT(varchar(10), blocking_spid) + ':' + - CAST(blocking_ecid AS varchar(10)) + + CONVERT(varchar(10), blocking_ecid) + ')', 'unresolved process' ) PERSISTED, blocked_desc AS '(' + - CAST(blocked_spid AS varchar(10)) + + CONVERT(varchar(10), blocked_spid) + ':' + - CAST(blocked_ecid AS varchar(10)) + + CONVERT(varchar(10), blocked_ecid) + ')' PERSISTED; CREATE CLUSTERED INDEX @@ -9222,7 +9232,7 @@ BEGIN currentdbname = bg.value('(process/@currentdbname)[1]', 'sysname'), currentdbid = bg.value('(process/@currentdb)[1]', 'integer'), blocking_level = 0, - sort_order = CAST('' AS varchar(400)), + sort_order = CONVERT(varchar(400), ''), activity = CASE WHEN oa.c.exist('//blocked-process-report/blocking-process') = 1 THEN 'blocking' END, blocked_process_report = oa.c.query('.') INTO #blocking @@ -9249,17 +9259,17 @@ BEGIN ISNULL ( '(' + - CAST(blocking_spid AS varchar(10)) + + CONVERT(varchar(10), blocking_spid) + ':' + - CAST(blocking_ecid AS varchar(10)) + + CONVERT(varchar(10), blocking_ecid) + ')', 'unresolved process' ) PERSISTED, blocked_desc AS '(' + - CAST(blocked_spid AS varchar(10)) + + CONVERT(varchar(10), blocked_spid) + ':' + - CAST(blocked_ecid AS varchar(10)) + + CONVERT(varchar(10), blocked_ecid) + ')' PERSISTED; CREATE CLUSTERED INDEX @@ -9283,11 +9293,12 @@ BEGIN b.blocked_desc, level = 0, sort_order = - CAST + CONVERT ( + varchar(400), b.blocking_desc + ' <-- ' + - b.blocked_desc AS varchar(400) + b.blocked_desc ) FROM #blocking AS b WHERE NOT EXISTS @@ -9307,13 +9318,14 @@ BEGIN bg.blocked_desc, h.level + 1, sort_order = - CAST + CONVERT ( + varchar(400), h.sort_order + ' ' + bg.blocking_desc + ' <-- ' + - bg.blocked_desc AS varchar(400) + bg.blocked_desc ) FROM hierarchy AS h JOIN #blocking AS bg @@ -10200,10 +10212,10 @@ BEGIN SELECT N'HumanEvents_Parameterization', 0x430052004500410054004500200056004900450057002000640062006F002E00480075006D0061006E004500760065006E00740073005F0050006100720061006D00650074006500720069007A006100740069006F006E000D000A00410053000D000A0057004900540048000D000A0020002000200020006300700071002000410053000D000A0028000D000A002000200020002000530045004C004500430054000D000A0020002000200020002000200020002000640061007400610062006100730065005F006E0061006D0065002C000D000A0020002000200020002000200020002000710075006500720079005F0068006100730068002C000D000A002000200020002000200020002000200074006F00740061006C005F0063006F006D00700069006C006500730020003D00200043004F0055004E0054005F0042004900470028002A0029002C000D000A002000200020002000200020002000200070006C0061006E005F0063006F0075006E00740020003D00200043004F0055004E0054002800440049005300540049004E00430054002000710075006500720079005F0070006C0061006E005F00680061007300680029002C000D000A002000200020002000200020002000200074006F00740061006C005F0063006F006D00700069006C0065005F0063007000750020003D002000530055004D00280063006F006D00700069006C0065005F006300700075005F00740069006D0065005F006D00730029002C000D000A00200020002000200020002000200020006100760067005F0063006F006D00700069006C0065005F0063007000750020003D002000410056004700280063006F006D00700069006C0065005F006300700075005F00740069006D0065005F006D00730029002C000D000A00200020002000200020002000200020006D00610078005F0063006F006D00700069006C0065005F0063007000750020003D0020004D0041005800280063006F006D00700069006C0065005F006300700075005F00740069006D0065005F006D00730029002C000D000A002000200020002000200020002000200074006F00740061006C005F0063006F006D00700069006C0065005F006400750072006100740069006F006E0020003D002000530055004D00280063006F006D00700069006C0065005F006400750072006100740069006F006E005F006D00730029002C000D000A00200020002000200020002000200020006100760067005F0063006F006D00700069006C0065005F006400750072006100740069006F006E0020003D002000410056004700280063006F006D00700069006C0065005F006400750072006100740069006F006E005F006D00730029002C000D000A00200020002000200020002000200020006D00610078005F0063006F006D00700069006C0065005F006400750072006100740069006F006E0020003D0020004D0041005800280063006F006D00700069006C0065005F006400750072006100740069006F006E005F006D00730029000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D000D000A002000200020002000470052004F00550050002000420059000D000A0020002000200020002000200020002000640061007400610062006100730065005F006E0061006D0065002C000D000A0020002000200020002000200020002000710075006500720079005F0068006100730068000D000A0029000D000A00530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A00200020002000200063002E00640061007400610062006100730065005F006E0061006D0065002C000D000A0020002000200020006B002E00730071006C005F0074006500780074002C000D000A0020002000200020006B002E00690073005F0070006100720061006D00650074006500720069007A00610062006C0065002C000D000A00200020002000200063002E0074006F00740061006C005F0063006F006D00700069006C00650073002C000D000A00200020002000200063002E0070006C0061006E005F0063006F0075006E0074002C000D000A00200020002000200063002E0074006F00740061006C005F0063006F006D00700069006C0065005F006300700075002C000D000A00200020002000200063002E006100760067005F0063006F006D00700069006C0065005F006300700075002C000D000A00200020002000200063002E006D00610078005F0063006F006D00700069006C0065005F006300700075002C000D000A00200020002000200063002E0074006F00740061006C005F0063006F006D00700069006C0065005F006400750072006100740069006F006E002C000D000A00200020002000200063002E006100760067005F0063006F006D00700069006C0065005F006400750072006100740069006F006E002C000D000A00200020002000200063002E006D00610078005F0063006F006D00700069006C0065005F006400750072006100740069006F006E002C000D000A0020002000200020006B002E00710075006500720079005F0070006100720061006D005F0074007900700065002C000D000A0020002000200020006B002E00690073005F006300610063006800650064002C000D000A0020002000200020006B002E00690073005F007200650063006F006D00700069006C00650064002C000D000A0020002000200020006B002E0063006F006D00700069006C0065005F0063006F00640065002C000D000A0020002000200020006B002E006800610073005F006C00690074006500720061006C0073002C000D000A0020002000200020006B002E0070006100720061006D00650074006500720069007A00650064005F00760061006C007500650073005F0063006F0075006E0074000D000A00460052004F004D002000630070007100200041005300200063000D000A00430052004F005300530020004100500050004C0059000D000A0028000D000A002000200020002000530045004C00450043005400200054004F00500020002800310029000D000A00200020002000200020002000200020006B002E002A000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D0020004100530020006B000D000A0020002000200020005700480045005200450020006B002E00710075006500720079005F00680061007300680020003D00200063002E00710075006500720079005F0068006100730068000D000A0020002000200020004F00520044004500520020004200590020006B002E0069006400200044004500530043000D000A00290020004100530020006B000D000A004F00520044004500520020004200590020000D000A00200020002000200063002E0074006F00740061006C005F0063006F006D00700069006C0065007300200044004500530043003B00 WHERE @parameterization_events = 1; INSERT #view_check (view_name, view_definition) - SELECT N'HumanEvents_Queries', 0x430052004500410054004500200056004900450057002000640062006F002E00480075006D0061006E004500760065006E00740073005F0051007500650072006900650073000D000A00410053000D000A0057004900540048000D000A002000200020002000710075006500720079005F006100670067002000410053000D000A0028000D000A002000200020002000530045004C004500430054000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A002000200020002000200020002000200074006F00740061006C005F006300700075005F006D00730020003D002000490053004E0055004C004C00280071002E006300700075005F006D0073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F006C006F0067006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E006C006F0067006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F0070006800790073006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E0070006800790073006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F006400750072006100740069006F006E005F006D00730020003D002000490053004E0055004C004C00280071002E006400750072006100740069006F006E005F006D0073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F007700720069007400650073005F006D00620020003D002000490053004E0055004C004C00280071002E007700720069007400650073005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F007300700069006C006C0073005F006D00620020003D002000490053004E0055004C004C00280071002E007300700069006C006C0073005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F0072006F007700730020003D002000490053004E0055004C004C00280071002E0072006F0077005F0063006F0075006E0074002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006300700075005F006D00730020003D002000490053004E0055004C004C00280071002E006300700075005F006D0073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006C006F0067006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E006C006F0067006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F0070006800790073006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E0070006800790073006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006400750072006100740069006F006E005F006D00730020003D002000490053004E0055004C004C00280071002E006400750072006100740069006F006E005F006D0073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F007700720069007400650073005F006D00620020003D002000490053004E0055004C004C00280071002E007700720069007400650073005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F007300700069006C006C0073005F006D00620020003D002000490053004E0055004C004C00280071002E007300700069006C006C0073005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F0072006F007700730020003D002000490053004E0055004C004C00280071002E0072006F0077005F0063006F0075006E0074002C002000300029000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D00200041005300200071000D000A00200020002000200057004800450052004500200071002E006500760065006E0074005F00740079007000650020003C003E0020004E002700710075006500720079005F0070006F00730074005F0065007800650063007500740069006F006E005F00730068006F00770070006C0061006E0027000D000A0020002000200020000D000A00200020002000200055004E0049004F004E00200041004C004C000D000A0020002000200020000D000A002000200020002000530045004C004500430054000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A00200020002000200020002000200020002F002A0074006F00740061006C0073002A002F000D000A002000200020002000200020002000200074006F00740061006C005F006300700075005F006D00730020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F006C006F0067006900630061006C005F007200650061006400730020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F0070006800790073006900630061006C005F007200650061006400730020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F006400750072006100740069006F006E005F006D00730020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F0077007200690074006500730020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F007300700069006C006C0073005F006D00620020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D002000490053004E0055004C004C00280071002E0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D002000490053004E0055004C004C00280071002E006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F0072006F007700730020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020002F002A00610076006500720061006700650073002A002F000D000A00200020002000200020002000200020006100760067005F006300700075005F006D00730020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F006C006F0067006900630061006C005F007200650061006400730020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F0070006800790073006900630061006C005F007200650061006400730020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F006400750072006100740069006F006E005F006D00730020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F007700720069007400650073005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F007300700069006C006C0073005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D002000490053004E0055004C004C00280071002E0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D002000490053004E0055004C004C00280071002E006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F0072006F007700730020003D0020004E0055004C004C000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D00200041005300200071000D000A00200020002000200057004800450052004500200071002E006500760065006E0074005F00740079007000650020003D0020004E002700710075006500720079005F0070006F00730074005F0065007800650063007500740069006F006E005F00730068006F00770070006C0061006E0027000D000A0029002C000D000A00200020002000200074006F00740061006C0073002000410053000D000A0028000D000A002000200020002000530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A002000200020002000200020002000200065007800650063007500740069006F006E00730020003D0020000D000A0020002000200020002000200020002000200020002000200043004F0055004E0054005F0042004900470028002A00290020004F0056004500520020000D000A0020002000200020002000200020002000200020002000200028000D000A00200020002000200020002000200020002000200020002000200020002000200050004100520054004900540049004F004E002000420059000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0020002000200020002000200020002000200020002000200029002C000D000A00200020002000200020002000200020002F002A0074006F00740061006C0073002A002F000D000A002000200020002000200020002000200074006F00740061006C005F006300700075005F006D00730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006300700075005F006D0073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006C006F0067006900630061006C005F007200650061006400730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0070006800790073006900630061006C005F007200650061006400730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006400750072006100740069006F006E005F006D00730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0077007200690074006500730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F007700720069007400650073005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F007300700069006C006C0073005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0072006F007700730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0072006F00770073002C00200030002E00290029002C000D000A00200020002000200020002000200020002F002A00610076006500720061006700650073002A002F000D000A00200020002000200020002000200020006100760067005F006300700075005F006D00730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006300700075005F006D0073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006C006F0067006900630061006C005F007200650061006400730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0070006800790073006900630061006C005F007200650061006400730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006400750072006100740069006F006E005F006D00730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006400750072006100740069006F006E005F006D0073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0077007200690074006500730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F007700720069007400650073005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F007300700069006C006C0073005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F007300700069006C006C0073005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0072006F007700730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0072006F00770073002C0020003000290029000D000A002000200020002000460052004F004D002000710075006500720079005F00610067006700200041005300200071000D000A002000200020002000470052004F00550050002000420059000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0029002C000D000A002000200020002000710075006500720079005F0072006500730075006C00740073002000410053000D000A0028000D000A002000200020002000530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A002000200020002000200020002000200071002E006500760065006E0074005F00740069006D0065002C000D000A002000200020002000200020002000200071002E00640061007400610062006100730065005F006E0061006D0065002C000D000A002000200020002000200020002000200071002E006F0062006A006500630074005F006E0061006D0065002C000D000A0020002000200020002000200020002000710032002E00730074006100740065006D0065006E0074005F0074006500780074002C000D000A002000200020002000200020002000200071002E00730071006C005F0074006500780074002C000D000A002000200020002000200020002000200071002E00730068006F00770070006C0061006E005F0078006D006C002C000D000A002000200020002000200020002000200074002E0065007800650063007500740069006F006E0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006300700075005F006D0073002C000D000A002000200020002000200020002000200074002E006100760067005F006300700075005F006D0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C000D000A002000200020002000200020002000200074002E006100760067005F006400750072006100740069006F006E005F006D0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F007700720069007400650073002C000D000A002000200020002000200020002000200074002E006100760067005F007700720069007400650073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F007300700069006C006C0073005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0072006F00770073002C000D000A002000200020002000200020002000200074002E006100760067005F0072006F00770073002C000D000A002000200020002000200020002000200071002E00730065007200690061006C005F0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E007200650071007500650073007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E0065007300740069006D0061007400650064005F0072006F00770073002C000D000A002000200020002000200020002000200071002E0064006F0070002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A00200020002000200020002000200020006E0020003D00200052004F0057005F004E0055004D004200450052002800290020004F0056004500520020000D000A0020002000200020002000200020002000200020002000200028000D000A00200020002000200020002000200020002000200020002000200020002000200050004100520054004900540049004F004E002000420059000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0020002000200020002000200020002000200020002000200020002000200020004F0052004400450052002000420059000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0020002000200020002000200020002000200020002000200029000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D00200041005300200071000D000A0020002000200020004A004F0049004E00200074006F00740061006C007300200041005300200074000D000A00200020002000200020002000200020004F004E002000200071002E00710075006500720079005F0068006100730068005F007300690067006E006500640020003D00200074002E00710075006500720079005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E004400200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E006500640020003D00200074002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E004400200071002E0070006C0061006E005F00680061006E0064006C00650020003D00200074002E0070006C0061006E005F00680061006E0064006C0065000D000A002000200020002000430052004F005300530020004100500050004C0059000D000A00200020002000200028000D000A0020002000200020002000200020002000530045004C00450043005400200054004F00500020002800310029000D000A00200020002000200020002000200020002000200020002000730074006100740065006D0065006E0074005F00740065007800740020003D002000710032002E00730074006100740065006D0065006E0074000D000A0020002000200020002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D002000410053002000710032000D000A002000200020002000200020002000200057004800450052004500200071002E00710075006500720079005F0068006100730068005F007300690067006E006500640020003D002000710032002E00710075006500720079005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E00440020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E006500640020003D002000710032002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E00440020002000200071002E0070006C0061006E005F00680061006E0064006C00650020003D002000710032002E0070006C0061006E005F00680061006E0064006C0065000D000A002000200020002000200020002000200041004E004400200020002000710032002E00730074006100740065006D0065006E00740020004900530020004E004F00540020004E0055004C004C000D000A00200020002000200020002000200020004F00520044004500520020004200590020000D000A00200020002000200020002000200020002000200020002000710032002E006500760065006E0074005F00740069006D006500200044004500530043000D000A00200020002000200029002000410053002000710032000D000A00200020002000200057004800450052004500200071002E00730068006F00770070006C0061006E005F0078006D006C002E0065007800690073007400280027002A002700290020003D00200031000D000A0029000D000A00530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A00200020002000200071002E006500760065006E0074005F00740069006D0065002C000D000A00200020002000200071002E00640061007400610062006100730065005F006E0061006D0065002C000D000A00200020002000200071002E006F0062006A006500630074005F006E0061006D0065002C000D000A002000200020002000730074006100740065006D0065006E0074005F00740065007800740020003D0020000D000A00200020002000200020002000200020005400520059005F004300410053005400280071002E00730074006100740065006D0065006E0074005F007400650078007400200041005300200078006D006C0029002C000D000A002000200020002000730071006C005F00740065007800740020003D0020000D000A00200020002000200020002000200020005400520059005F004300410053005400280071002E00730071006C005F007400650078007400200041005300200078006D006C0029002C000D000A00200020002000200071002E00730068006F00770070006C0061006E005F0078006D006C002C000D000A00200020002000200071002E0065007800650063007500740069006F006E0073002C000D000A00200020002000200071002E0074006F00740061006C005F006300700075005F006D0073002C000D000A00200020002000200071002E006100760067005F006300700075005F006D0073002C000D000A00200020002000200071002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C000D000A00200020002000200071002E006100760067005F006400750072006100740069006F006E005F006D0073002C000D000A00200020002000200071002E0074006F00740061006C005F007700720069007400650073002C000D000A00200020002000200071002E006100760067005F007700720069007400650073002C000D000A00200020002000200071002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C000D000A00200020002000200071002E006100760067005F007300700069006C006C0073005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F0072006F00770073002C000D000A00200020002000200071002E006100760067005F0072006F00770073002C000D000A00200020002000200071002E00730065007200690061006C005F0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E007200650071007500650073007400650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0065007300740069006D0061007400650064005F0072006F00770073002C000D000A00200020002000200071002E0064006F0070002C000D000A00200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A00200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A00200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A00460052004F004D002000710075006500720079005F0072006500730075006C0074007300200041005300200071000D000A0057004800450052004500200071002E006E0020003D00200031003B00 + SELECT N'HumanEvents_Queries', 0x430052004500410054004500200056004900450057002000640062006F002E00480075006D0061006E004500760065006E00740073005F0051007500650072006900650073000D000A00410053000D000A0057004900540048000D000A002000200020002000710075006500720079005F006100670067002000410053000D000A0028000D000A002000200020002000530045004C004500430054000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A0020002000200020002000200020002000690073005F0065007800650063007500740069006F006E0020003D00200031002C000D000A002000200020002000200020002000200074006F00740061006C005F006300700075005F006D00730020003D002000490053004E0055004C004C00280071002E006300700075005F006D0073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F006C006F0067006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E006C006F0067006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F0070006800790073006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E0070006800790073006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F006400750072006100740069006F006E005F006D00730020003D002000490053004E0055004C004C00280071002E006400750072006100740069006F006E005F006D0073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F007700720069007400650073005F006D00620020003D002000490053004E0055004C004C00280071002E007700720069007400650073005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F007300700069006C006C0073005F006D00620020003D002000490053004E0055004C004C00280071002E007300700069006C006C0073005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F0072006F007700730020003D002000490053004E0055004C004C00280071002E0072006F0077005F0063006F0075006E0074002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006300700075005F006D00730020003D002000490053004E0055004C004C00280071002E006300700075005F006D0073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006C006F0067006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E006C006F0067006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F0070006800790073006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E0070006800790073006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006400750072006100740069006F006E005F006D00730020003D002000490053004E0055004C004C00280071002E006400750072006100740069006F006E005F006D0073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F007700720069007400650073005F006D00620020003D002000490053004E0055004C004C00280071002E007700720069007400650073005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F007300700069006C006C0073005F006D00620020003D002000490053004E0055004C004C00280071002E007300700069006C006C0073005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F0072006F007700730020003D002000490053004E0055004C004C00280071002E0072006F0077005F0063006F0075006E0074002C002000300029000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D00200041005300200071000D000A00200020002000200057004800450052004500200071002E006500760065006E0074005F00740079007000650020003C003E0020004E002700710075006500720079005F0070006F00730074005F0065007800650063007500740069006F006E005F00730068006F00770070006C0061006E0027000D000A0020002000200020000D000A00200020002000200055004E0049004F004E00200041004C004C000D000A0020002000200020000D000A002000200020002000530045004C004500430054000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A0020002000200020002000200020002000690073005F0065007800650063007500740069006F006E0020003D00200030002C000D000A00200020002000200020002000200020002F002A0074006F00740061006C0073002A002F000D000A002000200020002000200020002000200074006F00740061006C005F006300700075005F006D00730020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F006C006F0067006900630061006C005F007200650061006400730020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F0070006800790073006900630061006C005F007200650061006400730020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F006400750072006100740069006F006E005F006D00730020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F0077007200690074006500730020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F007300700069006C006C0073005F006D00620020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D002000490053004E0055004C004C00280071002E0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D002000490053004E0055004C004C00280071002E006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F0072006F007700730020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020002F002A00610076006500720061006700650073002A002F000D000A00200020002000200020002000200020006100760067005F006300700075005F006D00730020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F006C006F0067006900630061006C005F007200650061006400730020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F0070006800790073006900630061006C005F007200650061006400730020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F006400750072006100740069006F006E005F006D00730020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F007700720069007400650073005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F007300700069006C006C0073005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D002000490053004E0055004C004C00280071002E0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D002000490053004E0055004C004C00280071002E006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F0072006F007700730020003D0020004E0055004C004C000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D00200041005300200071000D000A00200020002000200057004800450052004500200071002E006500760065006E0074005F00740079007000650020003D0020004E002700710075006500720079005F0070006F00730074005F0065007800650063007500740069006F006E005F00730068006F00770070006C0061006E0027000D000A0029002C000D000A00200020002000200074006F00740061006C0073002000410053000D000A0028000D000A002000200020002000530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A002000200020002000200020002000200065007800650063007500740069006F006E00730020003D0020000D000A00200020002000200020002000200020002000200020002000530055004D00280043004F004E005600450052005400280062006900670069006E0074002C00200071002E00690073005F0065007800650063007500740069006F006E00290029002C000D000A00200020002000200020002000200020002F002A0074006F00740061006C0073002A002F000D000A002000200020002000200020002000200074006F00740061006C005F006300700075005F006D00730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006300700075005F006D0073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006C006F0067006900630061006C005F007200650061006400730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0070006800790073006900630061006C005F007200650061006400730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006400750072006100740069006F006E005F006D00730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0077007200690074006500730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F007700720069007400650073005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F007300700069006C006C0073005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0072006F007700730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0072006F00770073002C00200030002E00290029002C000D000A00200020002000200020002000200020002F002A00610076006500720061006700650073002A002F000D000A00200020002000200020002000200020006100760067005F006300700075005F006D00730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006300700075005F006D0073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006C006F0067006900630061006C005F007200650061006400730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0070006800790073006900630061006C005F007200650061006400730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006400750072006100740069006F006E005F006D00730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006400750072006100740069006F006E005F006D0073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0077007200690074006500730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F007700720069007400650073005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F007300700069006C006C0073005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F007300700069006C006C0073005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0072006F007700730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0072006F00770073002C0020003000290029000D000A002000200020002000460052004F004D002000710075006500720079005F00610067006700200041005300200071000D000A002000200020002000470052004F00550050002000420059000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0029002C000D000A002000200020002000710075006500720079005F0072006500730075006C00740073002000410053000D000A0028000D000A002000200020002000530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A002000200020002000200020002000200071002E006500760065006E0074005F00740069006D0065002C000D000A002000200020002000200020002000200071002E00640061007400610062006100730065005F006E0061006D0065002C000D000A002000200020002000200020002000200071002E006F0062006A006500630074005F006E0061006D0065002C000D000A0020002000200020002000200020002000710032002E00730074006100740065006D0065006E0074005F0074006500780074002C000D000A002000200020002000200020002000200071002E00730071006C005F0074006500780074002C000D000A002000200020002000200020002000200071002E00730068006F00770070006C0061006E005F0078006D006C002C000D000A002000200020002000200020002000200074002E0065007800650063007500740069006F006E0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006300700075005F006D0073002C000D000A002000200020002000200020002000200074002E006100760067005F006300700075005F006D0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C000D000A002000200020002000200020002000200074002E006100760067005F006400750072006100740069006F006E005F006D0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F007700720069007400650073002C000D000A002000200020002000200020002000200074002E006100760067005F007700720069007400650073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F007300700069006C006C0073005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0072006F00770073002C000D000A002000200020002000200020002000200074002E006100760067005F0072006F00770073002C000D000A002000200020002000200020002000200071002E00730065007200690061006C005F0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E007200650071007500650073007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E0065007300740069006D0061007400650064005F0072006F00770073002C000D000A002000200020002000200020002000200071002E0064006F0070002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A00200020002000200020002000200020006E0020003D00200052004F0057005F004E0055004D004200450052002800290020004F0056004500520020000D000A0020002000200020002000200020002000200020002000200028000D000A00200020002000200020002000200020002000200020002000200020002000200050004100520054004900540049004F004E002000420059000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0020002000200020002000200020002000200020002000200020002000200020004F0052004400450052002000420059000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0020002000200020002000200020002000200020002000200029000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D00200041005300200071000D000A0020002000200020004A004F0049004E00200074006F00740061006C007300200041005300200074000D000A00200020002000200020002000200020004F004E002000200071002E00710075006500720079005F0068006100730068005F007300690067006E006500640020003D00200074002E00710075006500720079005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E004400200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E006500640020003D00200074002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E004400200071002E0070006C0061006E005F00680061006E0064006C00650020003D00200074002E0070006C0061006E005F00680061006E0064006C0065000D000A002000200020002000430052004F005300530020004100500050004C0059000D000A00200020002000200028000D000A0020002000200020002000200020002000530045004C00450043005400200054004F00500020002800310029000D000A00200020002000200020002000200020002000200020002000730074006100740065006D0065006E0074005F00740065007800740020003D002000710032002E00730074006100740065006D0065006E0074000D000A0020002000200020002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D002000410053002000710032000D000A002000200020002000200020002000200057004800450052004500200071002E00710075006500720079005F0068006100730068005F007300690067006E006500640020003D002000710032002E00710075006500720079005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E00440020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E006500640020003D002000710032002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E00440020002000200071002E0070006C0061006E005F00680061006E0064006C00650020003D002000710032002E0070006C0061006E005F00680061006E0064006C0065000D000A002000200020002000200020002000200041004E004400200020002000710032002E00730074006100740065006D0065006E00740020004900530020004E004F00540020004E0055004C004C000D000A00200020002000200020002000200020004F00520044004500520020004200590020000D000A00200020002000200020002000200020002000200020002000710032002E006500760065006E0074005F00740069006D006500200044004500530043000D000A00200020002000200029002000410053002000710032000D000A00200020002000200057004800450052004500200071002E00730068006F00770070006C0061006E005F0078006D006C002E0065007800690073007400280027002A002700290020003D00200031000D000A0029000D000A00530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A00200020002000200071002E006500760065006E0074005F00740069006D0065002C000D000A00200020002000200071002E00640061007400610062006100730065005F006E0061006D0065002C000D000A00200020002000200071002E006F0062006A006500630074005F006E0061006D0065002C000D000A002000200020002000730074006100740065006D0065006E0074005F00740065007800740020003D0020000D000A00200020002000200020002000200020005400520059005F004300410053005400280071002E00730074006100740065006D0065006E0074005F007400650078007400200041005300200078006D006C0029002C000D000A002000200020002000730071006C005F00740065007800740020003D0020000D000A00200020002000200020002000200020005400520059005F004300410053005400280071002E00730071006C005F007400650078007400200041005300200078006D006C0029002C000D000A00200020002000200071002E00730068006F00770070006C0061006E005F0078006D006C002C000D000A00200020002000200071002E0065007800650063007500740069006F006E0073002C000D000A00200020002000200071002E0074006F00740061006C005F006300700075005F006D0073002C000D000A00200020002000200071002E006100760067005F006300700075005F006D0073002C000D000A00200020002000200071002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C000D000A00200020002000200071002E006100760067005F006400750072006100740069006F006E005F006D0073002C000D000A00200020002000200071002E0074006F00740061006C005F007700720069007400650073002C000D000A00200020002000200071002E006100760067005F007700720069007400650073002C000D000A00200020002000200071002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C000D000A00200020002000200071002E006100760067005F007300700069006C006C0073005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F0072006F00770073002C000D000A00200020002000200071002E006100760067005F0072006F00770073002C000D000A00200020002000200071002E00730065007200690061006C005F0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E007200650071007500650073007400650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0065007300740069006D0061007400650064005F0072006F00770073002C000D000A00200020002000200071002E0064006F0070002C000D000A00200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A00200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A00200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A00460052004F004D002000710075006500720079005F0072006500730075006C0074007300200041005300200071000D000A0057004800450052004500200071002E006E0020003D00200031003B00 WHERE @skip_plans = 0; INSERT #view_check (view_name, view_definition) - SELECT N'HumanEvents_Queries', 0x430052004500410054004500200056004900450057002000640062006F002E00480075006D0061006E004500760065006E00740073005F0051007500650072006900650073005F006E0070000D000A00410053000D000A0057004900540048000D000A002000200020002000710075006500720079005F006100670067002000410053000D000A0028000D000A002000200020002000530045004C004500430054000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A002000200020002000200020002000200074006F00740061006C005F006300700075005F006D00730020003D002000490053004E0055004C004C00280071002E006300700075005F006D0073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F006C006F0067006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E006C006F0067006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F0070006800790073006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E0070006800790073006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F006400750072006100740069006F006E005F006D00730020003D002000490053004E0055004C004C00280071002E006400750072006100740069006F006E005F006D0073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F007700720069007400650073005F006D00620020003D002000490053004E0055004C004C00280071002E007700720069007400650073005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F007300700069006C006C0073005F006D00620020003D002000490053004E0055004C004C00280071002E007300700069006C006C0073005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F0072006F007700730020003D002000490053004E0055004C004C00280071002E0072006F0077005F0063006F0075006E0074002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006300700075005F006D00730020003D002000490053004E0055004C004C00280071002E006300700075005F006D0073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006C006F0067006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E006C006F0067006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F0070006800790073006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E0070006800790073006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006400750072006100740069006F006E005F006D00730020003D002000490053004E0055004C004C00280071002E006400750072006100740069006F006E005F006D0073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F007700720069007400650073005F006D00620020003D002000490053004E0055004C004C00280071002E007700720069007400650073005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F007300700069006C006C0073005F006D00620020003D002000490053004E0055004C004C00280071002E007300700069006C006C0073005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F0072006F007700730020003D002000490053004E0055004C004C00280071002E0072006F0077005F0063006F0075006E0074002C002000300029000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D00200041005300200071000D000A00200020002000200057004800450052004500200071002E006500760065006E0074005F00740079007000650020003C003E0020004E002700710075006500720079005F0070006F00730074005F0065007800650063007500740069006F006E005F00730068006F00770070006C0061006E0027000D000A0029002C000D000A00200020002000200074006F00740061006C0073002000410053000D000A0028000D000A002000200020002000530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A002000200020002000200020002000200065007800650063007500740069006F006E00730020003D0020000D000A0020002000200020002000200020002000200020002000200043004F0055004E0054005F0042004900470028002A00290020004F0056004500520020000D000A0020002000200020002000200020002000200020002000200028000D000A00200020002000200020002000200020002000200020002000200020002000200050004100520054004900540049004F004E002000420059000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0020002000200020002000200020002000200020002000200029002C000D000A00200020002000200020002000200020002F002A0074006F00740061006C0073002A002F000D000A002000200020002000200020002000200074006F00740061006C005F006300700075005F006D00730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006300700075005F006D0073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006C006F0067006900630061006C005F007200650061006400730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0070006800790073006900630061006C005F007200650061006400730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006400750072006100740069006F006E005F006D00730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0077007200690074006500730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F007700720069007400650073005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F007300700069006C006C0073005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0072006F007700730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0072006F00770073002C00200030002E00290029002C000D000A00200020002000200020002000200020002F002A00610076006500720061006700650073002A002F000D000A00200020002000200020002000200020006100760067005F006300700075005F006D00730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006300700075005F006D0073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006C006F0067006900630061006C005F007200650061006400730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0070006800790073006900630061006C005F007200650061006400730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006400750072006100740069006F006E005F006D00730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006400750072006100740069006F006E005F006D0073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0077007200690074006500730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F007700720069007400650073005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F007300700069006C006C0073005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F007300700069006C006C0073005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0072006F007700730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0072006F00770073002C0020003000290029000D000A002000200020002000460052004F004D002000710075006500720079005F00610067006700200041005300200071000D000A002000200020002000470052004F00550050002000420059000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0029002C000D000A002000200020002000710075006500720079005F0072006500730075006C00740073002000410053000D000A0028000D000A002000200020002000530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A002000200020002000200020002000200071002E006500760065006E0074005F00740069006D0065002C000D000A002000200020002000200020002000200071002E00640061007400610062006100730065005F006E0061006D0065002C000D000A002000200020002000200020002000200071002E006F0062006A006500630074005F006E0061006D0065002C000D000A0020002000200020002000200020002000710032002E00730074006100740065006D0065006E0074005F0074006500780074002C000D000A002000200020002000200020002000200071002E00730071006C005F0074006500780074002C000D000A002000200020002000200020002000200071002E00730068006F00770070006C0061006E005F0078006D006C002C000D000A002000200020002000200020002000200074002E0065007800650063007500740069006F006E0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006300700075005F006D0073002C000D000A002000200020002000200020002000200074002E006100760067005F006300700075005F006D0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C000D000A002000200020002000200020002000200074002E006100760067005F006400750072006100740069006F006E005F006D0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F007700720069007400650073002C000D000A002000200020002000200020002000200074002E006100760067005F007700720069007400650073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F007300700069006C006C0073005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0072006F00770073002C000D000A002000200020002000200020002000200074002E006100760067005F0072006F00770073002C000D000A002000200020002000200020002000200071002E00730065007200690061006C005F0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E007200650071007500650073007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E0065007300740069006D0061007400650064005F0072006F00770073002C000D000A002000200020002000200020002000200071002E0064006F0070002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A00200020002000200020002000200020006E0020003D00200052004F0057005F004E0055004D004200450052002800290020004F0056004500520020000D000A0020002000200020002000200020002000200020002000200028000D000A00200020002000200020002000200020002000200020002000200020002000200050004100520054004900540049004F004E002000420059000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0020002000200020002000200020002000200020002000200020002000200020004F0052004400450052002000420059000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0020002000200020002000200020002000200020002000200029000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D00200041005300200071000D000A0020002000200020004A004F0049004E00200074006F00740061006C007300200041005300200074000D000A002000200020002000200020004F004E002000200071002E00710075006500720079005F0068006100730068005F007300690067006E006500640020003D00200074002E00710075006500720079005F0068006100730068005F007300690067006E00650064000D000A0020002000200020002000200041004E004400200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E006500640020003D00200074002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064000D000A0020002000200020004F00550054004500520020004100500050004C0059000D000A00200020002000200028000D000A0020002000200020002000200020002000530045004C00450043005400200054004F00500020002800310029000D000A00200020002000200020002000200020002000200020002000730074006100740065006D0065006E0074005F00740065007800740020003D002000710032002E00730074006100740065006D0065006E0074000D000A0020002000200020002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D002000410053002000710032000D000A002000200020002000200020002000200057004800450052004500200071002E00710075006500720079005F0068006100730068005F007300690067006E006500640020003D002000710032002E00710075006500720079005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E00440020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E006500640020003D002000710032002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E004400200020002000710032002E00730074006100740065006D0065006E00740020004900530020004E004F00540020004E0055004C004C000D000A00200020002000200020002000200020004F00520044004500520020004200590020000D000A00200020002000200020002000200020002000200020002000710032002E006500760065006E0074005F00740069006D006500200044004500530043000D000A00200020002000200029002000410053002000710032000D000A0029000D000A00530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A00200020002000200071002E006500760065006E0074005F00740069006D0065002C000D000A00200020002000200071002E00640061007400610062006100730065005F006E0061006D0065002C000D000A002000200020002000730074006100740065006D0065006E0074005F00740065007800740020003D0020000D000A00200020002000200020002000200020005400520059005F004300410053005400280071002E00730074006100740065006D0065006E0074005F007400650078007400200041005300200078006D006C0029002C000D000A002000200020002000730071006C005F00740065007800740020003D0020000D000A00200020002000200020002000200020005400520059005F004300410053005400280071002E00730071006C005F007400650078007400200041005300200078006D006C0029002C000D000A00200020002000200071002E0065007800650063007500740069006F006E0073002C000D000A00200020002000200071002E0074006F00740061006C005F006300700075005F006D0073002C000D000A00200020002000200071002E006100760067005F006300700075005F006D0073002C000D000A00200020002000200071002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C000D000A00200020002000200071002E006100760067005F006400750072006100740069006F006E005F006D0073002C000D000A00200020002000200071002E0074006F00740061006C005F007700720069007400650073002C000D000A00200020002000200071002E006100760067005F007700720069007400650073002C000D000A00200020002000200071002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C000D000A00200020002000200071002E006100760067005F007300700069006C006C0073005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F0072006F00770073002C000D000A00200020002000200071002E006100760067005F0072006F00770073002C000D000A00200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A00200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A00200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A00460052004F004D002000710075006500720079005F0072006500730075006C0074007300200041005300200071000D000A0057004800450052004500200071002E006E0020003D00200031003B00 + SELECT N'HumanEvents_Queries', 0x430052004500410054004500200056004900450057002000640062006F002E00480075006D0061006E004500760065006E00740073005F0051007500650072006900650073005F006E0070000D000A00410053000D000A0057004900540048000D000A002000200020002000710075006500720079005F006100670067002000410053000D000A0028000D000A002000200020002000530045004C004500430054000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A0020002000200020002000200020002000690073005F0065007800650063007500740069006F006E0020003D00200031002C000D000A002000200020002000200020002000200074006F00740061006C005F006300700075005F006D00730020003D002000490053004E0055004C004C00280071002E006300700075005F006D0073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F006C006F0067006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E006C006F0067006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F0070006800790073006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E0070006800790073006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F006400750072006100740069006F006E005F006D00730020003D002000490053004E0055004C004C00280071002E006400750072006100740069006F006E005F006D0073002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F007700720069007400650073005F006D00620020003D002000490053004E0055004C004C00280071002E007700720069007400650073005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F007300700069006C006C0073005F006D00620020003D002000490053004E0055004C004C00280071002E007300700069006C006C0073005F006D0062002C00200030002E0029002C000D000A002000200020002000200020002000200074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A002000200020002000200020002000200074006F00740061006C005F0072006F007700730020003D002000490053004E0055004C004C00280071002E0072006F0077005F0063006F0075006E0074002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006300700075005F006D00730020003D002000490053004E0055004C004C00280071002E006300700075005F006D0073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006C006F0067006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E006C006F0067006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F0070006800790073006900630061006C005F007200650061006400730020003D002000490053004E0055004C004C00280071002E0070006800790073006900630061006C005F00720065006100640073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F006400750072006100740069006F006E005F006D00730020003D002000490053004E0055004C004C00280071002E006400750072006100740069006F006E005F006D0073002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F007700720069007400650073005F006D00620020003D002000490053004E0055004C004C00280071002E007700720069007400650073005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F007300700069006C006C0073005F006D00620020003D002000490053004E0055004C004C00280071002E007300700069006C006C0073005F006D0062002C00200030002E0029002C000D000A00200020002000200020002000200020006100760067005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004E0055004C004C002C000D000A00200020002000200020002000200020006100760067005F0072006F007700730020003D002000490053004E0055004C004C00280071002E0072006F0077005F0063006F0075006E0074002C002000300029000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D00200041005300200071000D000A00200020002000200057004800450052004500200071002E006500760065006E0074005F00740079007000650020003C003E0020004E002700710075006500720079005F0070006F00730074005F0065007800650063007500740069006F006E005F00730068006F00770070006C0061006E0027000D000A0029002C000D000A00200020002000200074006F00740061006C0073002000410053000D000A0028000D000A002000200020002000530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A002000200020002000200020002000200065007800650063007500740069006F006E00730020003D0020000D000A00200020002000200020002000200020002000200020002000530055004D00280043004F004E005600450052005400280062006900670069006E0074002C00200071002E00690073005F0065007800650063007500740069006F006E00290029002C000D000A00200020002000200020002000200020002F002A0074006F00740061006C0073002A002F000D000A002000200020002000200020002000200074006F00740061006C005F006300700075005F006D00730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006300700075005F006D0073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006C006F0067006900630061006C005F007200650061006400730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0070006800790073006900630061006C005F007200650061006400730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006400750072006100740069006F006E005F006D00730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0077007200690074006500730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F007700720069007400650073005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F007300700069006C006C0073005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A002000200020002000200020002000200074006F00740061006C005F0072006F007700730020003D002000530055004D002800490053004E0055004C004C00280071002E0074006F00740061006C005F0072006F00770073002C00200030002E00290029002C000D000A00200020002000200020002000200020002F002A00610076006500720061006700650073002A002F000D000A00200020002000200020002000200020006100760067005F006300700075005F006D00730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006300700075005F006D0073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006C006F0067006900630061006C005F007200650061006400730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0070006800790073006900630061006C005F007200650061006400730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006400750072006100740069006F006E005F006D00730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006400750072006100740069006F006E005F006D0073002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0077007200690074006500730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F007700720069007400650073005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F007300700069006C006C0073005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F007300700069006C006C0073005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0075007300650064005F006D0065006D006F00720079005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D00620020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C00200030002E00290029002C000D000A00200020002000200020002000200020006100760067005F0072006F007700730020003D0020004100560047002800490053004E0055004C004C00280071002E006100760067005F0072006F00770073002C0020003000290029000D000A002000200020002000460052004F004D002000710075006500720079005F00610067006700200041005300200071000D000A002000200020002000470052004F00550050002000420059000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0029002C000D000A002000200020002000710075006500720079005F0072006500730075006C00740073002000410053000D000A0028000D000A002000200020002000530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A002000200020002000200020002000200071002E006500760065006E0074005F00740069006D0065002C000D000A002000200020002000200020002000200071002E00640061007400610062006100730065005F006E0061006D0065002C000D000A002000200020002000200020002000200071002E006F0062006A006500630074005F006E0061006D0065002C000D000A0020002000200020002000200020002000710032002E00730074006100740065006D0065006E0074005F0074006500780074002C000D000A002000200020002000200020002000200071002E00730071006C005F0074006500780074002C000D000A002000200020002000200020002000200071002E00730068006F00770070006C0061006E005F0078006D006C002C000D000A002000200020002000200020002000200074002E0065007800650063007500740069006F006E0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006300700075005F006D0073002C000D000A002000200020002000200020002000200074002E006100760067005F006300700075005F006D0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C000D000A002000200020002000200020002000200074002E006100760067005F006400750072006100740069006F006E005F006D0073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F007700720069007400650073002C000D000A002000200020002000200020002000200074002E006100760067005F007700720069007400650073002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F007300700069006C006C0073005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200074002E0074006F00740061006C005F0072006F00770073002C000D000A002000200020002000200020002000200074002E006100760067005F0072006F00770073002C000D000A002000200020002000200020002000200071002E00730065007200690061006C005F0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E007200650071007500650073007400650064005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E0069006400650061006C005F006D0065006D006F00720079005F006D0062002C000D000A002000200020002000200020002000200071002E0065007300740069006D0061007400650064005F0072006F00770073002C000D000A002000200020002000200020002000200071002E0064006F0070002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065002C000D000A00200020002000200020002000200020006E0020003D00200052004F0057005F004E0055004D004200450052002800290020004F0056004500520020000D000A0020002000200020002000200020002000200020002000200028000D000A00200020002000200020002000200020002000200020002000200020002000200050004100520054004900540049004F004E002000420059000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0020002000200020002000200020002000200020002000200020002000200020004F0052004400450052002000420059000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A002000200020002000200020002000200020002000200020002000200020002000200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A0020002000200020002000200020002000200020002000200029000D000A002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D00200041005300200071000D000A0020002000200020004A004F0049004E00200074006F00740061006C007300200041005300200074000D000A002000200020002000200020004F004E002000200071002E00710075006500720079005F0068006100730068005F007300690067006E006500640020003D00200074002E00710075006500720079005F0068006100730068005F007300690067006E00650064000D000A0020002000200020002000200041004E004400200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E006500640020003D00200074002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064000D000A0020002000200020004F00550054004500520020004100500050004C0059000D000A00200020002000200028000D000A0020002000200020002000200020002000530045004C00450043005400200054004F00500020002800310029000D000A00200020002000200020002000200020002000200020002000730074006100740065006D0065006E0074005F00740065007800740020003D002000710032002E00730074006100740065006D0065006E0074000D000A0020002000200020002000200020002000460052004F004D0020005B007200650070006C006100630065005F006D0065005D002000410053002000710032000D000A002000200020002000200020002000200057004800450052004500200071002E00710075006500720079005F0068006100730068005F007300690067006E006500640020003D002000710032002E00710075006500720079005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E00440020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E006500640020003D002000710032002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064000D000A002000200020002000200020002000200041004E004400200020002000710032002E00730074006100740065006D0065006E00740020004900530020004E004F00540020004E0055004C004C000D000A00200020002000200020002000200020004F00520044004500520020004200590020000D000A00200020002000200020002000200020002000200020002000710032002E006500760065006E0074005F00740069006D006500200044004500530043000D000A00200020002000200029002000410053002000710032000D000A0029000D000A00530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A00200020002000200071002E006500760065006E0074005F00740069006D0065002C000D000A00200020002000200071002E00640061007400610062006100730065005F006E0061006D0065002C000D000A002000200020002000730074006100740065006D0065006E0074005F00740065007800740020003D0020000D000A00200020002000200020002000200020005400520059005F004300410053005400280071002E00730074006100740065006D0065006E0074005F007400650078007400200041005300200078006D006C0029002C000D000A002000200020002000730071006C005F00740065007800740020003D0020000D000A00200020002000200020002000200020005400520059005F004300410053005400280071002E00730071006C005F007400650078007400200041005300200078006D006C0029002C000D000A00200020002000200071002E0065007800650063007500740069006F006E0073002C000D000A00200020002000200071002E0074006F00740061006C005F006300700075005F006D0073002C000D000A00200020002000200071002E006100760067005F006300700075005F006D0073002C000D000A00200020002000200071002E0074006F00740061006C005F006C006F0067006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E006100760067005F006C006F0067006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E0074006F00740061006C005F0070006800790073006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E006100760067005F0070006800790073006900630061006C005F00720065006100640073002C000D000A00200020002000200071002E0074006F00740061006C005F006400750072006100740069006F006E005F006D0073002C000D000A00200020002000200071002E006100760067005F006400750072006100740069006F006E005F006D0073002C000D000A00200020002000200071002E0074006F00740061006C005F007700720069007400650073002C000D000A00200020002000200071002E006100760067005F007700720069007400650073002C000D000A00200020002000200071002E0074006F00740061006C005F007300700069006C006C0073005F006D0062002C000D000A00200020002000200071002E006100760067005F007300700069006C006C0073005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E006100760067005F0075007300650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E006100760067005F006700720061006E007400650064005F006D0065006D006F00720079005F006D0062002C000D000A00200020002000200071002E0074006F00740061006C005F0072006F00770073002C000D000A00200020002000200071002E006100760067005F0072006F00770073002C000D000A00200020002000200071002E00710075006500720079005F0070006C0061006E005F0068006100730068005F007300690067006E00650064002C000D000A00200020002000200071002E00710075006500720079005F0068006100730068005F007300690067006E00650064002C000D000A00200020002000200071002E0070006C0061006E005F00680061006E0064006C0065000D000A00460052004F004D002000710075006500720079005F0072006500730075006C0074007300200041005300200071000D000A0057004800450052004500200071002E006E0020003D00200031003B00 WHERE @skip_plans = 1; INSERT #view_check (view_name, view_definition) SELECT N'HumanEvents_RecompilesByDatabaseAndObject', 0x430052004500410054004500200056004900450057002000640062006F002E00480075006D0061006E004500760065006E00740073005F005200650063006F006D00700069006C0065007300420079004400610074006100620061007300650041006E0064004F0062006A006500630074000D000A00410053000D000A00530045004C00450043005400200054004F00500020002800320031003400370034003800330036003400380029000D000A0020002000200020006D0069006E005F006500760065006E0074005F00740069006D00650020003D0020004D0049004E0028006500760065006E0074005F00740069006D00650029002C000D000A0020002000200020006D00610078005F006500760065006E0074005F00740069006D00650020003D0020004D004100580028006500760065006E0074005F00740069006D00650029002C000D000A002000200020002000640061007400610062006100730065005F006E0061006D0065002C000D000A0020002000200020006F0062006A006500630074005F006E0061006D00650020003D0020000D000A002000200020002000200020002000200043004100530045000D000A002000200020002000200020002000200020002000200020005700480045004E0020006F0062006A006500630074005F006E0061006D00650020003D0020004E00270027000D000A002000200020002000200020002000200020002000200020005400480045004E0020004E0027004E002F00410027000D000A0020002000200020002000200020002000200020002000200045004C005300450020006F0062006A006500630074005F006E0061006D0065000D000A002000200020002000200020002000200045004E0044002C000D000A00200020002000200074006F00740061006C005F0063006F006D00700069006C006500730020003D00200043004F0055004E0054005F0042004900470028002A0029002C000D000A00200020002000200074006F00740061006C005F0063006F006D00700069006C0065005F0063007000750020003D002000530055004D00280063006F006D00700069006C0065005F006300700075005F006D00730029002C000D000A0020002000200020006100760067005F0063006F006D00700069006C0065005F0063007000750020003D002000410056004700280063006F006D00700069006C0065005F006300700075005F006D00730029002C000D000A0020002000200020006D00610078005F0063006F006D00700069006C0065005F0063007000750020003D0020004D0041005800280063006F006D00700069006C0065005F006300700075005F006D00730029002C000D000A00200020002000200074006F00740061006C005F0063006F006D00700069006C0065005F006400750072006100740069006F006E0020003D002000530055004D00280063006F006D00700069006C0065005F006400750072006100740069006F006E005F006D00730029002C000D000A0020002000200020006100760067005F0063006F006D00700069006C0065005F006400750072006100740069006F006E0020003D002000410056004700280063006F006D00700069006C0065005F006400750072006100740069006F006E005F006D00730029002C000D000A0020002000200020006D00610078005F0063006F006D00700069006C0065005F006400750072006100740069006F006E0020003D0020004D0041005800280063006F006D00700069006C0065005F006400750072006100740069006F006E005F006D00730029000D000A00460052004F004D0020005B007200650070006C006100630065005F006D0065005D000D000A00470052004F00550050002000420059000D000A002000200020002000640061007400610062006100730065005F006E0061006D0065002C000D000A0020002000200020006F0062006A006500630074005F006E0061006D0065000D000A004F005200440045005200200042005900200074006F00740061006C005F0063006F006D00700069006C0065007300200044004500530043003B00 @@ -11263,19 +11275,74 @@ BEGIN CATCH IF (@output_database_name = N'' AND @output_schema_name IN (N'', N'dbo')) BEGIN - IF @debug = 1 - BEGIN - RAISERROR(@stop_sql, 0, 1) WITH NOWAIT; - RAISERROR(N'all done, stopping session', 0, 1) WITH NOWAIT; - END; - EXECUTE (@stop_sql); + /* + Only stop and drop the session if it actually got created. + If setup failed before the session existed (for example, when + @event_type = 'blocking' but the blocked process report isn't + configured), trying to stop a session that was never created + raises its own error (Msg 15151) that masks the real problem. + + This existence check runs inside the CATCH, so it gets its own + TRY/CATCH: if the check itself fails (for example, missing + permission on the catalog views), default to leaving the + session alone so the original error still surfaces via THROW. + Azure SQL DB uses database-scoped sessions, so the catalog + view differs; that's the only reason for the @azure branch. + */ + BEGIN TRY + IF @azure = 0 + BEGIN + SELECT + @session_exists = + CASE + WHEN EXISTS + ( + SELECT + 1/0 + FROM sys.server_event_sessions AS ses + WHERE ses.name = @session_name + ) + THEN 1 + ELSE 0 + END; + END; + ELSE + BEGIN + SELECT + @session_exists = + CASE + WHEN EXISTS + ( + SELECT + 1/0 + FROM sys.database_event_sessions AS ses + WHERE ses.name = @session_name + ) + THEN 1 + ELSE 0 + END; + END; + END TRY + BEGIN CATCH + SET @session_exists = 0; + END CATCH; - IF @debug = 1 + IF @session_exists = 1 BEGIN - RAISERROR(@drop_sql, 0, 1) WITH NOWAIT; - RAISERROR(N'and dropping session', 0, 1) WITH NOWAIT; + IF @debug = 1 + BEGIN + RAISERROR(@stop_sql, 0, 1) WITH NOWAIT; + RAISERROR(N'all done, stopping session', 0, 1) WITH NOWAIT; + END; + EXECUTE (@stop_sql); + + IF @debug = 1 + BEGIN + RAISERROR(@drop_sql, 0, 1) WITH NOWAIT; + RAISERROR(N'and dropping session', 0, 1) WITH NOWAIT; + END; + EXECUTE (@drop_sql); END; - EXECUTE (@drop_sql); END; THROW; @@ -11365,6 +11432,7 @@ ALTER PROCEDURE @log_table_name_prefix sysname = 'HumanEventsBlockViewer', /*prefix for all logging tables*/ @log_retention_days integer = 30, /*Number of days to keep logs, 0 = keep indefinitely*/ @max_blocking_events integer = 5000, /*maximum blocking events to analyze, 0 = unlimited*/ + @skip_execution_plans bit = 0, /*skip the execution plans result set and go straight to the rollup*/ @help bit = 0, /*get help with this procedure*/ @debug bit = 0, /*print dynamic sql and select temp table contents*/ @version varchar(30) = NULL OUTPUT, /*check the version number*/ @@ -11380,7 +11448,7 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SELECT @version = '5.6', - @version_date = '20260501'; + @version_date = '20260701'; IF @help = 1 BEGIN @@ -11419,6 +11487,7 @@ BEGIN WHEN N'@log_table_name_prefix' THEN N'prefix for all logging tables' WHEN N'@log_retention_days' THEN N'how many days of data to retain' WHEN N'@max_blocking_events' THEN N'maximum blocking events to analyze, 0 = unlimited' + WHEN N'@skip_execution_plans' THEN N'skip gathering and returning the execution plans result set and jump straight to the findings rollup' WHEN N'@help' THEN 'how you got here' WHEN N'@debug' THEN 'dumps raw temp table contents' WHEN N'@version' THEN 'OUTPUT; for support' @@ -11443,6 +11512,7 @@ BEGIN WHEN N'@log_table_name_prefix' THEN N'any valid identifier' WHEN N'@log_retention_days' THEN N'a positive integer' WHEN N'@max_blocking_events' THEN N'0 to 2147483647 (0 = unlimited)' + WHEN N'@skip_execution_plans' THEN N'0 or 1' WHEN N'@help' THEN '0 or 1' WHEN N'@debug' THEN '0 or 1' WHEN N'@version' THEN 'none; OUTPUT' @@ -11467,6 +11537,7 @@ BEGIN WHEN N'@log_table_name_prefix' THEN N'HumanEventsBlockViewer' WHEN N'@log_retention_days' THEN N'30' WHEN N'@max_blocking_events' THEN N'5000' + WHEN N'@skip_execution_plans' THEN N'0' WHEN N'@help' THEN '0' WHEN N'@debug' THEN '0' WHEN N'@version' THEN 'none; OUTPUT' @@ -11610,6 +11681,20 @@ DECLARE THEN 1 ELSE 0 END, + @product_version integer = + CONVERT + ( + integer, + PARSENAME + ( + CONVERT(sysname, SERVERPROPERTY('ProductVersion')), + 4 + ) + ), + @resolve_sql nvarchar(max), + @resolve_database_id integer, + @key_resolution_dbs CURSOR, + @page_resolution_dbs CURSOR, @azure_msg nchar(1), @session_id integer, @target_session_id integer, @@ -12898,6 +12983,16 @@ BEGIN END OPTION(RECOMPILE); + /* + When the caller only wants the blocking results, skip gathering and + returning the execution plans. system_health has no findings rollup, + so we simply return after the blocking results above. + */ + IF @skip_execution_plans = 1 + BEGIN + RETURN; + END; + IF @debug = 1 BEGIN RAISERROR('Inserting to #available_plans_sh', 0, 1) WITH NOWAIT; @@ -13549,6 +13644,16 @@ SELECT kheb.database_name, kheb.object_id, contentious_object = CONVERT(nvarchar(4000), NULL), + /* wait_resource-decoded contended object, populated after this SELECT. Typed NULLs here + so the columns exist at compile time (ALTER + same-batch reference would not compile). + lock_type holds the resource-type token, wide enough for the longest (ALLOCATION_UNIT). */ + lock_type = CONVERT(varchar(32), NULL), + frag = CONVERT(nvarchar(1024), NULL), + resource_database_id = CONVERT(integer, NULL), + resource_hobt_id = CONVERT(bigint, NULL), + resource_file_id = CONVERT(integer, NULL), + resource_page_id = CONVERT(bigint, NULL), + resource_object_id = CONVERT(integer, NULL), kheb.activity, blocking_tree = REPLICATE(' > ', kheb.blocking_level) + @@ -13739,36 +13844,245 @@ BEGIN RAISERROR('Updating #blocks contentious_object column', 0, 1) WITH NOWAIT; END; +/* +Resolve the contended object from wait_resource. The blocked_process_report event's +object_id is unreliable for KEY/PAGE/RID lock waits (it reports 0 or a non-object id), +so decode the lock resource string instead: + KEY: db:hobt (hash) -> sys.partitions(hobt_id) -> object_id (per contended db) + OBJECT: db:objid[:part] -> object_id directly + PAGE: db:file:page -> sys.dm_db_page_info -> object_id (2019+, VIEW DATABASE STATE) + RID: db:file:page:slot -> as PAGE +Anything else (DATABASE, XACT, METADATA, APPLICATION, HOBT, ...) has no user object and +keeps the 'Unresolved' sentinel the findings/output logic depends on. Compound resources +('XACT: ... KEY: ...') resolve off the embedded KEY/PAGE token. The resolved value stays +plain schema.object so the @object_name filter still matches. +*/ + +/* Classify by the resolvable token (prefers an embedded KEY/PAGE in a compound resource). */ UPDATE b SET - b.contentious_object = - ISNULL - ( - co.contentious_object, - N'Unresolved: ' + - N'database: ' + - ISNULL(b.database_name, N'unknown') + - N' object_id: ' + - ISNULL(CONVERT(nvarchar(20), b.object_id), N'unknown') - ) + b.lock_type = + CASE + WHEN b.wait_resource LIKE N'%KEY: %' THEN 'KEY' + WHEN b.wait_resource LIKE N'%OBJECT: %' THEN 'OBJECT' + WHEN b.wait_resource LIKE N'%RID: %' THEN 'RID' + WHEN b.wait_resource LIKE N'%PAGE: %' THEN 'PAGE' + ELSE LEFT(UPPER(LEFT(b.wait_resource, CHARINDEX(N':', b.wait_resource + N':') - 1)), 32) + END +FROM #blocks AS b +WHERE b.wait_resource IS NOT NULL +AND b.wait_resource <> N'' +OPTION(RECOMPILE); + +/* Fragment after the ': ' prefix (resolvable types only). */ +UPDATE + b +SET + b.frag = + SUBSTRING + ( + b.wait_resource, + CHARINDEX(b.lock_type + N': ', b.wait_resource) + LEN(b.lock_type) + 2, + 1024 + ) +FROM #blocks AS b +WHERE b.lock_type IN ('KEY', 'OBJECT', 'RID', 'PAGE') +OPTION(RECOMPILE); + +/* Database id = first token of the fragment. */ +UPDATE + b +SET + b.resource_database_id = + TRY_CONVERT(integer, LEFT(b.frag, CHARINDEX(N':', b.frag + N':') - 1)) +FROM #blocks AS b +WHERE b.frag IS NOT NULL +OPTION(RECOMPILE); + +/* KEY: hobt id is token 2, up to the space before the row hash. */ +UPDATE + b +SET + b.resource_hobt_id = + TRY_CONVERT(bigint, LTRIM(LEFT(r.rest, CHARINDEX(N' ', r.rest + N' ') - 1))) FROM #blocks AS b CROSS APPLY ( SELECT - contentious_object = - OBJECT_SCHEMA_NAME - ( - b.object_id, - b.database_id - ) + - N'.' + - OBJECT_NAME - ( - b.object_id, - b.database_id - ) -) AS co + rest = SUBSTRING(b.frag, CHARINDEX(N':', b.frag) + 1, 1024) +) AS r +WHERE b.lock_type = 'KEY' +OPTION(RECOMPILE); + +/* OBJECT: object id is token 2. */ +UPDATE + b +SET + b.resource_object_id = + TRY_CONVERT(integer, LEFT(r.rest, CHARINDEX(N':', r.rest + N':') - 1)) +FROM #blocks AS b +CROSS APPLY +( + SELECT + rest = SUBSTRING(b.frag, CHARINDEX(N':', b.frag) + 1, 1024) +) AS r +WHERE b.lock_type = 'OBJECT' +OPTION(RECOMPILE); + +/* PAGE / RID: file id is token 2, page id is token 3. */ +UPDATE + b +SET + b.resource_file_id = + TRY_CONVERT(integer, LEFT(r.rest, CHARINDEX(N':', r.rest + N':') - 1)), + b.resource_page_id = + TRY_CONVERT(bigint, LEFT(p.rest2, CHARINDEX(N':', p.rest2 + N':') - 1)) +FROM #blocks AS b +CROSS APPLY +( + SELECT + rest = SUBSTRING(b.frag, CHARINDEX(N':', b.frag) + 1, 1024) +) AS r +CROSS APPLY +( + SELECT + rest2 = SUBSTRING(r.rest, CHARINDEX(N':', r.rest) + 1, 1024) +) AS p +WHERE b.lock_type IN ('PAGE', 'RID') +OPTION(RECOMPILE); + +/* KEY -> object_id via sys.partitions, per contended database (no cross-db form of the view). */ +SET @key_resolution_dbs = + CURSOR + LOCAL FAST_FORWARD + FOR + SELECT DISTINCT + b.resource_database_id + FROM #blocks AS b + WHERE b.lock_type = 'KEY' + AND b.resource_database_id IS NOT NULL + AND b.resource_hobt_id IS NOT NULL; + +OPEN @key_resolution_dbs; +FETCH NEXT FROM @key_resolution_dbs INTO @resolve_database_id; +WHILE @@FETCH_STATUS = 0 +BEGIN + IF DB_NAME(@resolve_database_id) IS NOT NULL + AND DATABASEPROPERTYEX(DB_NAME(@resolve_database_id), 'Status') = N'ONLINE' + BEGIN + SET @resolve_sql = N' + BEGIN TRY + UPDATE + b + SET + b.resource_object_id = p.object_id + FROM #blocks AS b + JOIN ' + QUOTENAME(DB_NAME(@resolve_database_id)) + N'.sys.partitions AS p + ON p.hobt_id = b.resource_hobt_id + WHERE b.lock_type = ''KEY'' + AND b.resource_database_id = @resolve_database_id + OPTION(RECOMPILE); + END TRY + BEGIN CATCH + /* no metadata access to the database -> leave for labeling */ + END CATCH;'; + + EXECUTE sys.sp_executesql + @resolve_sql, + N'@resolve_database_id integer', + @resolve_database_id; + END; + FETCH NEXT FROM @key_resolution_dbs INTO @resolve_database_id; +END; + +/* PAGE / RID -> object_id via sys.dm_db_page_info (2019+ or Azure SQL DB; needs VIEW DATABASE + STATE, so TRY/CATCH per database). @product_version reads 12 on Azure SQL DB regardless of + engine, so @azure carries the gate there. */ +IF @product_version >= 15 +OR @azure = 1 +BEGIN + SET @page_resolution_dbs = + CURSOR + LOCAL FAST_FORWARD + FOR + SELECT DISTINCT + b.resource_database_id + FROM #blocks AS b + WHERE b.lock_type IN ('PAGE', 'RID') + AND b.resource_database_id IS NOT NULL + AND b.resource_page_id IS NOT NULL; + + OPEN @page_resolution_dbs; + FETCH NEXT FROM @page_resolution_dbs INTO @resolve_database_id; + WHILE @@FETCH_STATUS = 0 + BEGIN + IF DB_NAME(@resolve_database_id) IS NOT NULL + AND DATABASEPROPERTYEX(DB_NAME(@resolve_database_id), 'Status') = N'ONLINE' + BEGIN + SET @resolve_sql = N' + BEGIN TRY + UPDATE + b + SET + b.resource_object_id = pi.object_id + FROM #blocks AS b + CROSS APPLY sys.dm_db_page_info(b.resource_database_id, b.resource_file_id, b.resource_page_id, ''LIMITED'') AS pi + WHERE b.lock_type IN (''PAGE'', ''RID'') + AND b.resource_database_id = @resolve_database_id + OPTION(RECOMPILE); + END TRY + BEGIN CATCH + /* no VIEW DATABASE STATE / page reallocated -> leave for labeling */ + END CATCH;'; + + EXECUTE sys.sp_executesql + @resolve_sql, + N'@resolve_database_id integer', + @resolve_database_id; + END; + FETCH NEXT FROM @page_resolution_dbs INTO @resolve_database_id; + END; +END; + +/* Format (plain schema.object so the @object_name filter still matches) + label. Order: + wait_resource-decoded object, then the event object_id (the previous behavior) as a fallback, + then the 'Unresolved' sentinel the findings/output logic keys on. */ +UPDATE + b +SET + b.contentious_object = + COALESCE + ( + CASE + WHEN b.resource_object_id > 0 + AND OBJECT_NAME(b.resource_object_id, b.resource_database_id) IS NOT NULL + THEN CONCAT + ( + OBJECT_SCHEMA_NAME(b.resource_object_id, b.resource_database_id), + N'.', + OBJECT_NAME(b.resource_object_id, b.resource_database_id) + ) + END, + CASE + WHEN b.object_id > 0 + AND OBJECT_NAME(b.object_id, b.database_id) IS NOT NULL + THEN CONCAT + ( + OBJECT_SCHEMA_NAME(b.object_id, b.database_id), + N'.', + OBJECT_NAME(b.object_id, b.database_id) + ) + END, + N'Unresolved: ' + + CASE + WHEN b.lock_type IS NULL + THEN N'' + ELSE LOWER(b.lock_type) + N' lock, ' + END + + N'database: ' + ISNULL(b.database_name, N'unknown') + ) +FROM #blocks AS b OPTION(RECOMPILE); /*Either return results or log to a table*/ @@ -13944,6 +14258,16 @@ when not logging to a table */ IF @log_to_table = 0 BEGIN + /* + When the caller only wants the findings rollup, skip gathering and + returning the execution plans result set entirely and jump straight + to the blocking rollup below. + */ + IF @skip_execution_plans = 1 + BEGIN + GOTO BlockingRollup; + END; + IF @debug = 1 BEGIN RAISERROR('Inserting #available_plans', 0, 1) WITH NOWAIT; @@ -14192,6 +14516,8 @@ BEGIN ap.avg_worker_time_ms DESC OPTION(RECOMPILE, LOOP JOIN, HASH JOIN); + BlockingRollup: + /* Capture actual date range and event count from the data */ SELECT @actual_start_date = MIN(event_time), @@ -14280,7 +14606,12 @@ BEGIN sort_order = ROW_NUMBER() OVER (ORDER BY COUNT_BIG(DISTINCT b.transaction_id) DESC) FROM #blocks AS b - WHERE (b.database_name = @database_name + /* Genuine lock contention only. Non-lock and self-referential reports are + counted separately in check_id 10; IS NULL keeps unknown-type rows here + so nothing is silently dropped. */ + WHERE (b.resource_owner_type = N'LOCK' + OR b.resource_owner_type IS NULL) + AND (b.database_name = @database_name OR @database_name IS NULL) AND (b.contentious_object = @object_name OR @object_name IS NULL) @@ -14327,7 +14658,12 @@ BEGIN sort_order = ROW_NUMBER() OVER (ORDER BY COUNT_BIG(DISTINCT b.transaction_id) DESC) FROM #blocks AS b - WHERE (b.database_name = @database_name + /* Genuine lock contention only. Non-lock and self-referential reports are + counted separately in check_id 10; IS NULL keeps unknown-type rows here + so nothing is silently dropped. */ + WHERE (b.resource_owner_type = N'LOCK' + OR b.resource_owner_type IS NULL) + AND (b.database_name = @database_name OR @database_name IS NULL) AND (b.contentious_object = @object_name OR @object_name IS NULL) @@ -15151,6 +15487,65 @@ BEGIN ls.stmtstart, ls.stmtend, bl.transaction_id + ), + per_victim_named AS + ( + /* + Resolve the lead blocker's identity the same way the detail + output does (see the #blocks query_text resolution above): when + the captured inputbuf is a stored procedure call of the form + "Proc [Database Id = N Object Id = M]", turn it into + schema.procedure via OBJECT_SCHEMA_NAME / OBJECT_NAME instead of + dropping the raw "Proc [Database Id = ...]" marker into the + rollup. Falls back to the raw inputbuf text for ad hoc + statements, or when the object can't be resolved (procedure + dropped, different database context, etc.). + */ + SELECT + pv.*, + lead_object_name = + CASE + WHEN pv.query_text_pre LIKE @inputbuf_bom + N'Proc |[Database Id = %' ESCAPE N'|' + THEN + ISNULL + ( + OBJECT_SCHEMA_NAME + ( + SUBSTRING + ( + pv.query_text_pre, + CHARINDEX(N'Object Id = ', pv.query_text_pre) + 12, + LEN(pv.query_text_pre) - (CHARINDEX(N'Object Id = ', pv.query_text_pre) + 12) + ), + SUBSTRING + ( + pv.query_text_pre, + CHARINDEX(N'Database Id = ', pv.query_text_pre) + 14, + CHARINDEX(N'Object Id', pv.query_text_pre) - (CHARINDEX(N'Database Id = ', pv.query_text_pre) + 14) + ) + ) + + N'.' + + OBJECT_NAME + ( + SUBSTRING + ( + pv.query_text_pre, + CHARINDEX(N'Object Id = ', pv.query_text_pre) + 12, + LEN(pv.query_text_pre) - (CHARINDEX(N'Object Id = ', pv.query_text_pre) + 12) + ), + SUBSTRING + ( + pv.query_text_pre, + CHARINDEX(N'Database Id = ', pv.query_text_pre) + 14, + CHARINDEX(N'Object Id', pv.query_text_pre) - (CHARINDEX(N'Database Id = ', pv.query_text_pre) + 14) + ) + ), + pv.query_text_pre + ) + ELSE + pv.query_text_pre + END + FROM per_victim AS pv ) INSERT #block_findings @@ -15175,7 +15570,7 @@ BEGIN ( ISNULL ( - MAX(pv.query_text_pre), + MAX(pv.lead_object_name), N'[Unknown]' ), NCHAR(13), @@ -15282,7 +15677,7 @@ BEGIN ) ) DESC ) - FROM per_victim AS pv + FROM per_victim_named AS pv GROUP BY pv.database_name, pv.sql_handle, @@ -15297,6 +15692,63 @@ BEGIN ) OPTION(RECOMPILE); + IF @debug = 1 + BEGIN + RAISERROR('Inserting #block_findings, check_id 10', 0, 1) WITH NOWAIT; + END; + + /* + Non-lock and self-referential blocked process reports. + + The blocked process monitor fires for any task that waits longer than + the configured blocked process threshold, not only lock waits. So a long + memory grant (RESOURCE_SEMAPHORE), parallelism (CXPACKET/exchange), or + other non-lock wait surfaces as a blocked process report, frequently with + the blocked and blocking process being the same session (a session + "blocking itself"). These carry a real signal but are not lock contention, + so they would otherwise be miscounted alongside genuine lock blocking in + the findings above. resource_owner_type is LOCK only for real lock waits; + every other value (GENERIC, EXCHANGE, THREAD, etc.) is a non-lock wait. + */ + INSERT + #block_findings + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = + 10, + database_name = + ISNULL(b.database_name, N'unknown'), + object_name = + N'-', + finding_group = + N'Non-Lock and Self Blocking', + finding = + N'The database ' + + ISNULL(b.database_name, N'unknown') + + N' has had ' + + CONVERT(nvarchar(20), COUNT_BIG(DISTINCT b.event_time)) + + N' blocked process report(s) for non-lock waits such as memory grants, parallelism, or other non-lock resources. ' + + N'The blocked process monitor fires for any task that waits longer than the configured blocked process threshold, not only lock waits, ' + + N'so these frequently show a session blocking itself and do not indicate lock contention.', + sort_order = + ROW_NUMBER() OVER (ORDER BY COUNT_BIG(DISTINCT b.event_time) DESC) + FROM #blocks AS b + WHERE b.resource_owner_type <> N'LOCK' + AND (b.database_name = @database_name + OR @database_name IS NULL) + AND (b.contentious_object = @object_name + OR @object_name IS NULL) + GROUP BY + b.database_name + OPTION(RECOMPILE); + IF @debug = 1 BEGIN RAISERROR('Inserting #block_findings, check_id 1000', 0, 1) WITH NOWAIT; @@ -15519,6 +15971,8 @@ BEGIN END; END; --Final End GO +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; SET ANSI_WARNINGS ON; SET ARITHABORT ON; SET CONCAT_NULL_YIELDS_NULL ON; @@ -15580,8 +16034,8 @@ ALTER PROCEDURE @min_rows bigint = 0, /*only look at indexes with a minimum number of rows*/ @dedupe_only bit = 'false', /*only perform deduplication, don't mark unused indexes for removal*/ @get_all_databases bit = 'false', /*looks for all accessible user databases and returns combined results*/ - @include_databases nvarchar(MAX) = NULL, /*comma-separated list of databases to include (only when @get_all_databases = 1)*/ - @exclude_databases nvarchar(MAX) = NULL, /*comma-separated list of databases to exclude (only when @get_all_databases = 1)*/ + @include_databases nvarchar(max) = NULL, /*comma-separated list of databases to include (only when @get_all_databases = 1)*/ + @exclude_databases nvarchar(max) = NULL, /*comma-separated list of databases to exclude (only when @get_all_databases = 1)*/ @sort_order varchar(20) = 'default', /*controls final result ordering: default (script type first) or object (cluster rows for the same index; Key Subset disables sort under their replacement)*/ @help bit = 'false', /*learn about the procedure and parameters*/ @debug bit = 'false', /*print dynamic sql, show temp table contents*/ @@ -15594,8 +16048,8 @@ BEGIN SET NOCOUNT ON; BEGIN TRY SELECT - @version = '2.6', - @version_date = '20260501'; + @version = '2.7', + @version_date = '20260701'; IF /* Check SQL Server 2012+ for FORMAT and CONCAT functions */ @@ -15779,7 +16233,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. DECLARE /*general script variables*/ - @sql nvarchar(MAX) = N'', + @sql nvarchar(max) = N'', @object_id integer = NULL, @full_object_name nvarchar(768) = NULL, @uptime_warning bit = 0, /* Will set after @uptime_days is calculated */ @@ -15903,7 +16357,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @current_database_name sysname, @current_database_id integer, @error_msg nvarchar(2048), - @conflict_list nvarchar(MAX) = N'', + @conflict_list nvarchar(max) = N'', @rc bigint; /* Set uptime warning flag after @uptime_days is calculated */ @@ -16197,11 +16651,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. index_id integer NOT NULL, index_name sysname NOT NULL, is_unique bit NULL, - key_columns nvarchar(MAX) NULL, - included_columns nvarchar(MAX) NULL, - filter_definition nvarchar(MAX) NULL, + key_columns nvarchar(max) NULL, + included_columns nvarchar(max) NULL, + filter_definition nvarchar(max) NULL, /* Query plan for original CREATE INDEX statement */ - original_index_definition nvarchar(MAX) NULL, + original_index_definition nvarchar(max) NULL, /* Consolidation rule that matched (e.g., Key Duplicate, Key Subset, etc) For exact duplicates, use one of: Exact Duplicate, Reverse Duplicate, or Equal Except For Filter @@ -17195,7 +17649,28 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ps.object_id HAVING SUM(ps.row_count) >= @min_rows - ) + )'; + + /* + Only filter on index usage when the caller actually asked for a read or write + minimum. sys.dm_db_index_usage_stats has no row for an index that hasn't been + touched since usage stats were last reset, and on Azure SQL DB / Hyperscale + those stats reset on every failover and scaling operation. Appending this + EXISTS unconditionally drops every index whenever the DMV is empty, which + silently empties #filtered_objects and makes the procedure exit with no result + set and no error. With the defaults (@min_reads = 0, @min_writes = 0) we want + to analyze every index, including never-used ones (the whole point of the tool). + @min_reads and @min_writes are validated to non-NULL, non-negative above. + */ + IF @min_reads > 0 + OR @min_writes > 0 + BEGIN + IF @debug = 1 + BEGIN + RAISERROR('adding index usage reads/writes filter', 0, 0) WITH NOWAIT; + END; + + SET @sql += N' AND EXISTS ( SELECT @@ -17209,7 +17684,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SUM(ius.user_seeks + ius.user_scans + ius.user_lookups) >= @min_reads OR SUM(ius.user_updates) >= @min_writes - ) + )'; + END; + + SET @sql += N' OPTION(RECOMPILE); '; @@ -17256,15 +17734,16 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. BEGIN IF @debug = 1 BEGIN - RAISERROR('No rows inserted into #filtered_objects from %s, continuing to next database...', 10, 0, @current_database_name) WITH NOWAIT; + RAISERROR('No rows inserted into #filtered_objects from %s; no indexes met the analysis criteria.', 10, 0, @current_database_name) WITH NOWAIT; END; - IF @get_all_databases = 0 - BEGIN - RETURN; - END; - - /* Get the next database and continue the loop */ + /* + Advance the cursor instead of bare-RETURNing. A bare RETURN here (the old + single-database behavior) exited the procedure with no result set and no + error. Falling through lets the database surface in the 'DATABASES WITH NO + QUALIFYING OBJECTS' result set below, so the caller always gets feedback. In + single-database mode the cursor is now exhausted and the loop ends normally. + */ FETCH NEXT FROM @database_cursor INTO @@ -17959,7 +18438,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. JOIN ' + QUOTENAME(@current_database_name) + CONVERT ( - nvarchar(MAX), + nvarchar(max), N'.sys.columns AS c ON ic.object_id = c.object_id AND ic.column_id = c.column_id @@ -17987,7 +18466,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ) + QUOTENAME(@current_database_name) + CONVERT ( - nvarchar(MAX), + nvarchar(max), N'.sys.dm_db_partition_stats ps WHERE ps.object_id = i.object_id AND ps.index_id IN (0, 1) @@ -18496,8 +18975,37 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THEN N' WHERE ' + id1.filter_definition ELSE N'' END + + /* Place the index back on its own filegroup or partition scheme */ + CASE + WHEN psfg.partition_function_name IS NOT NULL + THEN N' ON ' + + QUOTENAME(psfg.built_on) + + N'(' + + ISNULL(psfg.partition_columns, N'') + + N')' + WHEN psfg.built_on IS NOT NULL + THEN N' ON ' + + QUOTENAME(psfg.built_on) + ELSE N'' + END + N';' FROM #index_details id1 + LEFT JOIN + ( + /* One row per index carrying its storage/partition layout */ + SELECT + ps.index_hash, + ps.built_on, + ps.partition_function_name, + ps.partition_columns + FROM #partition_stats AS ps + GROUP BY + ps.index_hash, + ps.built_on, + ps.partition_function_name, + ps.partition_columns + ) AS psfg + ON psfg.index_hash = id1.index_hash WHERE id1.is_eligible_for_dedupe = 1 GROUP BY id1.schema_name, @@ -18509,7 +19017,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. id1.object_id, id1.index_id, id1.filter_definition, - id1.is_unique_constraint + id1.is_unique_constraint, + psfg.built_on, + psfg.partition_function_name, + psfg.partition_columns OPTION(RECOMPILE); SET @rc = ROWCOUNT_BIG(); @@ -18557,7 +19068,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. /* Find column names mentioned in filter_definition that aren''t already key or included columns */ SELECT N'', '' + - c.name + QUOTENAME(c.name) FROM ' + QUOTENAME(@current_database_name) + N'.sys.columns AS c WHERE c.object_id = ia.object_id AND ia.filter_definition LIKE N''%'' + c.name + N''%'' COLLATE DATABASE_DEFAULT @@ -19913,7 +20424,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. XML PATH(''), TYPE - ).value('.', 'nvarchar(MAX)'), + ).value('.', 'nvarchar(max)'), 1, 2, N'' @@ -20420,6 +20931,19 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THEN ', DATA_COMPRESSION = PAGE' ELSE N'' END + + CASE + WHEN @supports_optimize_for_sequential_key = 1 + AND EXISTS + ( + SELECT + 1/0 + FROM #index_details AS id_ofsk + WHERE id_ofsk.index_hash = ia.index_hash + AND id_ofsk.optimize_for_sequential_key = 1 + ) + THEN N', OPTIMIZE_FOR_SEQUENTIAL_KEY = ON' + ELSE N'' + END + N')' + CASE WHEN ps.partition_function_name IS NOT NULL @@ -20798,7 +21322,20 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2, '' ) + - N');' + N')' + + CASE + WHEN psfg.partition_function_name IS NOT NULL + THEN N' ON ' + + QUOTENAME(psfg.built_on) + + N'(' + + ISNULL(psfg.partition_columns, N'') + + N')' + WHEN psfg.built_on IS NOT NULL + THEN N' ON ' + + QUOTENAME(psfg.built_on) + ELSE N'' + END + + N';' WHEN id.is_primary_key = 0 THEN N'CREATE ' + CASE @@ -20845,7 +21382,20 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2, '' ) + - N');' + N')' + + CASE + WHEN psfg.partition_function_name IS NOT NULL + THEN N' ON ' + + QUOTENAME(psfg.built_on) + + N'(' + + ISNULL(psfg.partition_columns, N'') + + N')' + WHEN psfg.built_on IS NOT NULL + THEN N' ON ' + + QUOTENAME(psfg.built_on) + ELSE N'' + END + + N';' ELSE N'' END FROM #filtered_objects AS fo @@ -20858,6 +21408,28 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ON ce.database_id = fo.database_id AND ce.object_id = fo.object_id AND ce.index_id = fo.index_id + LEFT JOIN + ( + /* One row per index carrying its storage/partition layout */ + SELECT + ps.database_id, + ps.object_id, + ps.index_id, + ps.built_on, + ps.partition_function_name, + ps.partition_columns + FROM #partition_stats AS ps + GROUP BY + ps.database_id, + ps.object_id, + ps.index_id, + ps.built_on, + ps.partition_function_name, + ps.partition_columns + ) AS psfg + ON psfg.database_id = fo.database_id + AND psfg.object_id = fo.object_id + AND psfg.index_id = fo.index_id WHERE ( fo.index_id = 1 /* Clustered indexes only */ @@ -21436,7 +22008,21 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THEN N'ON' ELSE N'OFF' END + - N', DATA_COMPRESSION = PAGE);' + N', DATA_COMPRESSION = PAGE' + + CASE + WHEN @supports_optimize_for_sequential_key = 1 + AND EXISTS + ( + SELECT + 1/0 + FROM #index_details AS id_ofsk + WHERE id_ofsk.index_hash = ia.index_hash + AND id_ofsk.optimize_for_sequential_key = 1 + ) + THEN N', OPTIMIZE_FOR_SEQUENTIAL_KEY = ON' + ELSE N'' + END + + N');' ELSE NULL END FROM #index_analysis AS ia @@ -21493,7 +22079,419 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ) OPTION(RECOMPILE); - /* Insert overall summary information */ + /* + Table-level reporting statistics. + + All three reporting levels (TABLE, DATABASE, SUMMARY) are built bottom-up + from #index_analysis restricted to nonclustered indexes (index_id > 1) so + the numbers reconcile exactly: SUM(TABLE) = DATABASE = SUMMARY. The + analyzed_indexes spine produces one row per analyzed index with its + metrics pre-aggregated, which avoids the partition and key-column fan-out + that previously inflated the rolled-up index counts and write totals. + */ + IF @debug = 1 + BEGIN + RAISERROR('Generating #index_reporting_stats insert, TABLE', 0, 0) WITH NOWAIT; + END; + + WITH + analyzed_indexes AS + ( + SELECT + ia.database_name, + ia.schema_name, + ia.table_name, + ia.object_id, + ia.action, + index_size_gb = ISNULL(pps.total_space_gb, 0), + can_compress = ISNULL(ce.can_compress, 0), + user_seeks = ISNULL(us.user_seeks, 0), + user_scans = ISNULL(us.user_scans, 0), + user_lookups = ISNULL(us.user_lookups, 0), + user_updates = ISNULL(us.user_updates, 0), + range_scan_count = ISNULL(os.range_scan_count, 0), + singleton_lookup_count = ISNULL(os.singleton_lookup_count, 0), + row_lock_count = ISNULL(os.row_lock_count, 0), + row_lock_wait_count = ISNULL(os.row_lock_wait_count, 0), + row_lock_wait_in_ms = ISNULL(os.row_lock_wait_in_ms, 0), + page_lock_count = ISNULL(os.page_lock_count, 0), + page_lock_wait_count = ISNULL(os.page_lock_wait_count, 0), + page_lock_wait_in_ms = ISNULL(os.page_lock_wait_in_ms, 0), + page_latch_wait_count = ISNULL(os.page_latch_wait_count, 0), + page_latch_wait_in_ms = ISNULL(os.page_latch_wait_in_ms, 0), + page_io_latch_wait_count = ISNULL(os.page_io_latch_wait_count, 0), + page_io_latch_wait_in_ms = ISNULL(os.page_io_latch_wait_in_ms, 0), + forwarded_fetch_count = ISNULL(os.forwarded_fetch_count, 0), + leaf_insert_count = ISNULL(os.leaf_insert_count, 0), + leaf_update_count = ISNULL(os.leaf_update_count, 0), + leaf_delete_count = ISNULL(os.leaf_delete_count, 0) + FROM #index_analysis AS ia + LEFT JOIN + ( + /* Roll partition rows up to one size per index */ + SELECT + ps.index_hash, + total_space_gb = SUM(ps.total_space_gb) + FROM #partition_stats AS ps + GROUP BY + ps.index_hash + ) AS pps + ON pps.index_hash = ia.index_hash + LEFT JOIN #compression_eligibility AS ce + ON ce.index_hash = ia.index_hash + LEFT JOIN #usage_stats AS us + ON us.object_id = ia.object_id + AND us.index_id = ia.index_id + LEFT JOIN #operational_stats AS os + ON os.index_hash = ia.index_hash + /* + index_id > 0 keeps heaps out but includes clustered indexes. #index_analysis + only carries clustered indexes that are compression candidates (see the + clustered-index insert), so this counts every index the tool actually + examined and can act on: nonclustered dedup candidates plus clustered + compression candidates. That makes the headline reflect tables we looked + at for compression even when they have no nonclustered indexes, and lets + compressable_indexes line up with the compression scripts. + */ + WHERE ia.database_id = @current_database_id + AND ia.index_id > 0 + ), + analyzed_table_rows AS + ( + /* Clustered/heap row count per table, summed across partitions */ + SELECT + ps.object_id, + table_rows = SUM(ps.total_rows) + FROM #partition_stats AS ps + WHERE ps.index_id IN (0, 1) + GROUP BY + ps.object_id + ) + INSERT INTO + #index_reporting_stats + WITH + (TABLOCK) + ( + summary_level, + database_name, + schema_name, + table_name, + tables_analyzed, + index_count, + total_size_gb, + total_rows, + indexes_to_disable, + indexes_to_merge, + compressable_indexes, + unused_indexes, + unused_size_gb, + compression_min_savings_gb, + compression_max_savings_gb, + total_min_savings_gb, + total_max_savings_gb, + total_reads, + total_writes, + user_seeks, + user_scans, + user_lookups, + user_updates, + range_scan_count, + singleton_lookup_count, + row_lock_count, + row_lock_wait_count, + row_lock_wait_in_ms, + page_lock_count, + page_lock_wait_count, + page_lock_wait_in_ms, + page_latch_wait_count, + page_latch_wait_in_ms, + page_io_latch_wait_count, + page_io_latch_wait_in_ms, + forwarded_fetch_count, + leaf_insert_count, + leaf_update_count, + leaf_delete_count + ) + SELECT + summary_level = 'TABLE', + ai.database_name, + ai.schema_name, + ai.table_name, + tables_analyzed = 1, + index_count = COUNT_BIG(*), + total_size_gb = SUM(ai.index_size_gb), + total_rows = MAX(atr.table_rows), + indexes_to_disable = + SUM + ( + CASE + WHEN ai.action = N'DISABLE' + THEN 1 + ELSE 0 + END + ), + indexes_to_merge = + SUM + ( + CASE + WHEN ai.action IN (N'MERGE INCLUDES', N'MAKE UNIQUE') + THEN 1 + ELSE 0 + END + ), + compressable_indexes = + SUM + ( + CASE + WHEN ai.can_compress = 1 + THEN 1 + ELSE 0 + END + ), + unused_indexes = + SUM + ( + CASE + WHEN ai.action = N'DISABLE' + THEN 1 + ELSE 0 + END + ), + unused_size_gb = + SUM + ( + CASE + WHEN ai.action = N'DISABLE' + THEN ai.index_size_gb + ELSE 0 + END + ), + compression_min_savings_gb = + SUM + ( + CASE + WHEN (ai.action IS NULL OR ai.action = N'KEEP') + AND ai.can_compress = 1 + THEN ai.index_size_gb * 0.20 + ELSE 0 + END + ), + compression_max_savings_gb = + SUM + ( + CASE + WHEN (ai.action IS NULL OR ai.action = N'KEEP') + AND ai.can_compress = 1 + THEN ai.index_size_gb * 0.60 + ELSE 0 + END + ), + total_min_savings_gb = + SUM + ( + CASE + WHEN ai.action = N'DISABLE' + THEN ai.index_size_gb + WHEN (ai.action IS NULL OR ai.action = N'KEEP') + AND ai.can_compress = 1 + THEN ai.index_size_gb * 0.20 + ELSE 0 + END + ), + total_max_savings_gb = + SUM + ( + CASE + WHEN ai.action = N'DISABLE' + THEN ai.index_size_gb + WHEN (ai.action IS NULL OR ai.action = N'KEEP') + AND ai.can_compress = 1 + THEN ai.index_size_gb * 0.60 + ELSE 0 + END + ), + total_reads = SUM(ai.user_seeks + ai.user_scans + ai.user_lookups), + total_writes = SUM(ai.user_updates), + user_seeks = SUM(ai.user_seeks), + user_scans = SUM(ai.user_scans), + user_lookups = SUM(ai.user_lookups), + user_updates = SUM(ai.user_updates), + range_scan_count = SUM(ai.range_scan_count), + singleton_lookup_count = SUM(ai.singleton_lookup_count), + row_lock_count = SUM(ai.row_lock_count), + row_lock_wait_count = SUM(ai.row_lock_wait_count), + row_lock_wait_in_ms = SUM(ai.row_lock_wait_in_ms), + page_lock_count = SUM(ai.page_lock_count), + page_lock_wait_count = SUM(ai.page_lock_wait_count), + page_lock_wait_in_ms = SUM(ai.page_lock_wait_in_ms), + page_latch_wait_count = SUM(ai.page_latch_wait_count), + page_latch_wait_in_ms = SUM(ai.page_latch_wait_in_ms), + page_io_latch_wait_count = SUM(ai.page_io_latch_wait_count), + page_io_latch_wait_in_ms = SUM(ai.page_io_latch_wait_in_ms), + forwarded_fetch_count = SUM(ai.forwarded_fetch_count), + leaf_insert_count = SUM(ai.leaf_insert_count), + leaf_update_count = SUM(ai.leaf_update_count), + leaf_delete_count = SUM(ai.leaf_delete_count) + FROM analyzed_indexes AS ai + LEFT JOIN analyzed_table_rows AS atr + ON atr.object_id = ai.object_id + GROUP BY + ai.database_name, + ai.schema_name, + ai.table_name, + ai.object_id + OPTION(RECOMPILE); + + /* + Database-level reporting statistics: a straight roll-up of this database's + TABLE rows, which guarantees DATABASE = SUM(TABLE) for every column. + */ + IF @debug = 1 + BEGIN + RAISERROR('Generating #index_reporting_stats insert, DATABASE', 0, 0) WITH NOWAIT; + END; + + INSERT INTO + #index_reporting_stats + WITH + (TABLOCK) + ( + summary_level, + database_name, + tables_analyzed, + index_count, + total_size_gb, + total_rows, + indexes_to_disable, + indexes_to_merge, + compressable_indexes, + unused_indexes, + unused_size_gb, + compression_min_savings_gb, + compression_max_savings_gb, + total_min_savings_gb, + total_max_savings_gb, + total_reads, + total_writes, + user_seeks, + user_scans, + user_lookups, + user_updates, + range_scan_count, + singleton_lookup_count, + row_lock_count, + row_lock_wait_count, + row_lock_wait_in_ms, + page_lock_count, + page_lock_wait_count, + page_lock_wait_in_ms, + page_latch_wait_count, + page_latch_wait_in_ms, + page_io_latch_wait_count, + page_io_latch_wait_in_ms, + forwarded_fetch_count, + leaf_insert_count, + leaf_update_count, + leaf_delete_count + ) + SELECT + summary_level = 'DATABASE', + irs.database_name, + tables_analyzed = SUM(irs.tables_analyzed), + index_count = SUM(irs.index_count), + total_size_gb = SUM(irs.total_size_gb), + total_rows = SUM(irs.total_rows), + indexes_to_disable = SUM(irs.indexes_to_disable), + indexes_to_merge = SUM(irs.indexes_to_merge), + compressable_indexes = SUM(irs.compressable_indexes), + unused_indexes = SUM(irs.unused_indexes), + unused_size_gb = SUM(irs.unused_size_gb), + compression_min_savings_gb = SUM(irs.compression_min_savings_gb), + compression_max_savings_gb = SUM(irs.compression_max_savings_gb), + total_min_savings_gb = SUM(irs.total_min_savings_gb), + total_max_savings_gb = SUM(irs.total_max_savings_gb), + total_reads = SUM(irs.total_reads), + total_writes = SUM(irs.total_writes), + user_seeks = SUM(irs.user_seeks), + user_scans = SUM(irs.user_scans), + user_lookups = SUM(irs.user_lookups), + user_updates = SUM(irs.user_updates), + range_scan_count = SUM(irs.range_scan_count), + singleton_lookup_count = SUM(irs.singleton_lookup_count), + row_lock_count = SUM(irs.row_lock_count), + row_lock_wait_count = SUM(irs.row_lock_wait_count), + row_lock_wait_in_ms = SUM(irs.row_lock_wait_in_ms), + page_lock_count = SUM(irs.page_lock_count), + page_lock_wait_count = SUM(irs.page_lock_wait_count), + page_lock_wait_in_ms = SUM(irs.page_lock_wait_in_ms), + page_latch_wait_count = SUM(irs.page_latch_wait_count), + page_latch_wait_in_ms = SUM(irs.page_latch_wait_in_ms), + page_io_latch_wait_count = SUM(irs.page_io_latch_wait_count), + page_io_latch_wait_in_ms = SUM(irs.page_io_latch_wait_in_ms), + forwarded_fetch_count = SUM(irs.forwarded_fetch_count), + leaf_insert_count = SUM(irs.leaf_insert_count), + leaf_update_count = SUM(irs.leaf_update_count), + leaf_delete_count = SUM(irs.leaf_delete_count) + FROM #index_reporting_stats AS irs + WHERE irs.summary_level = 'TABLE' + AND irs.database_name = @current_database_name + GROUP BY + irs.database_name + OPTION(RECOMPILE); + + /* + The server-wide SUMMARY row is generated after the database cursor + completes (see below), rolling up every DATABASE row so the top line + always equals the sum of the detail. + */ + + /* We're not doing index-level summaries - focusing on database and table level reports */ + + /* + Return the consolidated results in a single result set + Results are ordered by: + 1. Summary information (overall stats, savings estimates) + 2. Merge scripts (includes merges and unique conversions) - sort_order 5 + 3. Disable scripts (for redundant indexes) - sort_order 20 + 4. Constraint scripts (for unique constraints to disable) + 5. Compression scripts (for tables eligible for compression) + 6. Partition-specific compression scripts + 7. Ineligible objects (tables that can't be compressed) + 8. Kept indexes - sort_order 95 + + Note: Merge target scripts are sorted higher in the results (sort_order 5) + so that new merged indexes are created before subset indexes are disabled. + + Within each category, indexes are sorted by size and impact for better prioritization. + */ + IF @debug = 1 + BEGIN + SELECT + table_name = '#index_reporting_stats', + irs.* + FROM #index_reporting_stats AS irs + OPTION(RECOMPILE); + + SELECT + table_name = '#index_cleanup_results', + icr.* + FROM #index_cleanup_results AS icr + OPTION(RECOMPILE); + END; + + /* Get the next database */ + FETCH NEXT + FROM @database_cursor + INTO + @current_database_name, + @current_database_id; + END; + + /* + Server-wide SUMMARY row. Built after the cursor finishes by rolling up + every DATABASE row, so SUMMARY = SUM(DATABASE) = SUM(TABLE) regardless of + how many databases were processed. + */ IF @debug = 1 BEGIN RAISERROR('Generating #index_reporting_stats insert, SUMMARY', 0, 0) WITH NOWAIT; @@ -21525,628 +22523,25 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. summary_level = 'SUMMARY', server_uptime_days = @uptime_days, uptime_warning = @uptime_warning, - tables_analyzed = - COUNT_BIG(DISTINCT CONCAT(ia.database_id, N'.', ia.schema_id, N'.', ia.object_id)), - index_count = COUNT_BIG(*), - total_size_gb = SUM(ps.total_space_gb), - indexes_to_disable = - SUM - ( - CASE - WHEN ia.action = N'DISABLE' - THEN 1 - ELSE 0 - END - ), - indexes_to_merge = - SUM - ( - CASE - WHEN ia.action IN (N'MERGE INCLUDES', N'MAKE UNIQUE') - THEN 1 - ELSE 0 - END - ), - compressable_indexes = - SUM - ( - CASE - WHEN ce.can_compress = 1 - THEN 1 - ELSE 0 - END - ), + tables_analyzed = SUM(irs.tables_analyzed), + index_count = SUM(irs.index_count), + total_size_gb = SUM(irs.total_size_gb), + indexes_to_disable = SUM(irs.unused_indexes), + indexes_to_merge = SUM(irs.indexes_to_merge), + compressable_indexes = SUM(irs.compressable_indexes), avg_indexes_per_table = - COUNT_BIG(*) * 1.0 / - NULLIF - ( - COUNT_BIG(DISTINCT CONCAT(ia.database_id, N'.', ia.schema_id, N'.', ia.object_id)), - 0 - ), - /* Space savings from cleanup - only count DISABLE actions */ - space_saved_gb = - SUM - ( - CASE - WHEN ia.action = N'DISABLE' - THEN ps.total_space_gb - ELSE 0 - END - ), - /* Conservative compression savings estimate (20%) */ - compression_min_savings_gb = - SUM - ( - CASE - WHEN (ia.action IS NULL OR ia.action = N'KEEP') - AND ce.can_compress = 1 - THEN ps.total_space_gb * 0.20 - ELSE 0 - END - ), - /* Optimistic compression savings estimate (60%) */ - compression_max_savings_gb = - SUM - ( - CASE - WHEN (ia.action IS NULL OR ia.action = N'KEEP') - AND ce.can_compress = 1 - THEN ps.total_space_gb * 0.60 - ELSE 0 - END - ), - /* Total conservative savings - only count DISABLE actions for space savings */ - total_min_savings_gb = - SUM - ( - CASE - WHEN ia.action = N'DISABLE' - THEN ps.total_space_gb - WHEN (ia.action IS NULL OR ia.action = N'KEEP') - AND ce.can_compress = 1 - THEN ps.total_space_gb * 0.20 - ELSE 0 - END - ), - /* Total optimistic savings - only count DISABLE actions for space savings */ - total_max_savings_gb = - SUM - ( - CASE - WHEN ia.action = N'DISABLE' - THEN ps.total_space_gb - WHEN (ia.action IS NULL OR ia.action = N'KEEP') - AND ce.can_compress = 1 - THEN ps.total_space_gb * 0.60 - ELSE 0 - END - ), - /* Get total rows from database unique tables */ - total_rows = - ( - SELECT - SUM(t.row_count) - FROM - ( - SELECT - ps_distinct.object_id, - row_count = - MAX - ( - CASE - WHEN ps_distinct.index_id IN (0, 1) - THEN ps_distinct.total_rows - ELSE 0 - END - ) - FROM #partition_stats AS ps_distinct - WHERE ps_distinct.index_id IN (0, 1) - GROUP BY - ps_distinct.object_id - ) AS t - ) - FROM #index_analysis AS ia - LEFT JOIN #partition_stats AS ps - ON ia.index_hash = ps.index_hash - LEFT JOIN #compression_eligibility AS ce - ON ia.database_id = ce.database_id - AND ia.object_id = ce.object_id - AND ia.index_id = ce.index_id - WHERE ia.index_id > 1 - OPTION(RECOMPILE); - - /* Return enhanced database impact summaries */ - IF @debug = 1 - BEGIN - RAISERROR('Generating enhanced summary reports', 0, 0) WITH NOWAIT; - END; - - /* Insert database-level summaries */ - IF @debug = 1 - BEGIN - RAISERROR('Generating #index_reporting_stats insert, DATABASE', 0, 0) WITH NOWAIT; - END; - - INSERT INTO - #index_reporting_stats - WITH - (TABLOCK) - ( - summary_level, - database_name, - index_count, - total_size_gb, - total_rows, - indexes_to_merge, - compressable_indexes, - unused_indexes, - unused_size_gb, - compression_min_savings_gb, - compression_max_savings_gb, - total_min_savings_gb, - total_max_savings_gb, - total_reads, - total_writes, - user_seeks, - user_scans, - user_lookups, - user_updates, - range_scan_count, - singleton_lookup_count, - row_lock_count, - row_lock_wait_count, - row_lock_wait_in_ms, - page_lock_count, - page_lock_wait_count, - page_lock_wait_in_ms, - page_latch_wait_count, - page_latch_wait_in_ms, - page_io_latch_wait_count, - page_io_latch_wait_in_ms, - forwarded_fetch_count, - leaf_insert_count, - leaf_update_count, - leaf_delete_count - ) - SELECT - summary_level = 'DATABASE', - ps.database_name, - index_count = - COUNT_BIG(DISTINCT CONCAT(ps.object_id, N'.', ps.index_id)), - total_size_gb = SUM(DISTINCT ps.total_space_gb), - /* Use a simple aggregation to avoid double-counting */ - /* Get actual row count by grabbing the real row count from clustered index/heap per table */ - total_rows = SUM(DISTINCT d.actual_rows), - indexes_to_merge = - ( - SELECT - COUNT_BIG(*) - FROM #index_analysis AS ia - WHERE ia.action IN (N'MERGE INCLUDES', N'MAKE UNIQUE') - AND ia.database_id = ps.database_id - ), - compressable_indexes = - ( - SELECT - COUNT_BIG(*) - FROM #compression_eligibility AS ce - WHERE ce.can_compress = 1 - AND ce.database_id = ps.database_id - ), - /* Use count from analysis to keep consistent with SUMMARY level */ - unused_indexes = - ( - SELECT - COUNT_BIG(*) - FROM #index_analysis AS ia - WHERE ia.action = N'DISABLE' - AND ia.database_id = ps.database_id - ), - unused_size_gb = - ( - SELECT - SUM(subps.total_space_gb) - FROM #partition_stats AS subps - JOIN #index_analysis AS subia - ON subps.index_hash = subia.index_hash - WHERE subia.action = N'DISABLE' - AND subia.database_id = ps.database_id - ), - /* Conservative compression savings estimate (20%) */ - compression_min_savings_gb = - ( - SELECT - SUM(subps.total_space_gb * 0.20) - FROM #partition_stats AS subps - JOIN #index_analysis AS subia - ON subps.index_hash = subia.index_hash - JOIN #compression_eligibility AS subce - ON subce.index_hash = subia.index_hash - WHERE (subia.action IS NULL OR subia.action = N'KEEP') - AND subce.can_compress = 1 - AND subia.database_id = ps.database_id - ), - /* Optimistic compression savings estimate (60%) */ - compression_max_savings_gb = - ( - SELECT - SUM(subps.total_space_gb * 0.60) - FROM #partition_stats AS subps - JOIN #index_analysis AS subia - ON subps.index_hash = subia.index_hash - JOIN #compression_eligibility AS subce - ON subce.index_hash = subia.index_hash - WHERE (subia.action IS NULL OR subia.action = N'KEEP') - AND subce.can_compress = 1 - AND subia.database_id = ps.database_id - ), - /* Total conservative savings */ - total_min_savings_gb = - ( - SELECT - SUM - ( - CASE - WHEN subia.action = N'DISABLE' - THEN subps.total_space_gb - WHEN (subia.action IS NULL OR subia.action = N'KEEP') - AND subce.can_compress = 1 - THEN subps.total_space_gb * 0.20 - ELSE 0 - END - ) - FROM #partition_stats AS subps - JOIN #index_analysis AS subia - ON subps.index_hash = subia.index_hash - LEFT JOIN #compression_eligibility AS subce - ON subce.index_hash = subia.index_hash - WHERE subia.database_id = ps.database_id - ), - /* Total optimistic savings */ - total_max_savings_gb = - ( - SELECT - SUM - ( - CASE - WHEN subia.action = N'DISABLE' - THEN subps.total_space_gb - WHEN (subia.action IS NULL OR subia.action = N'KEEP') - AND subce.can_compress = 1 - THEN subps.total_space_gb * 0.60 - ELSE 0 - END - ) - FROM #partition_stats AS subps - JOIN #index_analysis AS subia - ON subps.index_hash = subia.index_hash - LEFT JOIN #compression_eligibility AS subce - ON subce.index_hash = subia.index_hash - WHERE subia.database_id = ps.database_id - ), - total_reads = SUM(id.user_seeks + id.user_scans + id.user_lookups), - total_writes = SUM(id.user_updates), - user_seeks = SUM(id.user_seeks), - user_scans = SUM(id.user_scans), - user_lookups = SUM(id.user_lookups), - user_updates = SUM(id.user_updates), - range_scan_count = SUM(os.range_scan_count), - singleton_lookup_count = SUM(os.singleton_lookup_count), - row_lock_count = SUM(os.row_lock_count), - row_lock_wait_count = SUM(os.row_lock_wait_count), - row_lock_wait_in_ms = SUM(os.row_lock_wait_in_ms), - page_lock_count = SUM(os.page_lock_count), - page_lock_wait_count = SUM(os.page_lock_wait_count), - page_lock_wait_in_ms = SUM(os.page_lock_wait_in_ms), - page_latch_wait_count = SUM(os.page_latch_wait_count), - page_latch_wait_in_ms = SUM(os.page_latch_wait_in_ms), - page_io_latch_wait_count = SUM(os.page_io_latch_wait_count), - page_io_latch_wait_in_ms = SUM(os.page_io_latch_wait_in_ms), - forwarded_fetch_count = SUM(os.forwarded_fetch_count), - leaf_insert_count = SUM(os.leaf_insert_count), - leaf_update_count = SUM(os.leaf_update_count), - leaf_delete_count = SUM(os.leaf_delete_count) - FROM #partition_stats AS ps - LEFT JOIN #index_details AS id - ON id.index_hash = ps.index_hash - AND id.is_included_column = 0 - AND id.key_ordinal > 0 - LEFT JOIN #operational_stats AS os - ON os.index_hash = ps.index_hash - OUTER APPLY - ( - /* Get actual row count per table using MAX from clustered index/heap */ - SELECT - actual_rows = - MAX - ( - CASE - WHEN ps2.index_id IN (0, 1) - THEN ps2.total_rows - ELSE 0 - END - ) - FROM #partition_stats AS ps2 - WHERE ps2.database_id = ps.database_id - AND ps2.object_id = ps.object_id - AND ps2.index_id IN (0, 1) - GROUP BY - ps2.object_id - ) AS d - GROUP BY - ps.database_name, - ps.database_id + SUM(irs.index_count) * 1.0 / + NULLIF(SUM(irs.tables_analyzed), 0), + space_saved_gb = SUM(irs.unused_size_gb), + compression_min_savings_gb = SUM(irs.compression_min_savings_gb), + compression_max_savings_gb = SUM(irs.compression_max_savings_gb), + total_min_savings_gb = SUM(irs.total_min_savings_gb), + total_max_savings_gb = SUM(irs.total_max_savings_gb), + total_rows = SUM(irs.total_rows) + FROM #index_reporting_stats AS irs + WHERE irs.summary_level = 'DATABASE' OPTION(RECOMPILE); - /* Insert table-level summaries */ - IF @debug = 1 - BEGIN - RAISERROR('Generating #index_reporting_stats insert, TABLE', 0, 0) WITH NOWAIT; - END; - - INSERT INTO - #index_reporting_stats - WITH - (TABLOCK) - ( - summary_level, - database_name, - schema_name, - table_name, - index_count, - total_size_gb, - total_rows, - indexes_to_merge, - compressable_indexes, - unused_indexes, - unused_size_gb, - compression_min_savings_gb, - compression_max_savings_gb, - total_min_savings_gb, - total_max_savings_gb, - total_reads, - total_writes, - user_seeks, - user_scans, - user_lookups, - user_updates, - range_scan_count, - singleton_lookup_count, - row_lock_count, - row_lock_wait_count, - row_lock_wait_in_ms, - page_lock_count, - page_lock_wait_count, - page_lock_wait_in_ms, - page_latch_wait_count, - page_latch_wait_in_ms, - page_io_latch_wait_count, - page_io_latch_wait_in_ms, - forwarded_fetch_count, - leaf_insert_count, - leaf_update_count, - leaf_delete_count - ) - SELECT - summary_level = 'TABLE', - ps.database_name, - ps.schema_name, - ps.table_name, - index_count = COUNT_BIG(DISTINCT ps.index_id), - total_size_gb = SUM(DISTINCT ps.total_space_gb), - /* Use MAX to get the row count from the clustered index or heap */ - total_rows = - MAX - ( - CASE - WHEN ps.index_id IN (0, 1) - THEN ps.total_rows - ELSE 0 - END - ), - indexes_to_merge = - ( - SELECT - COUNT_BIG(*) - FROM #index_analysis AS ia - WHERE ia.action IN (N'MERGE INCLUDES', N'MAKE UNIQUE') - AND ia.database_id = ps.database_id - AND ia.schema_id = ps.schema_id - AND ia.object_id = ps.object_id - ), - compressable_indexes = - ( - SELECT - COUNT_BIG(*) - FROM #compression_eligibility AS ce - WHERE ce.can_compress = 1 - AND ce.database_id = ps.database_id - AND ce.schema_id = ps.schema_id - AND ce.object_id = ps.object_id - ), - /* Use count from analysis to keep consistent with SUMMARY level */ - unused_indexes = - ( - SELECT - COUNT_BIG(*) - FROM #index_analysis AS ia - WHERE ia.action = N'DISABLE' - AND ia.database_id = ps.database_id - AND ia.schema_id = ps.schema_id - AND ia.object_id = ps.object_id - ), - unused_size_gb = - ( - SELECT - SUM(subps.total_space_gb) - FROM #partition_stats AS subps - JOIN #index_analysis AS subia - ON subps.index_hash = subia.index_hash - WHERE subia.action = N'DISABLE' - AND subia.database_id = ps.database_id - AND subia.schema_id = ps.schema_id - AND subia.object_id = ps.object_id - ), - /* Conservative compression savings estimate (20%) */ - compression_min_savings_gb = - ( - SELECT - SUM(subps.total_space_gb * 0.20) - FROM #partition_stats AS subps - JOIN #index_analysis AS subia - ON subps.index_hash = subia.index_hash - JOIN #compression_eligibility AS subce - ON subce.index_hash = subia.index_hash - WHERE (subia.action IS NULL OR subia.action = N'KEEP') - AND subce.can_compress = 1 - AND subia.database_id = ps.database_id - AND subia.schema_id = ps.schema_id - AND subia.object_id = ps.object_id - ), - /* Optimistic compression savings estimate (60%) */ - compression_max_savings_gb = - ( - SELECT - SUM(subps.total_space_gb * 0.60) - FROM #partition_stats AS subps - JOIN #index_analysis AS subia - ON subps.index_hash = subia.index_hash - JOIN #compression_eligibility AS subce - ON subce.index_hash = subia.index_hash - WHERE (subia.action IS NULL OR subia.action = N'KEEP') - AND subce.can_compress = 1 - AND subia.database_id = ps.database_id - AND subia.schema_id = ps.schema_id - AND subia.object_id = ps.object_id - ), - /* Total conservative savings */ - total_min_savings_gb = - ( - SELECT - SUM - ( - CASE - WHEN subia.action = N'DISABLE' - THEN subps.total_space_gb - WHEN (subia.action IS NULL OR subia.action = N'KEEP') - AND subce.can_compress = 1 - THEN subps.total_space_gb * 0.20 - ELSE 0 - END - ) - FROM #partition_stats AS subps - JOIN #index_analysis AS subia - ON subps.index_hash = subia.index_hash - LEFT JOIN #compression_eligibility AS subce - ON subce.index_hash = subia.index_hash - WHERE subia.database_id = ps.database_id - AND subia.schema_id = ps.schema_id - AND subia.object_id = ps.object_id - ), - /* Total optimistic savings */ - total_max_savings_gb = - ( - SELECT - SUM - ( - CASE - WHEN subia.action = N'DISABLE' - THEN subps.total_space_gb - WHEN (subia.action IS NULL OR subia.action = N'KEEP') - AND subce.can_compress = 1 - THEN subps.total_space_gb * 0.60 - ELSE 0 - END - ) - FROM #partition_stats AS subps - JOIN #index_analysis AS subia - ON subps.index_hash = subia.index_hash - LEFT JOIN #compression_eligibility AS subce - ON subce.index_hash = subia.index_hash - WHERE subia.database_id = ps.database_id - AND subia.schema_id = ps.schema_id - AND subia.object_id = ps.object_id - ), - total_reads = SUM(id.user_seeks + id.user_scans + id.user_lookups), - total_writes = SUM(id.user_updates), - user_seeks = SUM(id.user_seeks), - user_scans = SUM(id.user_scans), - user_lookups = SUM(id.user_lookups), - user_updates = SUM(id.user_updates), - range_scan_count = SUM(os.range_scan_count), - singleton_lookup_count = SUM(os.singleton_lookup_count), - row_lock_count = SUM(os.row_lock_count), - row_lock_wait_count = SUM(os.row_lock_wait_count), - row_lock_wait_in_ms = SUM(os.row_lock_wait_in_ms), - page_lock_count = SUM(os.page_lock_count), - page_lock_wait_count = SUM(os.page_lock_wait_count), - page_lock_wait_in_ms = SUM(os.page_lock_wait_in_ms), - page_latch_wait_count = SUM(os.page_latch_wait_count), - page_latch_wait_in_ms = SUM(os.page_latch_wait_in_ms), - page_io_latch_wait_count = SUM(os.page_io_latch_wait_count), - page_io_latch_wait_in_ms = SUM(os.page_io_latch_wait_in_ms), - forwarded_fetch_count = SUM(os.forwarded_fetch_count), - leaf_insert_count = SUM(os.leaf_insert_count), - leaf_update_count = SUM(os.leaf_update_count), - leaf_delete_count = SUM(os.leaf_delete_count) - FROM #partition_stats AS ps - LEFT JOIN #index_details AS id - ON id.index_hash = ps.index_hash - AND id.is_included_column = 0 - AND id.key_ordinal > 0 - LEFT JOIN #operational_stats AS os - ON os.index_hash = ps.index_hash - GROUP BY - ps.database_name, - ps.database_id, - ps.schema_name, - ps.schema_id, - ps.table_name, - ps.object_id - OPTION(RECOMPILE); - - /* We're not doing index-level summaries - focusing on database and table level reports */ - - /* - Return the consolidated results in a single result set - Results are ordered by: - 1. Summary information (overall stats, savings estimates) - 2. Merge scripts (includes merges and unique conversions) - sort_order 5 - 3. Disable scripts (for redundant indexes) - sort_order 20 - 4. Constraint scripts (for unique constraints to disable) - 5. Compression scripts (for tables eligible for compression) - 6. Partition-specific compression scripts - 7. Ineligible objects (tables that can't be compressed) - 8. Kept indexes - sort_order 95 - - Note: Merge target scripts are sorted higher in the results (sort_order 5) - so that new merged indexes are created before subset indexes are disabled. - - Within each category, indexes are sorted by size and impact for better prioritization. - */ - IF @debug = 1 - BEGIN - SELECT - table_name = '#index_reporting_stats', - irs.* - FROM #index_reporting_stats AS irs - OPTION(RECOMPILE); - - SELECT - table_name = '#index_cleanup_results', - icr.* - FROM #index_cleanup_results AS icr - OPTION(RECOMPILE); - END; - - /* Get the next database */ - FETCH NEXT - FROM @database_cursor - INTO - @current_database_name, - @current_database_id; - END; - IF @debug = 1 BEGIN RAISERROR('Generating #index_cleanup_results, RESULTS', 0, 0) WITH NOWAIT; @@ -22269,7 +22664,30 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. END, /* Always order by script type within the current grouping */ ir.sort_order, - /* Default mode: within each sort_order group, prioritize by size and usage */ + /* + Default mode: within each sort_order group, prioritize by writes, + then size and usage. Writes rank first because an unused index that + absorbs heavy writes is pure write-amplification overhead, making it + the highest-value removal even when it isn't the largest on disk. + */ + CASE + WHEN @sort_order <> N'default' + THEN NULL + /* For SUMMARY, keep the original order */ + WHEN ir.result_type = 'SUMMARY' + THEN 0 + /* + Compression candidates are prioritized by size (the next key), + not by writes: the goal is reclaiming the most space, so the + largest indexes come first regardless of write volume. Returning + a constant here ties all COMPRESS / COMPRESS_PARTITION rows on + this key so the size key below becomes their primary sort. + */ + WHEN ir.result_type IN ('COMPRESS', 'COMPRESS_PARTITION') + THEN 0 + /* For script categories, order by write overhead first */ + ELSE ISNULL(ir.index_writes, 0) + END DESC, CASE WHEN @sort_order <> N'default' THEN NULL @@ -22459,17 +22877,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. database_info = irs.database_name, schema_name = N'N/A', table_name = N'N/A', - tables_analyzed = - FORMAT - ( - ( - SELECT - COUNT_BIG(DISTINCT CONCAT(ia.schema_id, N'.', ia.object_id)) - FROM #index_analysis AS ia - WHERE ia.database_name = irs.database_name - ), - 'N0' - ), + tables_analyzed = FORMAT(ISNULL(irs.tables_analyzed, 0), 'N0'), total_indexes = FORMAT(ISNULL(irs.index_count, 0), 'N0'), removable_indexes = FORMAT(ISNULL(irs.unused_indexes, 0), 'N0'), mergeable_indexes = FORMAT(ISNULL(irs.indexes_to_merge, 0), 'N0'), @@ -22591,7 +22999,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. database_info = irs.database_name, schema_name = irs.schema_name, table_name = irs.table_name, - tables_analyzed = FORMAT(1, 'N0'), + tables_analyzed = FORMAT(ISNULL(irs.tables_analyzed, 0), 'N0'), total_indexes = FORMAT(ISNULL(irs.index_count, 0), 'N0'), removable_indexes = FORMAT(ISNULL(irs.unused_indexes, 0), 'N0'), mergeable_indexes = FORMAT(ISNULL(irs.indexes_to_merge, 0), 'N0'), @@ -22929,6 +23337,82 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. fica.filter_definition, ia.original_index_definition, fica.missing_included_columns, + /* + Rebuild the index with the filter columns appended to the INCLUDE + list, using DROP_EXISTING so the existing index is replaced in a + single statement. Pieces are reused from #index_analysis, so the + key columns, existing includes, and filter all match the original. + The WITH options and partition ON clause mirror the merge/create + scripts so this advice stays consistent with the rest of the tool. + */ + create_index_script = + N'CREATE ' + + CASE + WHEN ia.is_unique = 1 + THEN N'UNIQUE ' + ELSE N'' + END + + N'NONCLUSTERED INDEX ' + + QUOTENAME(fica.index_name) + + N' ON ' + + QUOTENAME(fica.database_name) + + N'.' + + QUOTENAME(fica.schema_name) + + N'.' + + QUOTENAME(fica.table_name) + + N' (' + + ia.key_columns + + N') INCLUDE (' + + CASE + WHEN ia.included_columns IS NOT NULL + AND LEN(ia.included_columns) > 0 + THEN ia.included_columns + N', ' + fica.missing_included_columns + ELSE fica.missing_included_columns + END + + N')' + + CASE + WHEN fica.filter_definition IS NOT NULL + THEN N' WHERE ' + fica.filter_definition + ELSE N'' + END + + N' WITH (DROP_EXISTING = ON, FILLFACTOR = 100, SORT_IN_TEMPDB = ON, ONLINE = ' + + CASE + WHEN @online = 1 + THEN N'ON' + ELSE N'OFF' + END + + CASE + WHEN ce.can_compress = 1 + THEN N', DATA_COMPRESSION = PAGE' + ELSE N'' + END + + CASE + WHEN @supports_optimize_for_sequential_key = 1 + AND EXISTS + ( + SELECT + 1/0 + FROM #index_details AS id_ofsk + WHERE id_ofsk.index_hash = ia.index_hash + AND id_ofsk.optimize_for_sequential_key = 1 + ) + THEN N', OPTIMIZE_FOR_SEQUENTIAL_KEY = ON' + ELSE N'' + END + + N')' + + CASE + WHEN ps.partition_function_name IS NOT NULL + THEN N' ON ' + + QUOTENAME(ps.built_on) + + N'(' + + ISNULL(ps.partition_columns, N'') + + N')' + WHEN ps.built_on IS NOT NULL + THEN N' ON ' + + QUOTENAME(ps.built_on) + ELSE N'' + END + + N';', recommendation = 'Add filter columns to INCLUDE list to improve performance and avoid key lookups' FROM #filtered_index_columns_analysis AS fica JOIN #index_analysis AS ia @@ -22936,6 +23420,24 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. AND ia.schema_id = fica.schema_id AND ia.object_id = fica.object_id AND ia.index_id = fica.index_id + LEFT JOIN + ( + /* One row per index carrying its storage/partition layout */ + SELECT + ps.index_hash, + ps.built_on, + ps.partition_function_name, + ps.partition_columns + FROM #partition_stats AS ps + GROUP BY + ps.index_hash, + ps.built_on, + ps.partition_function_name, + ps.partition_columns + ) AS ps + ON ps.index_hash = ia.index_hash + LEFT JOIN #compression_eligibility AS ce + ON ce.index_hash = ia.index_hash WHERE fica.should_include_filter_columns = 1 ORDER BY fica.database_name, @@ -23070,7 +23572,7 @@ SET DATEFORMAT MDY; BEGIN SELECT @version = '3.6', - @version_date = '20260501'; + @version_date = '20260701'; IF @help = 1 BEGIN @@ -23772,6 +24274,8 @@ BEGIN END; END; GO +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; SET ANSI_WARNINGS ON; SET ARITHABORT ON; SET CONCAT_NULL_YIELDS_NULL ON; @@ -23839,7 +24343,7 @@ BEGIN */ SELECT @version = N'2.6', - @version_date = N'20260501'; + @version_date = N'20260701'; /* Help section, for help. @@ -28977,7 +29481,7 @@ SET LANGUAGE us_english; SELECT @version = '6.6', - @version_date = '20260501'; + @version_date = '20260701'; IF @help = 1 @@ -31414,6 +31918,87 @@ OPTION(MAXDOP 1, RECOMPILE);', PATH('tempdb_query_activity'), TYPE + ), + /* + Version store cleanup is gated instance-wide by the + oldest active transaction touching row-versioned data + (RCSI or snapshot isolation), in ANY database. A single + such transaction freezes version store cleanup for the + whole instance, which is what inflates version_store_gb + above. These are the transaction(s) holding it open, + oldest first. + */ + version_store_blockers = + ( + SELECT + asdt.session_id, + database_context = + DB_NAME(ses.database_id), + ses.login_name, + ses.host_name, + ses.program_name, + session_status = + ses.status, + open_transaction_count = + ses.open_transaction_count, + isolation_level = + CASE + WHEN ses.transaction_isolation_level = 0 + THEN N'Unspecified' + WHEN ses.transaction_isolation_level = 1 + THEN N'Read Uncommitted' + WHEN ses.transaction_isolation_level = 2 + THEN N'Read Committed' + WHEN ses.transaction_isolation_level = 3 + THEN N'Repeatable Read' + WHEN ses.transaction_isolation_level = 4 + THEN N'Serializable' + WHEN ses.transaction_isolation_level = 5 + THEN N'Snapshot' + ELSE N'Unknown' + END, + is_snapshot_transaction = + asdt.is_snapshot, + transaction_age = + CONCAT + ( + asdt.elapsed_time_seconds / 86400, N'd ', + (asdt.elapsed_time_seconds % 86400) / 3600, N'h ', + (asdt.elapsed_time_seconds % 3600) / 60, N'm ', + asdt.elapsed_time_seconds % 60, N's' + ), + elapsed_seconds = + asdt.elapsed_time_seconds, + seconds_since_last_request = + DATEDIFF + ( + SECOND, + ses.last_request_end_time, + SYSDATETIME() + ), + transaction_begin_time = + dtat.transaction_begin_time, + max_version_chain_traversed = + asdt.max_version_chain_traversed, + transaction_sequence_num = + asdt.transaction_sequence_num, + transaction_name = + dtat.name, + last_query = + dest.text + FROM sys.dm_tran_active_snapshot_database_transactions AS asdt + JOIN sys.dm_exec_sessions AS ses + ON ses.session_id = asdt.session_id + LEFT JOIN sys.dm_tran_active_transactions AS dtat + ON dtat.transaction_id = asdt.transaction_id + LEFT JOIN sys.dm_exec_connections AS dconn + ON dconn.session_id = asdt.session_id + OUTER APPLY sys.dm_exec_sql_text(dconn.most_recent_sql_handle) AS dest + ORDER BY + asdt.elapsed_time_seconds DESC + FOR XML + PATH('version_store_blockers'), + TYPE ) FOR XML PATH('tempdb'), @@ -33606,8 +34191,14 @@ ALTER PROCEDURE @end_date datetimeoffset(7) = NULL, /*the end date of your search, will be converted to UTC internally*/ @include_plan_ids nvarchar(4000) = NULL, /*a list of plan ids to search for*/ @include_query_ids nvarchar(4000) = NULL, /*a list of query ids to search for*/ + @include_query_hashes nvarchar(4000) = NULL, /*a list of query hashes to search for*/ + @include_plan_hashes nvarchar(4000) = NULL, /*a list of query plan hashes to search for*/ + @include_sql_handles nvarchar(4000) = NULL, /*a list of sql handles to search for*/ @ignore_plan_ids nvarchar(4000) = NULL, /*a list of plan ids to ignore*/ @ignore_query_ids nvarchar(4000) = NULL, /*a list of query ids to ignore*/ + @ignore_query_hashes nvarchar(4000) = NULL, /*a list of query hashes to ignore*/ + @ignore_plan_hashes nvarchar(4000) = NULL, /*a list of query plan hashes to ignore*/ + @ignore_sql_handles nvarchar(4000) = NULL, /*a list of sql handles to ignore*/ @procedure_schema sysname = NULL, /*the schema of the procedure you're searching for*/ @procedure_name sysname = NULL, /*the name of the programmable object you're searching for*/ @query_text_search nvarchar(4000) = NULL, /*query text to search for*/ @@ -33632,7 +34223,7 @@ BEGIN TRY /*Version*/ SELECT @version = '1.6', - @version_date = '20260501'; + @version_date = '20260701'; /*Help*/ IF @help = 1 @@ -33660,8 +34251,14 @@ BEGIN WHEN N'@end_date' THEN 'the end date of your search, will be converted to UTC internally' WHEN N'@include_plan_ids' THEN 'a list of plan ids to search for' WHEN N'@include_query_ids' THEN 'a list of query ids to search for' + WHEN N'@include_query_hashes' THEN 'a list of query hashes to search for' + WHEN N'@include_plan_hashes' THEN 'a list of query plan hashes to search for' + WHEN N'@include_sql_handles' THEN 'a list of sql handles to search for' WHEN N'@ignore_plan_ids' THEN 'a list of plan ids to ignore' WHEN N'@ignore_query_ids' THEN 'a list of query ids to ignore' + WHEN N'@ignore_query_hashes' THEN 'a list of query hashes to ignore' + WHEN N'@ignore_plan_hashes' THEN 'a list of query plan hashes to ignore' + WHEN N'@ignore_sql_handles' THEN 'a list of sql handles to ignore' WHEN N'@procedure_schema' THEN 'the schema of the procedure you''re searching for' WHEN N'@procedure_name' THEN 'the name of the programmable object you''re searching for' WHEN N'@query_text_search' THEN 'query text to search for' @@ -33681,8 +34278,14 @@ BEGIN WHEN N'@end_date' THEN 'January 1, 1753, through December 31, 9999' WHEN N'@include_plan_ids' THEN 'a string; comma separated for multiple ids' WHEN N'@include_query_ids' THEN 'a string; comma separated for multiple ids' + WHEN N'@include_query_hashes' THEN 'a string; comma separated for multiple hashes' + WHEN N'@include_plan_hashes' THEN 'a string; comma separated for multiple hashes' + WHEN N'@include_sql_handles' THEN 'a string; comma separated for multiple handles' WHEN N'@ignore_plan_ids' THEN 'a string; comma separated for multiple ids' WHEN N'@ignore_query_ids' THEN 'a string; comma separated for multiple ids' + WHEN N'@ignore_query_hashes' THEN 'a string; comma separated for multiple hashes' + WHEN N'@ignore_plan_hashes' THEN 'a string; comma separated for multiple hashes' + WHEN N'@ignore_sql_handles' THEN 'a string; comma separated for multiple handles' WHEN N'@procedure_schema' THEN 'a valid schema in your database' WHEN N'@procedure_name' THEN 'a valid programmable object in your database' WHEN N'@query_text_search' THEN 'a string; leading and trailing wildcards will be added if missing' @@ -33702,8 +34305,14 @@ BEGIN WHEN N'@end_date' THEN 'NULL' WHEN N'@include_plan_ids' THEN 'NULL' WHEN N'@include_query_ids' THEN 'NULL' + WHEN N'@include_query_hashes' THEN 'NULL' + WHEN N'@include_plan_hashes' THEN 'NULL' + WHEN N'@include_sql_handles' THEN 'NULL' WHEN N'@ignore_plan_ids' THEN 'NULL' WHEN N'@ignore_query_ids' THEN 'NULL' + WHEN N'@ignore_query_hashes' THEN 'NULL' + WHEN N'@ignore_plan_hashes' THEN 'NULL' + WHEN N'@ignore_sql_handles' THEN 'NULL' WHEN N'@procedure_schema' THEN 'NULL; dbo if NULL and procedure name is not NULL' WHEN N'@procedure_name' THEN 'NULL' WHEN N'@query_text_search' THEN 'NULL' @@ -33751,7 +34360,9 @@ DECLARE @sql_2017 bit = 0, @sql_2022_views bit = 0, @new bit = 0, - @current_table nvarchar(100) + @current_table nvarchar(100), + @include_plans_seeded bit = 0, + @match nvarchar(max) = N'' /*Fix NULL @database_name*/ IF @@ -34072,10 +34683,22 @@ SELECT NULLIF(@include_plan_ids, ''), @include_query_ids = NULLIF(@include_query_ids, ''), + @include_query_hashes = + NULLIF(@include_query_hashes, ''), + @include_plan_hashes = + NULLIF(@include_plan_hashes, ''), + @include_sql_handles = + NULLIF(@include_sql_handles, ''), @ignore_plan_ids = NULLIF(@ignore_plan_ids, ''), @ignore_query_ids = NULLIF(@ignore_query_ids, ''), + @ignore_query_hashes = + NULLIF(@ignore_query_hashes, ''), + @ignore_plan_hashes = + NULLIF(@ignore_plan_hashes, ''), + @ignore_sql_handles = + NULLIF(@ignore_sql_handles, ''), @query_text_search = NULLIF(@query_text_search, ''), @query_text_search_not = @@ -34300,6 +34923,48 @@ CREATE TABLE INDEX query_id CLUSTERED (query_id) ); +CREATE TABLE + #include_query_hashes +( + query_hash binary(8) NOT NULL, + INDEX query_hash CLUSTERED (query_hash) +); + +CREATE TABLE + #include_plan_hashes +( + plan_hash binary(8) NOT NULL, + INDEX plan_hash CLUSTERED (plan_hash) +); + +CREATE TABLE + #include_sql_handles +( + sql_handle varbinary(64) NOT NULL, + INDEX sql_handle CLUSTERED (sql_handle) +); + +CREATE TABLE + #ignore_query_hashes +( + query_hash binary(8) NOT NULL, + INDEX query_hash CLUSTERED (query_hash) +); + +CREATE TABLE + #ignore_plan_hashes +( + plan_hash binary(8) NOT NULL, + INDEX plan_hash CLUSTERED (plan_hash) +); + +CREATE TABLE + #ignore_sql_handles +( + sql_handle varbinary(64) NOT NULL, + INDEX sql_handle CLUSTERED (sql_handle) +); + CREATE TABLE #query_text_search ( @@ -34915,6 +35580,21 @@ Populate filter temp tables using XML-based string splitting for compatibility IF @query_plan_xml IS NULL BEGIN +/* +Resolve the include filters into #include_plan_ids. + +Multiple include filters combine as an INTERSECTION: OR within a single filter's +comma-separated list, but AND across different filters. The first filter that is +supplied seeds #include_plan_ids with every plan id it matches; each later filter +then DELETEs any plan id it does not also match. This keeps results narrowed to +exactly what the caller asked for, instead of widening back out to every plan +(which is what unioning the filters would do). + +@include_plan_ids is handled first, so when it is supplied it always seeds the +set with the literal plan ids requested. @match holds the per-filter predicate +(correlated to a sys.query_store_plan row aliased qsp) used for both seeding and +intersecting. +*/ IF @include_plan_ids IS NOT NULL BEGIN SELECT @@ -34972,6 +35652,9 @@ BEGIN @sql, N'@include_plan_ids nvarchar(4000)', @include_plan_ids; + + SELECT + @include_plans_seeded = 1; END; IF @include_query_ids IS NOT NULL @@ -35032,12 +35715,176 @@ BEGIN N'@include_query_ids nvarchar(4000)', @include_query_ids; - /*Convert query IDs to plan IDs for filtering*/ + /* + Resolve these query ids into #include_plan_ids (seed or intersect). + */ + SELECT + @match = N'EXISTS + ( + SELECT + 1/0 + FROM #include_query_ids AS iqi + WHERE iqi.query_id = qsp.query_id + )'; + + IF @include_plans_seeded = 0 + BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #include_plan_ids + WITH + (TABLOCK) + ( + plan_id + ) + SELECT DISTINCT + qsp.plan_id + FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp + WHERE ' + @match + N' + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; + + SELECT + @include_plans_seeded = 1; + END; + ELSE + BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + DELETE + ipi + FROM #include_plan_ids AS ipi + WHERE NOT EXISTS + ( + SELECT + 1/0 + FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp + WHERE qsp.plan_id = ipi.plan_id + AND ' + @match + N' + ) + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; + END; +END; + +/* +@include_query_hashes: match plans whose query has one of these query hashes. +*/ +IF @include_query_hashes IS NOT NULL +BEGIN SELECT @sql = @isolation_level; SELECT @sql += N' + INSERT + #include_query_hashes + WITH + (TABLOCK) + ( + query_hash + ) + SELECT + query_hash = + CONVERT(binary(8), ids.query_hash_s, 1) + FROM + ( + SELECT + query_hash_s = + NULLIF + ( + LTRIM + ( + RTRIM + ( + x.x.value + ( + ''(./text())[1]'', + ''varchar(131)'' + ) + ) + ), + '''' + ) + FROM + ( + SELECT + ids = + CONVERT + ( + xml, + '''' + + REPLACE + ( + @include_query_hashes, + '','', + '''' + ) + + '''' + ) + ) AS ids + CROSS APPLY ids.ids.nodes(''x'') AS x (x) + ) AS ids + WHERE ids.query_hash_s IS NOT NULL + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql, + N'@include_query_hashes nvarchar(4000)', + @include_query_hashes; + + SELECT + @match = N'EXISTS + ( + SELECT + 1/0 + FROM ' + @database_name_quoted + N'.sys.query_store_query AS qsq + WHERE qsq.query_id = qsp.query_id + AND EXISTS + ( + SELECT + 1/0 + FROM #include_query_hashes AS iqh + WHERE iqh.query_hash = qsq.query_hash + ) + )'; + + IF @include_plans_seeded = 0 + BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' INSERT #include_plan_ids WITH @@ -35048,15 +35895,113 @@ BEGIN SELECT DISTINCT qsp.plan_id FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp - WHERE EXISTS + WHERE ' + @match + N' + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; + + SELECT + @include_plans_seeded = 1; + END; + ELSE + BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + DELETE + ipi + FROM #include_plan_ids AS ipi + WHERE NOT EXISTS ( SELECT 1/0 - FROM #include_query_ids AS iqi - WHERE iqi.query_id = qsp.query_id + FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp + WHERE qsp.plan_id = ipi.plan_id + AND ' + @match + N' ) OPTION(RECOMPILE);' + @nc10; + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; + END; +END; + +/* +@include_plan_hashes: match plans with one of these query plan hashes. +*/ +IF @include_plan_hashes IS NOT NULL +BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #include_plan_hashes + WITH + (TABLOCK) + ( + plan_hash + ) + SELECT + plan_hash = + CONVERT(binary(8), ids.plan_hash_s, 1) + FROM + ( + SELECT + plan_hash_s = + NULLIF + ( + LTRIM + ( + RTRIM + ( + x.x.value + ( + ''(./text())[1]'', + ''varchar(131)'' + ) + ) + ), + '''' + ) + FROM + ( + SELECT + ids = + CONVERT + ( + xml, + '''' + + REPLACE + ( + @include_plan_hashes, + '','', + '''' + ) + + '''' + ) + ) AS ids + CROSS APPLY ids.ids.nodes(''x'') AS x (x) + ) AS ids + WHERE ids.plan_hash_s IS NOT NULL + OPTION(RECOMPILE);' + @nc10; + IF @debug = 1 BEGIN PRINT LEN(@sql); @@ -35064,7 +36009,238 @@ BEGIN END; EXECUTE sys.sp_executesql - @sql; + @sql, + N'@include_plan_hashes nvarchar(4000)', + @include_plan_hashes; + + SELECT + @match = N'EXISTS + ( + SELECT + 1/0 + FROM #include_plan_hashes AS iph + WHERE iph.plan_hash = qsp.query_plan_hash + )'; + + IF @include_plans_seeded = 0 + BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #include_plan_ids + WITH + (TABLOCK) + ( + plan_id + ) + SELECT DISTINCT + qsp.plan_id + FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp + WHERE ' + @match + N' + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; + + SELECT + @include_plans_seeded = 1; + END; + ELSE + BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + DELETE + ipi + FROM #include_plan_ids AS ipi + WHERE NOT EXISTS + ( + SELECT + 1/0 + FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp + WHERE qsp.plan_id = ipi.plan_id + AND ' + @match + N' + ) + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; + END; +END; + +/* +@include_sql_handles: match plans whose query text has one of these sql handles. +*/ +IF @include_sql_handles IS NOT NULL +BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #include_sql_handles + WITH + (TABLOCK) + ( + sql_handle + ) + SELECT + sql_handle = + CONVERT(varbinary(64), ids.sql_handle_s, 1) + FROM + ( + SELECT + sql_handle_s = + NULLIF + ( + LTRIM + ( + RTRIM + ( + x.x.value + ( + ''(./text())[1]'', + ''varchar(131)'' + ) + ) + ), + '''' + ) + FROM + ( + SELECT + ids = + CONVERT + ( + xml, + '''' + + REPLACE + ( + @include_sql_handles, + '','', + '''' + ) + + '''' + ) + ) AS ids + CROSS APPLY ids.ids.nodes(''x'') AS x (x) + ) AS ids + WHERE ids.sql_handle_s IS NOT NULL + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql, + N'@include_sql_handles nvarchar(4000)', + @include_sql_handles; + + SELECT + @match = N'EXISTS + ( + SELECT + 1/0 + FROM ' + @database_name_quoted + N'.sys.query_store_query AS qsq + WHERE qsq.query_id = qsp.query_id + AND EXISTS + ( + SELECT + 1/0 + FROM ' + @database_name_quoted + N'.sys.query_store_query_text AS qsqt + WHERE qsqt.query_text_id = qsq.query_text_id + AND EXISTS + ( + SELECT + 1/0 + FROM #include_sql_handles AS ish + WHERE ish.sql_handle = qsqt.statement_sql_handle + ) + ) + )'; + + IF @include_plans_seeded = 0 + BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #include_plan_ids + WITH + (TABLOCK) + ( + plan_id + ) + SELECT DISTINCT + qsp.plan_id + FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp + WHERE ' + @match + N' + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; + + SELECT + @include_plans_seeded = 1; + END; + ELSE + BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + DELETE + ipi + FROM #include_plan_ids AS ipi + WHERE NOT EXISTS + ( + SELECT + 1/0 + FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp + WHERE qsp.plan_id = ipi.plan_id + AND ' + @match + N' + ) + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; + END; END; IF @ignore_plan_ids IS NOT NULL @@ -35219,6 +36395,345 @@ BEGIN @sql; END; +/* +Ignore filters union into #ignore_plan_ids. Unlike the include filters, exclusion +is correct as a union: "ignore plan Z AND all plans of query X AND all plans with +hash H" excludes plan Z, X's plans, and H's plans, which the single NOT EXISTS +against #ignore_plan_ids handles directly. +*/ +IF @ignore_query_hashes IS NOT NULL +BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #ignore_query_hashes + WITH + (TABLOCK) + ( + query_hash + ) + SELECT + query_hash = + CONVERT(binary(8), ids.query_hash_s, 1) + FROM + ( + SELECT + query_hash_s = + NULLIF + ( + LTRIM + ( + RTRIM + ( + x.x.value + ( + ''(./text())[1]'', + ''varchar(131)'' + ) + ) + ), + '''' + ) + FROM + ( + SELECT + ids = + CONVERT + ( + xml, + '''' + + REPLACE + ( + @ignore_query_hashes, + '','', + '''' + ) + + '''' + ) + ) AS ids + CROSS APPLY ids.ids.nodes(''x'') AS x (x) + ) AS ids + WHERE ids.query_hash_s IS NOT NULL + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql, + N'@ignore_query_hashes nvarchar(4000)', + @ignore_query_hashes; + + /*Convert query hashes to plan IDs for filtering*/ + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #ignore_plan_ids + WITH + (TABLOCK) + ( + plan_id + ) + SELECT DISTINCT + qsp.plan_id + FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp + WHERE EXISTS + ( + SELECT + 1/0 + FROM ' + @database_name_quoted + N'.sys.query_store_query AS qsq + WHERE qsq.query_id = qsp.query_id + AND EXISTS + ( + SELECT + 1/0 + FROM #ignore_query_hashes AS iqh + WHERE iqh.query_hash = qsq.query_hash + ) + ) + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; +END; + +IF @ignore_plan_hashes IS NOT NULL +BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #ignore_plan_hashes + WITH + (TABLOCK) + ( + plan_hash + ) + SELECT + plan_hash = + CONVERT(binary(8), ids.plan_hash_s, 1) + FROM + ( + SELECT + plan_hash_s = + NULLIF + ( + LTRIM + ( + RTRIM + ( + x.x.value + ( + ''(./text())[1]'', + ''varchar(131)'' + ) + ) + ), + '''' + ) + FROM + ( + SELECT + ids = + CONVERT + ( + xml, + '''' + + REPLACE + ( + @ignore_plan_hashes, + '','', + '''' + ) + + '''' + ) + ) AS ids + CROSS APPLY ids.ids.nodes(''x'') AS x (x) + ) AS ids + WHERE ids.plan_hash_s IS NOT NULL + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql, + N'@ignore_plan_hashes nvarchar(4000)', + @ignore_plan_hashes; + + /*Convert plan hashes to plan IDs for filtering*/ + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #ignore_plan_ids + WITH + (TABLOCK) + ( + plan_id + ) + SELECT DISTINCT + qsp.plan_id + FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp + WHERE EXISTS + ( + SELECT + 1/0 + FROM #ignore_plan_hashes AS iph + WHERE iph.plan_hash = qsp.query_plan_hash + ) + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; +END; + +IF @ignore_sql_handles IS NOT NULL +BEGIN + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #ignore_sql_handles + WITH + (TABLOCK) + ( + sql_handle + ) + SELECT + sql_handle = + CONVERT(varbinary(64), ids.sql_handle_s, 1) + FROM + ( + SELECT + sql_handle_s = + NULLIF + ( + LTRIM + ( + RTRIM + ( + x.x.value + ( + ''(./text())[1]'', + ''varchar(131)'' + ) + ) + ), + '''' + ) + FROM + ( + SELECT + ids = + CONVERT + ( + xml, + '''' + + REPLACE + ( + @ignore_sql_handles, + '','', + '''' + ) + + '''' + ) + ) AS ids + CROSS APPLY ids.ids.nodes(''x'') AS x (x) + ) AS ids + WHERE ids.sql_handle_s IS NOT NULL + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql, + N'@ignore_sql_handles nvarchar(4000)', + @ignore_sql_handles; + + /*Convert sql handles to plan IDs for filtering*/ + SELECT + @sql = @isolation_level; + + SELECT + @sql += N' + INSERT + #ignore_plan_ids + WITH + (TABLOCK) + ( + plan_id + ) + SELECT DISTINCT + qsp.plan_id + FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp + WHERE EXISTS + ( + SELECT + 1/0 + FROM ' + @database_name_quoted + N'.sys.query_store_query AS qsq + WHERE qsq.query_id = qsp.query_id + AND EXISTS + ( + SELECT + 1/0 + FROM ' + @database_name_quoted + N'.sys.query_store_query_text AS qsqt + WHERE qsqt.query_text_id = qsq.query_text_id + AND EXISTS + ( + SELECT + 1/0 + FROM #ignore_sql_handles AS ish + WHERE ish.sql_handle = qsqt.statement_sql_handle + ) + ) + ) + OPTION(RECOMPILE);' + @nc10; + + IF @debug = 1 + BEGIN + PRINT LEN(@sql); + PRINT @sql; + END; + + EXECUTE sys.sp_executesql + @sql; +END; + /*Process @query_text_search parameter*/ IF @query_text_search IS NOT NULL BEGIN @@ -35804,6 +37319,9 @@ IF ( @include_plan_ids IS NOT NULL OR @include_query_ids IS NOT NULL + OR @include_query_hashes IS NOT NULL + OR @include_plan_hashes IS NOT NULL + OR @include_sql_handles IS NOT NULL ) BEGIN SELECT @@ -35822,6 +37340,9 @@ IF ( @ignore_plan_ids IS NOT NULL OR @ignore_query_ids IS NOT NULL + OR @ignore_query_hashes IS NOT NULL + OR @ignore_plan_hashes IS NOT NULL + OR @ignore_sql_handles IS NOT NULL ) BEGIN SELECT @@ -36073,6 +37594,9 @@ IF ( @include_plan_ids IS NOT NULL OR @include_query_ids IS NOT NULL + OR @include_query_hashes IS NOT NULL + OR @include_plan_hashes IS NOT NULL + OR @include_sql_handles IS NOT NULL ) BEGIN SELECT @@ -36091,6 +37615,9 @@ IF ( @ignore_plan_ids IS NOT NULL OR @ignore_query_ids IS NOT NULL + OR @ignore_query_hashes IS NOT NULL + OR @ignore_plan_hashes IS NOT NULL + OR @ignore_sql_handles IS NOT NULL ) BEGIN SELECT @@ -37810,6 +39337,8 @@ BEGIN END; END; /*Final end*/ +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; SET ANSI_WARNINGS ON; SET ARITHABORT ON; SET CONCAT_NULL_YIELDS_NULL ON; @@ -37889,7 +39418,7 @@ BEGIN SELECT @version = '1.6', - @version_date = '20260501'; + @version_date = '20260701'; /* ╔══════════════════════════════════════════════════╗ @@ -38194,7 +39723,21 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1 ) + N');', query_text = - st.text, + ( + SELECT + [processing-instruction(query)] = + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + st.text COLLATE Latin1_General_BIN2, + NCHAR(31),N'?'),NCHAR(30),N'?'),NCHAR(29),N'?'),NCHAR(28),N'?'),NCHAR(27),N'?'),NCHAR(26),N'?'),NCHAR(25),N'?'),NCHAR(24),N'?'),NCHAR(23),N'?'),NCHAR(22),N'?'), + NCHAR(21),N'?'),NCHAR(20),N'?'),NCHAR(19),N'?'),NCHAR(18),N'?'),NCHAR(17),N'?'),NCHAR(16),N'?'),NCHAR(15),N'?'),NCHAR(14),N'?'),NCHAR(12),N'?'), + NCHAR(11),N'?'),NCHAR(8),N'?'),NCHAR(7),N'?'),NCHAR(6),N'?'),NCHAR(5),N'?'),NCHAR(4),N'?'),NCHAR(3),N'?'),NCHAR(2),N'?'),NCHAR(1),N'?'),NCHAR(0),N'') + FOR + XML + PATH(N''), + TYPE + ), query_plan = CASE WHEN TRY_CAST(qp.query_plan AS xml) IS NOT NULL @@ -38264,60 +39807,102 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ IF @find_duplicate_plans = 1 BEGIN - SELECT TOP (@top) - database_name = - DB_NAME(CONVERT(integer, MAX(pa.value))), - qs.query_hash, - plan_count = - FORMAT(COUNT_BIG(DISTINCT qs.plan_handle), N'N0'), - total_executions = - FORMAT(SUM(qs.execution_count), N'N0'), - total_cpu_ms = - FORMAT(SUM(qs.total_worker_time) / 1000.0, N'N3'), - total_duration_ms = - FORMAT(SUM(qs.total_elapsed_time) / 1000.0, N'N3'), - total_logical_reads = - FORMAT(SUM(qs.total_logical_reads), N'N0'), - total_logical_writes = - FORMAT(SUM(qs.total_logical_writes), N'N0'), - total_physical_reads = - FORMAT(SUM(qs.total_physical_reads), N'N0'), - total_rows = - FORMAT(SUM(qs.total_rows), N'N0'), - min_rows = - FORMAT(MIN(qs.min_rows), N'N0'), - max_rows = - FORMAT(MAX(qs.max_rows), N'N0'), - total_cached_size_mb = - FORMAT(SUM(cp.size_in_bytes) / 1048576.0, N'N2'), - oldest_plan = - MIN(qs.creation_time), - newest_plan = - MAX(qs.creation_time), + SELECT + d.database_name, + d.query_hash, + d.plan_count, + d.total_executions, + d.total_cpu_ms, + d.total_duration_ms, + d.total_logical_reads, + d.total_logical_writes, + d.total_physical_reads, + d.total_rows, + d.min_rows, + d.max_rows, + d.total_cached_size_mb, + d.oldest_plan, + d.newest_plan, sample_query_text = - MAX(st.text) - FROM sys.dm_exec_query_stats AS qs - JOIN sys.dm_exec_cached_plans AS cp - ON cp.plan_handle = qs.plan_handle - CROSS APPLY + ( + SELECT + [processing-instruction(query)] = + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + d.sample_query_text COLLATE Latin1_General_BIN2, + NCHAR(31),N'?'),NCHAR(30),N'?'),NCHAR(29),N'?'),NCHAR(28),N'?'),NCHAR(27),N'?'),NCHAR(26),N'?'),NCHAR(25),N'?'),NCHAR(24),N'?'),NCHAR(23),N'?'),NCHAR(22),N'?'), + NCHAR(21),N'?'),NCHAR(20),N'?'),NCHAR(19),N'?'),NCHAR(18),N'?'),NCHAR(17),N'?'),NCHAR(16),N'?'),NCHAR(15),N'?'),NCHAR(14),N'?'),NCHAR(12),N'?'), + NCHAR(11),N'?'),NCHAR(8),N'?'),NCHAR(7),N'?'),NCHAR(6),N'?'),NCHAR(5),N'?'),NCHAR(4),N'?'),NCHAR(3),N'?'),NCHAR(2),N'?'),NCHAR(1),N'?'),NCHAR(0),N'') + FOR + XML + PATH(N''), + TYPE + ) + FROM ( - SELECT TOP (1) - value = pa.value - FROM sys.dm_exec_plan_attributes(qs.plan_handle) AS pa - WHERE pa.attribute = N'dbid' - ) AS pa - CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st - WHERE qs.query_hash <> 0x0000000000000000 - AND (@ignore_system_databases = 0 OR ISNULL(CONVERT(integer, pa.value), 0) NOT IN (1, 2, 3, 4)) - AND ISNULL(CONVERT(integer, pa.value), 0) < 32761 - AND (@database_id IS NULL OR CONVERT(integer, pa.value) = @database_id) - GROUP BY - qs.query_hash - HAVING - COUNT_BIG(DISTINCT qs.plan_handle) > 1 + SELECT TOP (@top) + database_name = + DB_NAME(CONVERT(integer, MAX(pa.value))), + qs.query_hash, + plan_count = + FORMAT(COUNT_BIG(DISTINCT qs.plan_handle), N'N0'), + total_executions = + FORMAT(SUM(qs.execution_count), N'N0'), + total_cpu_ms = + FORMAT(SUM(qs.total_worker_time) / 1000.0, N'N3'), + total_duration_ms = + FORMAT(SUM(qs.total_elapsed_time) / 1000.0, N'N3'), + total_logical_reads = + FORMAT(SUM(qs.total_logical_reads), N'N0'), + total_logical_writes = + FORMAT(SUM(qs.total_logical_writes), N'N0'), + total_physical_reads = + FORMAT(SUM(qs.total_physical_reads), N'N0'), + total_rows = + FORMAT(SUM(qs.total_rows), N'N0'), + min_rows = + FORMAT(MIN(qs.min_rows), N'N0'), + max_rows = + FORMAT(MAX(qs.max_rows), N'N0'), + total_cached_size_mb = + FORMAT(SUM(cp.size_in_bytes) / 1048576.0, N'N2'), + oldest_plan = + MIN(qs.creation_time), + newest_plan = + MAX(qs.creation_time), + sample_query_text = + MAX(st.text), + sort_plan_count = + COUNT_BIG(DISTINCT qs.plan_handle), + sort_cpu = + SUM(qs.total_worker_time) + FROM sys.dm_exec_query_stats AS qs + JOIN sys.dm_exec_cached_plans AS cp + ON cp.plan_handle = qs.plan_handle + CROSS APPLY + ( + SELECT TOP (1) + value = pa.value + FROM sys.dm_exec_plan_attributes(qs.plan_handle) AS pa + WHERE pa.attribute = N'dbid' + ) AS pa + CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st + WHERE qs.query_hash <> 0x0000000000000000 + AND (@ignore_system_databases = 0 OR ISNULL(CONVERT(integer, pa.value), 0) NOT IN (1, 2, 3, 4)) + AND ISNULL(CONVERT(integer, pa.value), 0) < 32761 + AND (@database_id IS NULL OR CONVERT(integer, pa.value) = @database_id) + GROUP BY + qs.query_hash + HAVING + COUNT_BIG(DISTINCT qs.plan_handle) > 1 + ORDER BY + COUNT_BIG(DISTINCT qs.plan_handle) DESC, + SUM(qs.total_worker_time) DESC + ) AS d ORDER BY - COUNT_BIG(DISTINCT qs.plan_handle) DESC, - SUM(qs.total_worker_time) DESC + d.sort_plan_count DESC, + d.sort_cpu DESC OPTION(RECOMPILE, MAXDOP 1); RETURN; @@ -38346,9 +39931,20 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Plan creation time distribution: What % of plans were created in the last 24h / 4h / 1h? High percentages suggest plan cache instability or memory pressure. + + @total_cached_plans is the true compiled-plan count from + sys.dm_exec_cached_plans — the number the user actually sees in the + cache — so the finding reconciles with what they observe. The age + buckets are computed at plan grain (DISTINCT plan_handle) from + sys.dm_exec_query_stats, the only source with per-plan compile + times. Counting raw query_stats rows (statement grain) inflated + every number, because a multi-statement plan contributes one row + per statement; @total_plans is now distinct plans with execution + stats, the consistent denominator for the recency percentages. */ DECLARE @total_plans bigint = 0, + @total_cached_plans bigint = 0, @plans_24h bigint = 0, @plans_4h bigint = 0, @plans_1h bigint = 0, @@ -38358,32 +39954,49 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @oldest_plan_date datetime = NULL; SELECT - @total_plans = COUNT_BIG(*), + @total_cached_plans = COUNT_BIG(*) + FROM sys.dm_exec_cached_plans AS cp + CROSS APPLY + ( + SELECT TOP (1) + value = pa.value + FROM sys.dm_exec_plan_attributes(cp.plan_handle) AS pa + WHERE pa.attribute = N'dbid' + ) AS pa + WHERE cp.cacheobjtype = N'Compiled Plan' + AND pa.value IS NOT NULL + AND (@ignore_system_databases = 0 OR CONVERT(integer, pa.value) NOT IN (1, 2, 3, 4)) + AND CONVERT(integer, pa.value) < 32761 + AND (@database_id IS NULL OR CONVERT(integer, pa.value) = @database_id) + OPTION(RECOMPILE); + + SELECT + @total_plans = COUNT_BIG(DISTINCT qs.plan_handle), @plans_24h = - SUM + COUNT_BIG ( + DISTINCT CASE WHEN DATEDIFF(HOUR, qs.creation_time, GETDATE()) <= 24 - THEN 1 - ELSE 0 + THEN qs.plan_handle END ), @plans_4h = - SUM + COUNT_BIG ( + DISTINCT CASE WHEN DATEDIFF(HOUR, qs.creation_time, GETDATE()) <= 4 - THEN 1 - ELSE 0 + THEN qs.plan_handle END ), @plans_1h = - SUM + COUNT_BIG ( + DISTINCT CASE WHEN DATEDIFF(HOUR, qs.creation_time, GETDATE()) <= 1 - THEN 1 - ELSE 0 + THEN qs.plan_handle END ), @oldest_plan_date = MIN(qs.creation_time) @@ -38395,7 +40008,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. FROM sys.dm_exec_plan_attributes(qs.plan_handle) AS pa WHERE pa.attribute = N'dbid' ) AS pa - WHERE 1 = 1 + WHERE (@ignore_system_databases = 0 OR ISNULL(CONVERT(integer, pa.value), 0) NOT IN (1, 2, 3, 4)) + AND ISNULL(CONVERT(integer, pa.value), 0) < 32761 AND (@database_id IS NULL OR CONVERT(integer, pa.value) = @database_id) OPTION(RECOMPILE); @@ -38422,9 +40036,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. N'Plan Cache Instability', N'Most plans are recent', 1, - N'Of ' + FORMAT(@total_plans, N'N0') + - N' cached plans, ' + - CONVERT(nvarchar(10), @pct_24h) + N'% created in the last 24 hours, ' + + N'Of ' + FORMAT(@total_cached_plans, N'N0') + + N' cached compiled plans, ' + FORMAT(@total_plans, N'N0') + + N' have execution stats; of those, ' + + CONVERT(nvarchar(10), @pct_24h) + N'% were compiled in the last 24 hours, ' + CONVERT(nvarchar(10), @pct_4h) + N'% in the last 4 hours, ' + CONVERT(nvarchar(10), @pct_1h) + N'% in the last 1 hour. ' + N'This may indicate memory pressure, frequent recompiles, or plan cache flushes. ' + @@ -38446,9 +40061,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. N'Plan Cache Stability', N'Plan age distribution looks healthy', 254, - N'Of ' + FORMAT(@total_plans, N'N0') + - N' cached plans, ' + - CONVERT(nvarchar(10), @pct_24h) + N'% created in the last 24 hours, ' + + N'Of ' + FORMAT(@total_cached_plans, N'N0') + + N' cached compiled plans, ' + FORMAT(@total_plans, N'N0') + + N' have execution stats; of those, ' + + CONVERT(nvarchar(10), @pct_24h) + N'% were compiled in the last 24 hours, ' + CONVERT(nvarchar(10), @pct_4h) + N'% in the last 4 hours, ' + CONVERT(nvarchar(10), @pct_1h) + N'% in the last 1 hour. ' + N'Oldest cached plan: ' + CONVERT(nvarchar(30), @oldest_plan_date, 120) + N'.' @@ -38457,9 +40073,22 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. /* Single-use plan bloat per database: - High % of execution_count = 1 plans suggests ad hoc workload - that may benefit from Forced Parameterization. - Only surfaces databases where single-use plans exceed 10%. + A high percentage of single-use adhoc/prepared plans suggests an + unparameterized ad hoc workload that bloats the plan cache and may + benefit from Optimize for Ad Hoc Workloads and/or Forced + Parameterization. Only surfaces databases where single-use plans + exceed 10% of that database's cached compiled plans. + + Sourced from sys.dm_exec_cached_plans on purpose, NOT + sys.dm_exec_query_stats. query_stats is statement-grained and only + contains plans that have executed and are still cached, so its + per-database counts are a small, misleading slice of the cache + (e.g. "27 of 27 plans" on a server whose cache actually holds + 100k+ plans). dm_exec_cached_plans IS the plan cache the user sees, + and usecounts = 1 on an Adhoc/Prepared compiled plan is the + canonical single-use plan signal. total_count is every cached + compiled plan for the database (the denominator the user counts), + so the percentage reconciles with what they observe. */ INSERT #plan_cache_health @@ -38473,29 +40102,29 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SELECT TOP (5) finding_group = CASE - WHEN single_use_pct > 75 + WHEN x.single_use_pct > 75 THEN N'Single-Use Plan Bloat' ELSE N'Single-Use Plans' END, finding = CASE - WHEN single_use_pct > 75 + WHEN x.single_use_pct > 75 THEN N'Excessive single-use plans in cache' ELSE N'Notable single-use plan percentage' END, x.database_name, priority = CASE - WHEN single_use_pct > 75 + WHEN x.single_use_pct > 75 THEN 1 ELSE 254 END, details = FORMAT(x.single_use_count, N'N0') + N' of ' + FORMAT(x.total_count, N'N0') + - N' plans (' + + N' cached plans (' + CONVERT(nvarchar(10), x.single_use_pct) + - N'%) executed only once. Consider Forced Parameterization if these are unparameterized ad hoc queries.' + N'%) are single-use adhoc or prepared plans. Consider Optimize for Ad Hoc Workloads, and Forced Parameterization if these are unparameterized ad hoc queries.' FROM ( SELECT @@ -38507,7 +40136,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SUM ( CASE - WHEN qs.execution_count = 1 + WHEN cp.usecounts = 1 + AND cp.objtype IN (N'Adhoc', N'Prepared') THEN 1 ELSE 0 END @@ -38519,21 +40149,23 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SUM ( CASE - WHEN qs.execution_count = 1 + WHEN cp.usecounts = 1 + AND cp.objtype IN (N'Adhoc', N'Prepared') THEN 1 ELSE 0 END ) * 100.0 / COUNT_BIG(*) ) - FROM sys.dm_exec_query_stats AS qs + FROM sys.dm_exec_cached_plans AS cp CROSS APPLY ( SELECT TOP (1) value = pa.value - FROM sys.dm_exec_plan_attributes(qs.plan_handle) AS pa + FROM sys.dm_exec_plan_attributes(cp.plan_handle) AS pa WHERE pa.attribute = N'dbid' ) AS pa - WHERE pa.value IS NOT NULL + WHERE cp.cacheobjtype = N'Compiled Plan' + AND pa.value IS NOT NULL AND (@ignore_system_databases = 0 OR CONVERT(integer, pa.value) NOT IN (1, 2, 3, 4)) AND CONVERT(integer, pa.value) < 32761 AND (@database_id IS NULL OR CONVERT(integer, pa.value) = @database_id) @@ -38561,7 +40193,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. FROM ( SELECT - plan_count = COUNT_BIG(*) + plan_count = COUNT_BIG(DISTINCT qs.plan_handle) FROM sys.dm_exec_query_stats AS qs CROSS APPLY ( @@ -38571,11 +40203,13 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. WHERE pa.attribute = N'dbid' ) AS pa WHERE qs.query_hash <> 0x0000000000000000 + AND (@ignore_system_databases = 0 OR ISNULL(CONVERT(integer, pa.value), 0) NOT IN (1, 2, 3, 4)) + AND ISNULL(CONVERT(integer, pa.value), 0) < 32761 AND (@database_id IS NULL OR CONVERT(integer, pa.value) = @database_id) GROUP BY qs.query_hash HAVING - COUNT_BIG(*) > 5 + COUNT_BIG(DISTINCT qs.plan_handle) > 5 ) AS x OPTION(RECOMPILE); @@ -38620,7 +40254,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. details = FORMAT(COUNT_BIG(DISTINCT qs2.query_hash), N'N0') + N' query hashes with 5+ plans, totaling ' + - FORMAT(COUNT_BIG(*), N'N0') + N' plans. ' + + FORMAT(COUNT_BIG(DISTINCT qs2.plan_handle), N'N0') + N' plans. ' + N'Most likely unparameterized queries. SET option differences between sessions can also cause this. Consider Forced Parameterization.' FROM ( @@ -38631,7 +40265,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. GROUP BY qs.query_hash HAVING - COUNT_BIG(*) > 5 + COUNT_BIG(DISTINCT qs.plan_handle) > 5 ) AS x JOIN sys.dm_exec_query_stats AS qs2 ON qs2.query_hash = x.query_hash @@ -38649,7 +40283,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. GROUP BY pa.value ORDER BY - COUNT_BIG(*) DESC + COUNT_BIG(DISTINCT qs2.plan_handle) DESC OPTION(RECOMPILE); END; @@ -38722,8 +40356,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. BEGIN SELECT @debug_msg = - N'Plan cache health: ' + CONVERT(nvarchar(20), @total_plans) + - N' total plans, ' + CONVERT(nvarchar(10), @pct_24h) + + N'Plan cache health: ' + CONVERT(nvarchar(20), @total_cached_plans) + + N' cached compiled plans, ' + CONVERT(nvarchar(20), @total_plans) + + N' with stats, ' + CONVERT(nvarchar(10), @pct_24h) + N'% < 24h, ' + CONVERT(nvarchar(10), @pct_4h) + N'% < 4h, ' + CONVERT(nvarchar(10), @pct_1h) + N'% < 1h'; RAISERROR(N'%s', 0, 1, @debug_msg) WITH NOWAIT; @@ -40142,22 +41777,36 @@ OPTION(RECOMPILE, MAXDOP 1);'; s.query_type, s.object_name, query_text = - CASE - WHEN s.query_type = 'Statement' - THEN SUBSTRING - ( - st.text, - (s.sample_statement_start / 2) + 1, - ( - CASE s.sample_statement_end - WHEN -1 - THEN DATALENGTH(st.text) - ELSE s.sample_statement_end - END - s.sample_statement_start - ) / 2 + 1 - ) - ELSE st.text - END, + ( + SELECT + [processing-instruction(query)] = + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + CASE + WHEN s.query_type = 'Statement' + THEN SUBSTRING + ( + st.text, + (s.sample_statement_start / 2) + 1, + ( + CASE s.sample_statement_end + WHEN -1 + THEN DATALENGTH(st.text) + ELSE s.sample_statement_end + END - s.sample_statement_start + ) / 2 + 1 + ) + ELSE st.text + END COLLATE Latin1_General_BIN2, + NCHAR(31),N'?'),NCHAR(30),N'?'),NCHAR(29),N'?'),NCHAR(28),N'?'),NCHAR(27),N'?'),NCHAR(26),N'?'),NCHAR(25),N'?'),NCHAR(24),N'?'),NCHAR(23),N'?'),NCHAR(22),N'?'), + NCHAR(21),N'?'),NCHAR(20),N'?'),NCHAR(19),N'?'),NCHAR(18),N'?'),NCHAR(17),N'?'),NCHAR(16),N'?'),NCHAR(15),N'?'),NCHAR(14),N'?'),NCHAR(12),N'?'), + NCHAR(11),N'?'),NCHAR(8),N'?'),NCHAR(7),N'?'),NCHAR(6),N'?'),NCHAR(5),N'?'),NCHAR(4),N'?'),NCHAR(3),N'?'),NCHAR(2),N'?'),NCHAR(1),N'?'),NCHAR(0),N'') + FOR + XML + PATH(N''), + TYPE + ), query_plan = CASE WHEN TRY_CAST(qp.query_plan AS xml) IS NOT NULL @@ -40331,8 +41980,8 @@ ALTER PROCEDURE @hide_help_table bit = 0, /*hides the "bottom table" that shows help and support information*/ @format_output bit = 1, /*returns numbers formatted with commas and most decimals rounded away*/ @get_all_databases bit = 0, /*looks for query store enabled user databases and returns combined results from all of them*/ - @include_databases nvarchar(MAX) = NULL, /*comma-separated list of databases to include (only when @get_all_databases = 1)*/ - @exclude_databases nvarchar(MAX) = NULL, /*comma-separated list of databases to exclude (only when @get_all_databases = 1)*/ + @include_databases nvarchar(max) = NULL, /*comma-separated list of databases to include (only when @get_all_databases = 1)*/ + @exclude_databases nvarchar(max) = NULL, /*comma-separated list of databases to exclude (only when @get_all_databases = 1)*/ @workdays bit = 0, /*Use this to filter out weekends and after-hours queries*/ @work_start time(0) = '9am', /*Use this to set a specific start of your work days*/ @work_end time(0) = '5pm', /*Use this to set a specific end of your work days*/ @@ -40368,8 +42017,8 @@ BEGIN TRY These are for your outputs. */ SELECT - @version = '6.6', - @version_date = '20260501'; + @version = '6.7', + @version_date = '20260701'; /* Helpful section! For help. diff --git a/sp_HealthParser/sp_HealthParser.sql b/sp_HealthParser/sp_HealthParser.sql index 3a158d56..1b38a666 100644 --- a/sp_HealthParser/sp_HealthParser.sql +++ b/sp_HealthParser/sp_HealthParser.sql @@ -73,7 +73,7 @@ BEGIN SELECT @version = '3.6', - @version_date = '20260501'; + @version_date = '20260701'; IF @help = 1 BEGIN diff --git a/sp_HumanEvents/sp_HumanEvents.sql b/sp_HumanEvents/sp_HumanEvents.sql index 4f6cb1b0..aacb60cb 100644 --- a/sp_HumanEvents/sp_HumanEvents.sql +++ b/sp_HumanEvents/sp_HumanEvents.sql @@ -89,7 +89,7 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SELECT @version = '7.7', - @version_date = '20260601'; + @version_date = '20260701'; IF @help = 1 BEGIN diff --git a/sp_HumanEvents/sp_HumanEventsBlockViewer.sql b/sp_HumanEvents/sp_HumanEventsBlockViewer.sql index 341299b2..ce7315bc 100644 --- a/sp_HumanEvents/sp_HumanEventsBlockViewer.sql +++ b/sp_HumanEvents/sp_HumanEventsBlockViewer.sql @@ -95,7 +95,7 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SELECT @version = '5.6', - @version_date = '20260501'; + @version_date = '20260701'; IF @help = 1 BEGIN diff --git a/sp_IndexCleanup/sp_IndexCleanup.sql b/sp_IndexCleanup/sp_IndexCleanup.sql index 52367b0d..97087c74 100644 --- a/sp_IndexCleanup/sp_IndexCleanup.sql +++ b/sp_IndexCleanup/sp_IndexCleanup.sql @@ -1,3 +1,5 @@ +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; SET ANSI_WARNINGS ON; SET ARITHABORT ON; SET CONCAT_NULL_YIELDS_NULL ON; @@ -74,7 +76,7 @@ SET NOCOUNT ON; BEGIN TRY SELECT @version = '2.7', - @version_date = '20260601'; + @version_date = '20260701'; IF /* Check SQL Server 2012+ for FORMAT and CONCAT functions */ diff --git a/sp_LogHunter/sp_LogHunter.sql b/sp_LogHunter/sp_LogHunter.sql index 0deadf89..035b76a2 100644 --- a/sp_LogHunter/sp_LogHunter.sql +++ b/sp_LogHunter/sp_LogHunter.sql @@ -74,7 +74,7 @@ SET DATEFORMAT MDY; BEGIN SELECT @version = '3.6', - @version_date = '20260501'; + @version_date = '20260701'; IF @help = 1 BEGIN diff --git a/sp_PerfCheck/sp_PerfCheck.sql b/sp_PerfCheck/sp_PerfCheck.sql index cb1247b4..1f9246af 100644 --- a/sp_PerfCheck/sp_PerfCheck.sql +++ b/sp_PerfCheck/sp_PerfCheck.sql @@ -1,3 +1,5 @@ +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; SET ANSI_WARNINGS ON; SET ARITHABORT ON; SET CONCAT_NULL_YIELDS_NULL ON; @@ -65,7 +67,7 @@ BEGIN */ SELECT @version = N'2.6', - @version_date = N'20260501'; + @version_date = N'20260701'; /* Help section, for help. diff --git a/sp_PressureDetector/sp_PressureDetector.sql b/sp_PressureDetector/sp_PressureDetector.sql index 8f97d7bf..8db07fad 100644 --- a/sp_PressureDetector/sp_PressureDetector.sql +++ b/sp_PressureDetector/sp_PressureDetector.sql @@ -79,7 +79,7 @@ SET LANGUAGE us_english; SELECT @version = '6.6', - @version_date = '20260501'; + @version_date = '20260701'; IF @help = 1 diff --git a/sp_QueryReproBuilder/sp_QueryReproBuilder.sql b/sp_QueryReproBuilder/sp_QueryReproBuilder.sql index a1dfca41..d00fab2b 100644 --- a/sp_QueryReproBuilder/sp_QueryReproBuilder.sql +++ b/sp_QueryReproBuilder/sp_QueryReproBuilder.sql @@ -91,7 +91,7 @@ BEGIN TRY /*Version*/ SELECT @version = '1.6', - @version_date = '20260501'; + @version_date = '20260701'; /*Help*/ IF @help = 1 diff --git a/sp_QuickieCache/sp_QuickieCache.sql b/sp_QuickieCache/sp_QuickieCache.sql index 37c6e414..0f932df8 100644 --- a/sp_QuickieCache/sp_QuickieCache.sql +++ b/sp_QuickieCache/sp_QuickieCache.sql @@ -1,3 +1,5 @@ +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; SET ANSI_WARNINGS ON; SET ARITHABORT ON; SET CONCAT_NULL_YIELDS_NULL ON; @@ -77,7 +79,7 @@ BEGIN SELECT @version = '1.6', - @version_date = '20260501'; + @version_date = '20260701'; /* ╔══════════════════════════════════════════════════╗ diff --git a/sp_QuickieStore/sp_QuickieStore.sql b/sp_QuickieStore/sp_QuickieStore.sql index ccfb9715..7fb16fb5 100644 --- a/sp_QuickieStore/sp_QuickieStore.sql +++ b/sp_QuickieStore/sp_QuickieStore.sql @@ -128,7 +128,7 @@ These are for your outputs. */ SELECT @version = '6.7', - @version_date = '20260601'; + @version_date = '20260701'; /* Helpful section! For help.