Skip to content

Commit 8b25fee

Browse files
committed
improve performance of FetchGroupedByModuleAsync using a late get heavy element
1 parent fcc8a3c commit 8b25fee

1 file changed

Lines changed: 34 additions & 14 deletions

File tree

src/PlanViewer.Core/Services/QueryStoreService.cs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,14 +1208,15 @@ FROM ranked
12081208
return result;
12091209
}
12101210

1211-
/// <summary>
1212-
/// Fetches grouped-by-Module results.
1213-
/// Step 1: Top X modules by metric.
1214-
/// Step 2: Top 5 query hashes per module with metrics.
1215-
/// Step 3: Top and bottom QueryId/PlanId per module/query_hash.
1216-
/// Returns intermediate (query_hash level) and leaf (query_id/plan_id level) rows.
1217-
/// </summary>
1218-
public static async Task<QueryStoreGroupedResult> FetchGroupedByModuleAsync(
1211+
/// <summary>
1212+
/// Fetches grouped-by-Module results.
1213+
/// Step 1: Top X modules by metric.
1214+
/// Step 2: Top 5 query hashes per module with metrics.
1215+
/// Step 3: Top and bottom QueryId/PlanId per module/query_hash.
1216+
/// Final Step: Fetch Query Text and Plan XML for the identified QueryId/PlanId.
1217+
/// Returns intermediate (query_hash level) and leaf (query_id/plan_id level) rows.
1218+
/// </summary>
1219+
public static async Task<QueryStoreGroupedResult> FetchGroupedByModuleAsync(
12191220
string connectionString, int topN = 25, string orderBy = "cpu",
12201221
QueryStoreFilter? filter = null, CancellationToken ct = default,
12211222
DateTime? startUtc = null, DateTime? endUtc = null)
@@ -1371,8 +1372,7 @@ THEN OBJECT_SCHEMA_NAME(q.object_id) + N'.' + OBJECT_NAME(q.object_id)
13711372
CONVERT(varchar(18), p.query_plan_hash, 1) AS plan_hash,
13721373
q.query_id,
13731374
ps.plan_id,
1374-
qt.query_sql_text,
1375-
TRY_CONVERT(nvarchar(max), p.query_plan) AS plan_xml,
1375+
qt.query_text_id,
13761376
CAST(ps.total_cpu_us AS bigint) AS total_cpu_us,
13771377
CAST(ps.total_duration_us AS bigint) AS total_duration_us,
13781378
CAST(ps.total_reads AS bigint) AS total_reads,
@@ -1403,13 +1403,33 @@ THEN OBJECT_SCHEMA_NAME(q.object_id) + N'.' + OBJECT_NAME(q.object_id)
14031403
ELSE N'' END
14041404
AND qhr.query_hash = CONVERT(varchar(18), q.query_hash, 1))
14051405
)
1406-
SELECT module_name, query_hash, plan_hash, query_id, plan_id, query_sql_text, plan_xml,
1407-
total_cpu_us, total_duration_us, total_reads, total_writes,
1408-
total_physical_reads, total_memory_pages, total_executions, last_execution_time,
1409-
CASE WHEN rn_top = 1 THEN 1 ELSE 0 END AS is_top
1406+
SELECT *
1407+
into #ranked_light
14101408
FROM ranked
14111409
WHERE rn_top = 1 OR rn_bottom = 1;
14121410
1411+
/* Final select: join heavy elements (query_text, plan_xml) only for the top/bottom representatives */
1412+
SELECT
1413+
r.module_name,
1414+
r.query_hash,
1415+
r.plan_hash,
1416+
r.query_id,
1417+
r.plan_id,
1418+
qt.query_sql_text,
1419+
TRY_CONVERT(nvarchar(max), p.query_plan) AS plan_xml,
1420+
r.total_cpu_us,
1421+
r.total_duration_us,
1422+
r.total_reads,
1423+
r.total_writes,
1424+
r.total_physical_reads,
1425+
r.total_memory_pages,
1426+
r.total_executions,
1427+
r.last_execution_time,
1428+
CASE WHEN r.rn_top = 1 THEN 1 ELSE 0 END AS is_top
1429+
FROM #ranked_light r
1430+
JOIN sys.query_store_query_text qt ON r.query_text_id = qt.query_text_id
1431+
JOIN sys.query_store_plan p ON r.plan_id = p.plan_id;
1432+
14131433
/* Return intermediate rows (result set 2) */
14141434
SELECT * FROM #qhash_rows ORDER BY module_name, total_executions DESC;
14151435
";

0 commit comments

Comments
 (0)