Skip to content

Commit f921886

Browse files
committed
improve performance of FetchGroupedByQueryHashAsync : get heavy elements (query text and plan xml) after computations and top/bottom reduction
1 parent c489d6a commit f921886

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

src/PlanViewer.Core/Services/QueryStoreService.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,7 @@ ELSE N'' END
10981098
CONVERT(varchar(18), p.query_plan_hash, 1) AS plan_hash,
10991099
q.query_id,
11001100
ps.plan_id,
1101-
qt.query_sql_text,
1102-
TRY_CONVERT(nvarchar(max), p.query_plan) AS plan_xml,
1101+
q.query_text_id,
11031102
CASE WHEN q.object_id <> 0
11041103
THEN OBJECT_SCHEMA_NAME(q.object_id) + N'.' + OBJECT_NAME(q.object_id)
11051104
ELSE N'' END AS module_name,
@@ -1121,13 +1120,33 @@ WHERE EXISTS (SELECT 1 FROM #plan_hash_rows phr
11211120
WHERE phr.query_hash = CONVERT(varchar(18), q.query_hash, 1)
11221121
AND phr.plan_hash = CONVERT(varchar(18), p.query_plan_hash, 1))
11231122
)
1124-
SELECT query_hash, plan_hash, query_id, plan_id, query_sql_text, plan_xml,
1125-
module_name, total_cpu_us, total_duration_us, total_reads, total_writes,
1126-
total_physical_reads, total_memory_pages, total_executions, last_execution_time,
1127-
CASE WHEN rn_top = 1 THEN 1 ELSE 0 END AS is_top
1123+
SELECT *
1124+
into #ranked_light
11281125
FROM ranked
11291126
WHERE rn_top = 1 OR rn_bottom = 1;
11301127
1128+
/* Final select: join heavy elements (query_text, plan_xml) only for the top/bottom representatives */
1129+
SELECT
1130+
r.query_hash,
1131+
r.plan_hash,
1132+
r.query_id,
1133+
r.plan_id,
1134+
qt.query_sql_text,
1135+
TRY_CONVERT(nvarchar(max), p.query_plan) AS plan_xml,
1136+
r.module_name,
1137+
r.total_cpu_us,
1138+
r.total_duration_us,
1139+
r.total_reads,
1140+
r.total_writes,
1141+
r.total_physical_reads,
1142+
r.total_memory_pages,
1143+
r.total_executions,
1144+
r.last_execution_time,
1145+
CASE WHEN r.rn_top = 1 THEN 1 ELSE 0 END AS is_top
1146+
FROM #ranked_light r
1147+
JOIN sys.query_store_query_text qt ON r.query_text_id = qt.query_text_id
1148+
JOIN sys.query_store_plan p ON r.plan_id = p.plan_id;
1149+
11311150
/* Return intermediate rows (result set 1) */
11321151
SELECT * FROM #plan_hash_rows ORDER BY query_hash, total_executions DESC;
11331152
";

0 commit comments

Comments
 (0)