All The Pretty Little Caveats
I’ve said quite a bit about missing index request utility generally in SQL Server, even as recently as last week!
But then I got a user question about using Query Store to do something similar, so here goes.
If you need a pre-2019 way to do this with Query Store, Kendra Little has a blog post about that here.
WITH queries AS ( SELECT TOP (100) parent_object_name = ISNULL ( OBJECT_NAME(qsq.object_id), 'No Parent Object' ), qsqt.query_sql_text, query_plan = TRY_CAST(qsp.query_plan AS xml), qsrs.first_execution_time, qsrs.last_execution_time, qsrs.count_executions, qsrs.avg_duration, qsrs.avg_cpu_time, qsp.query_plan_hash, qsq.query_hash FROM sys.query_store_runtime_stats AS qsrs JOIN sys.query_store_plan AS qsp ON qsp.plan_id = qsrs.plan_id JOIN sys.query_store_query AS qsq ON qsq.query_id = qsp.query_id JOIN sys.query_store_query_text AS qsqt ON qsqt.query_text_id = qsq.query_text_id WHERE qsrs.last_execution_time >= DATEADD(DAY, -7, SYSDATETIME()) AND qsrs.avg_cpu_time >= (10 * 1000) AND qsq.is_internal_query = 0 AND qsp.is_online_index_plan = 0 ORDER BY qsrs.avg_cpu_time DESC ) SELECT qs.* FROM queries AS qs CROSS APPLY ( SELECT TOP (1) gqs.* FROM sys.dm_db_missing_index_group_stats_query AS gqs WHERE qs.query_hash = gqs.query_hash AND qs.query_plan_hash = gqs.query_plan_hash ORDER BY gqs.last_user_seek DESC, gqs.last_user_scan DESC ) AS gqs ORDER BY qs.avg_cpu_time DESC OPTION(RECOMPILE);
I don’t love this query, because I don’t love querying Query Store views. That’s why I wrote sp_QuickieStore to make it a whole lot easier.
But anyway, this will get you a similar bunch of information.
Thanks for reading!
Going Further
If this is the kind of SQL Server stuff you love learning about, you’ll love my training. I’m offering a 75% discount to my blog readers if you click from here. I’m also available for consulting if you just don’t have time for that and need to solve performance problems quickly.
One thought on “Finding Query Store Queries With Missing Index Requests In SQL Server 2019”
Comments are closed.