Skip to content

Commit 0a9a6ec

Browse files
authored
Merge pull request #3346 from BrentOzarULTD/3345_sp_BlitzCache_sort_by_duplicates
3345 sp blitz cache sort by duplicates
2 parents 2509965 + 1b6d668 commit 0a9a6ec

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

sp_BlitzCache.sql

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ IF @Help = 1
354354
UNION ALL
355355
SELECT N'@SortOrder',
356356
N'VARCHAR(10)',
357-
N'Data processing and display order. @SortOrder will still be used, even when preparing output for a table or for excel. Possible values are: "CPU", "Reads", "Writes", "Duration", "Executions", "Recent Compilations", "Memory Grant", "Unused Grant", "Spills", "Query Hash". Additionally, the word "Average" or "Avg" can be used to sort on averages rather than total. "Executions per minute" and "Executions / minute" can be used to sort by execution per minute. For the truly lazy, "xpm" can also be used. Note that when you use all or all avg, the only parameters you can use are @Top and @DatabaseName. All others will be ignored.'
357+
N'Data processing and display order. @SortOrder will still be used, even when preparing output for a table or for excel. Possible values are: "CPU", "Reads", "Writes", "Duration", "Executions", "Recent Compilations", "Memory Grant", "Unused Grant", "Spills", "Query Hash", "Duplicates". Additionally, the word "Average" or "Avg" can be used to sort on averages rather than total. "Executions per minute" and "Executions / minute" can be used to sort by execution per minute. For the truly lazy, "xpm" can also be used. Note that when you use all or all avg, the only parameters you can use are @Top and @DatabaseName. All others will be ignored.'
358358

359359
UNION ALL
360360
SELECT N'@UseTriggersAnyway',
@@ -810,6 +810,35 @@ IF @SortOrder LIKE 'query hash%'
810810
END
811811

812812

813+
/* If they want to sort by duplicates, populate the @OnlySqlHandles list for them */
814+
IF @SortOrder LIKE 'duplicate%'
815+
BEGIN
816+
RAISERROR('Beginning duplicate query hash sort', 0, 1) WITH NOWAIT;
817+
818+
SELECT TOP(@Top) qs.query_hash,
819+
MAX(qs.sql_handle) AS max_sql_handle,
820+
COUNT_BIG(*) AS records
821+
INTO #duplicate_grouped
822+
FROM sys.dm_exec_query_stats AS qs
823+
CROSS APPLY ( SELECT pa.value
824+
FROM sys.dm_exec_plan_attributes(qs.plan_handle) AS pa
825+
WHERE pa.attribute = 'dbid' ) AS ca
826+
GROUP BY qs.query_hash, ca.value
827+
HAVING COUNT_BIG(*) > 100
828+
ORDER BY records DESC;
829+
830+
SELECT TOP (1)
831+
@OnlySqlHandles = STUFF((SELECT DISTINCT N',' + CONVERT(NVARCHAR(MAX), qhg.max_sql_handle, 1)
832+
FROM #duplicate_grouped AS qhg
833+
WHERE qhg.max_sql_handle <> 0x00
834+
FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'')
835+
OPTION(RECOMPILE);
836+
837+
SET @SortOrder = 'cpu';
838+
839+
END
840+
841+
813842
/* Set @Top based on sort */
814843
IF (
815844
@Top IS NULL

0 commit comments

Comments
 (0)