Created
May 13, 2022 16:01
-
-
Save Otterpohl/f02455ba1d5f330be75ebb57c969a195 to your computer and use it in GitHub Desktop.
Get queries which have many plans per query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH RedundantQueries | |
AS (SELECT TOP 10 | |
query_hash, | |
COUNT(query_hash) AS PlansCached, | |
COUNT(DISTINCT (query_hash)) AS DistinctPlansCached, | |
MIN(creation_time) AS FirstPlanCreationTime, | |
MAX(creation_time) AS LastPlanCreationTime, | |
SUM(total_worker_time) AS Total_CPU_ms, | |
SUM(total_elapsed_time) AS Total_Duration_ms, | |
SUM(total_logical_reads) AS Total_Reads, | |
SUM(total_logical_writes) AS Total_Writes, | |
--SUM(total_spills) AS Total_Spills, | |
N'EXEC sp_BlitzCache @OnlyQueryHashes=''0x' + CONVERT(NVARCHAR(50), query_hash, 2) + '''' AS MoreInfo | |
FROM sys.dm_exec_query_stats | |
GROUP BY query_hash | |
HAVING COUNT(query_hash) > 1 | |
ORDER BY COUNT(query_hash) DESC) | |
SELECT r.query_hash, | |
r.PlansCached, | |
r.DistinctPlansCached, | |
q.SampleQueryText, | |
q.SampleQueryPlan, | |
r.MoreInfo, | |
r.Total_CPU_ms, | |
r.Total_Duration_ms, | |
r.Total_Reads, | |
r.Total_Writes, --r.Total_Spills, | |
r.FirstPlanCreationTime, | |
r.LastPlanCreationTime | |
FROM RedundantQueries AS r | |
CROSS APPLY | |
( | |
SELECT TOP 10 | |
st.text AS SampleQueryText, | |
qp.query_plan AS SampleQueryPlan, | |
qs.total_elapsed_time | |
FROM sys.dm_exec_query_stats AS qs | |
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st | |
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp | |
WHERE r.query_hash = qs.query_hash | |
ORDER BY qs.total_elapsed_time DESC | |
) AS q | |
ORDER BY r.PlansCached DESC, | |
q.total_elapsed_time DESC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment