Skip to content

Commit f7bdea9

Browse files
Remove unnecessary query_store_query_text joins and fix whitespace in grouped fetches
The Step 3 ranked CTEs in both FetchGroupedByQueryHashAsync and FetchGroupedByModuleAsync joined sys.query_store_query_text without using any of its columns — the actual text fetch happens in the Final select. Also fixes tab/space inconsistency in the doc comment blocks for both methods. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 59ecc7f commit f7bdea9

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

src/PlanViewer.Core/Services/QueryStoreService.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -943,15 +943,15 @@ private static (string PlanStatsCol, string AggAlias) ResolveGroupMetric(string
943943
};
944944
}
945945

946-
/// <summary>
947-
/// Fetches grouped-by-QueryHash results.
948-
/// Step 1: Top X query hashes by metric.
949-
/// Step 2: Top 5 plan hashes per query hash with metrics.
950-
/// Step 3: Top and bottom QueryId/PlanId per query_hash/plan_hash.
951-
/// Final : Fetch Query Text and Plan XML for the identified QueryId/PlanId.
952-
/// Returns intermediate (plan_hash level) and leaf (query_id/plan_id level) rows.
953-
/// </summary>
954-
public static async Task<QueryStoreGroupedResult> FetchGroupedByQueryHashAsync(
946+
/// <summary>
947+
/// Fetches grouped-by-QueryHash results.
948+
/// Step 1: Top X query hashes by metric.
949+
/// Step 2: Top 5 plan hashes per query hash with metrics.
950+
/// Step 3: Top and bottom QueryId/PlanId per query_hash/plan_hash.
951+
/// Final : Fetch Query Text and Plan XML for the identified QueryId/PlanId.
952+
/// Returns intermediate (plan_hash level) and leaf (query_id/plan_id level) rows.
953+
/// </summary>
954+
public static async Task<QueryStoreGroupedResult> FetchGroupedByQueryHashAsync(
955955
string connectionString, int topN = 25, string orderBy = "cpu",
956956
QueryStoreFilter? filter = null, CancellationToken ct = default,
957957
DateTime? startUtc = null, DateTime? endUtc = null)
@@ -1116,7 +1116,6 @@ THEN OBJECT_SCHEMA_NAME(q.object_id) + N'.' + OBJECT_NAME(q.object_id)
11161116
FROM #plan_stats ps
11171117
JOIN sys.query_store_plan p ON ps.plan_id = p.plan_id
11181118
JOIN sys.query_store_query q ON p.query_id = q.query_id
1119-
JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
11201119
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))
@@ -1208,15 +1207,15 @@ FROM ranked
12081207
return result;
12091208
}
12101209

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(
1210+
/// <summary>
1211+
/// Fetches grouped-by-Module results.
1212+
/// Step 1: Top X modules by metric.
1213+
/// Step 2: Top 5 query hashes per module with metrics.
1214+
/// Step 3: Top and bottom QueryId/PlanId per module/query_hash.
1215+
/// Final Step: Fetch Query Text and Plan XML for the identified QueryId/PlanId.
1216+
/// Returns intermediate (query_hash level) and leaf (query_id/plan_id level) rows.
1217+
/// </summary>
1218+
public static async Task<QueryStoreGroupedResult> FetchGroupedByModuleAsync(
12201219
string connectionString, int topN = 25, string orderBy = "cpu",
12211220
QueryStoreFilter? filter = null, CancellationToken ct = default,
12221221
DateTime? startUtc = null, DateTime? endUtc = null)
@@ -1372,7 +1371,7 @@ THEN OBJECT_SCHEMA_NAME(q.object_id) + N'.' + OBJECT_NAME(q.object_id)
13721371
CONVERT(varchar(18), p.query_plan_hash, 1) AS plan_hash,
13731372
q.query_id,
13741373
ps.plan_id,
1375-
qt.query_text_id,
1374+
q.query_text_id,
13761375
CAST(ps.total_cpu_us AS bigint) AS total_cpu_us,
13771376
CAST(ps.total_duration_us AS bigint) AS total_duration_us,
13781377
CAST(ps.total_reads AS bigint) AS total_reads,
@@ -1396,7 +1395,6 @@ THEN OBJECT_SCHEMA_NAME(q.object_id) + N'.' + OBJECT_NAME(q.object_id)
13961395
FROM #plan_stats ps
13971396
JOIN sys.query_store_plan p ON ps.plan_id = p.plan_id
13981397
JOIN sys.query_store_query q ON p.query_id = q.query_id
1399-
JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
14001398
WHERE EXISTS (SELECT 1 FROM #qhash_rows qhr
14011399
WHERE qhr.module_name = CASE WHEN q.object_id <> 0
14021400
THEN OBJECT_SCHEMA_NAME(q.object_id) + N'.' + OBJECT_NAME(q.object_id)

0 commit comments

Comments
 (0)