@@ -4471,7 +4471,64 @@ OPTION(RECOMPILE);' + @nc10;
44714471 INTO #hi_scored
44724472 FROM #hi_query_stats AS qs;
44734473
4474- /* Step 4: Time bucketing (dynamic SQL for Query Store DMVs)*/
4474+ /* Step 3b: Stage query_ids for interesting hashes (reused by time bucketing and identifiers)*/
4475+ SELECT
4476+ @current_table = ' inserting #hi_id_staging_queries' ,
4477+ @sql = @isolation_level;
4478+
4479+ IF @troubleshoot_performance = 1
4480+ BEGIN
4481+ EXECUTE sys .sp_executesql
4482+ @troubleshoot_insert,
4483+ N ' @current_table nvarchar(100)' ,
4484+ @current_table;
4485+
4486+ SET STATISTICS XML ON ;
4487+ END ;
4488+
4489+ SELECT
4490+ @sql + = N'
4491+ SELECT DISTINCT
4492+ qsq.query_hash,
4493+ qsq.query_id
4494+ FROM ' + @database_name_quoted + N' .sys.query_store_query AS qsq
4495+ JOIN #hi_interesting AS i
4496+ ON qsq.query_hash = i.query_hash
4497+ OPTION(RECOMPILE);' + @nc10;
4498+
4499+ IF @debug = 1
4500+ BEGIN
4501+ PRINT LEN (@sql);
4502+ PRINT @sql;
4503+ END ;
4504+
4505+ INSERT
4506+ #hi_id_staging_queries WITH (TABLOCK )
4507+ (
4508+ query_hash,
4509+ query_id
4510+ )
4511+ EXECUTE sys .sp_executesql
4512+ @sql;
4513+
4514+ IF @troubleshoot_performance = 1
4515+ BEGIN
4516+ SET STATISTICS XML OFF ;
4517+
4518+ EXECUTE sys .sp_executesql
4519+ @troubleshoot_update,
4520+ N ' @current_table nvarchar(100)' ,
4521+ @current_table;
4522+
4523+ EXECUTE sys .sp_executesql
4524+ @troubleshoot_info,
4525+ N' @sql nvarchar(max),
4526+ @current_table nvarchar(100)' ,
4527+ @sql,
4528+ @current_table;
4529+ END ;
4530+
4531+ /* Step 4: Time bucketing (starts from staged query_ids, skips query_store_query)*/
44754532 DECLARE
44764533 @hi_utc_to_local smallint = - @utc_minutes_difference;
44774534
@@ -4492,7 +4549,7 @@ OPTION(RECOMPILE);' + @nc10;
44924549 SELECT
44934550 @sql + = N'
44944551SELECT
4495- qsq .query_hash,
4552+ sq .query_hash,
44964553 time_bucket =
44974554 CASE
44984555 WHEN DATEPART
@@ -4516,19 +4573,17 @@ SELECT
45164573 END,
45174574 executions =
45184575 SUM(qsrs.count_executions)
4519- FROM ' + @database_name_quoted + N ' .sys.query_store_query AS qsq
4576+ FROM #hi_id_staging_queries AS sq
45204577JOIN ' + @database_name_quoted + N' .sys.query_store_plan AS qsp
4521- ON qsq .query_id = qsp .query_id
4578+ ON qsp .query_id = sq .query_id
45224579JOIN ' + @database_name_quoted + N' .sys.query_store_runtime_stats AS qsrs
4523- ON qsp .plan_id = qsrs .plan_id
4580+ ON qsrs .plan_id = qsp .plan_id
45244581JOIN ' + @database_name_quoted + N' .sys.query_store_runtime_stats_interval AS qsrsi
4525- ON qsrs.runtime_stats_interval_id = qsrsi.runtime_stats_interval_id
4526- JOIN #hi_interesting AS i
4527- ON qsq.query_hash = i.query_hash
4582+ ON qsrsi.runtime_stats_interval_id = qsrs.runtime_stats_interval_id
45284583WHERE qsrsi.start_time >= @start_date
45294584AND qsrsi.start_time < @end_date
45304585GROUP BY
4531- qsq .query_hash,
4586+ sq .query_hash,
45324587 CASE
45334588 WHEN DATEPART
45344589 (
@@ -4765,65 +4820,7 @@ OPTION(RECOMPILE);' + @nc10;
47654820 ) AS ws;
47664821 END ; /* End wait stats*/
47674822
4768- /* Step 5b: Query identifiers (two-stage approach)*/
4769- /* Stage 1: Dynamic SQL gets distinct IDs*/
4770- SELECT
4771- @current_table = ' inserting #hi_id_staging' ,
4772- @sql = @isolation_level;
4773-
4774- IF @troubleshoot_performance = 1
4775- BEGIN
4776- EXECUTE sys .sp_executesql
4777- @troubleshoot_insert,
4778- N ' @current_table nvarchar(100)' ,
4779- @current_table;
4780-
4781- SET STATISTICS XML ON ;
4782- END ;
4783-
4784- SELECT
4785- @sql + = N'
4786- SELECT DISTINCT
4787- qsq.query_hash,
4788- qsq.query_id
4789- FROM ' + @database_name_quoted + N' .sys.query_store_query AS qsq
4790- JOIN #hi_interesting AS i
4791- ON qsq.query_hash = i.query_hash
4792- OPTION(RECOMPILE);' + @nc10;
4793-
4794- IF @debug = 1
4795- BEGIN
4796- PRINT LEN (@sql);
4797- PRINT @sql;
4798- END ;
4799-
4800- INSERT
4801- #hi_id_staging_queries WITH (TABLOCK )
4802- (
4803- query_hash,
4804- query_id
4805- )
4806- EXECUTE sys .sp_executesql
4807- @sql;
4808-
4809- IF @troubleshoot_performance = 1
4810- BEGIN
4811- SET STATISTICS XML OFF ;
4812-
4813- EXECUTE sys .sp_executesql
4814- @troubleshoot_update,
4815- N ' @current_table nvarchar(100)' ,
4816- @current_table;
4817-
4818- EXECUTE sys .sp_executesql
4819- @troubleshoot_info,
4820- N' @sql nvarchar(max),
4821- @current_table nvarchar(100)' ,
4822- @sql,
4823- @current_table;
4824- END ;
4825-
4826- /* Insert plan and object IDs separately*/
4823+ /* Step 5b: Query identifiers (plan and object IDs)*/
48274824 SELECT
48284825 @sql = @isolation_level;
48294826
0 commit comments