Cached Out
There are lots of examples of how to do this with the plan cache, but, well, the plan cache can be an awfully unstable place.
Query store being a bit more historically reliable, we can use some of the same tricks to track them down there too.
SELECT
x.total_query_plans,
qsq.query_hash,
qsp.query_plan_hash,
query_plan =
TRY_CONVERT
(
xml,
qsp.query_plan
)
FROM
(
SELECT
qsq.query_hash,
distinct_query_plans =
COUNT_BIG(DISTINCT qsp.query_plan_hash),
total_query_plans =
COUNT_BIG(qsp.query_plan_hash)
FROM sys.query_store_query AS qsq
JOIN sys.query_store_plan AS qsp
ON qsq.query_id = qsp.query_id
GROUP BY
qsq.query_hash
HAVING COUNT_BIG(DISTINCT qsp.query_plan_hash) > 1
AND COUNT_BIG(DISTINCT qsp.query_plan_hash)
<= COUNT_BIG(qsp.query_plan_hash)
) AS x
CROSS APPLY
(
SELECT TOP (x.total_query_plans)
qsq.*
FROM sys.query_store_query AS qsq
WHERE x.query_hash = qsq.query_hash
) AS qsq
CROSS APPLY
(
SELECT
qsp.*
FROM sys.query_store_plan AS qsp
WHERE qsp.query_id = qsq.query_id
) AS qsp
ORDER BY
x.total_query_plans DESC;
Thanks for reading!
Going Further
If this is the kind of SQL Server stuff you love learning about, you’ll love my training. Blog readers get 25% off the Everything Bundle — over 100 hours of performance tuning content. Need hands-on help? I offer consulting engagements from targeted investigations to ongoing retainers. Want a quick sanity check before committing to a full engagement? Schedule a call — no commitment required.
Related Posts
- How To Find Poorly Performing SQL Server Queries To Tune Using Query Store
- SQL Server Community Tools: The Wrap Up And Combined Link
- SQL Server Community Tools: How To Dig Deeper With Expert Mode With sp_QuickieStore
- SQL Server Community Tools: Formatting sp_QuickieStore Output So It’s Easier To Understand