Skip to content

Fix #286: Query Store query_id filter excludes target before applying#287

Merged
erikdarlingdata merged 1 commit intodevfrom
fix/issue-286-query-id-filter
Apr 26, 2026
Merged

Fix #286: Query Store query_id filter excludes target before applying#287
erikdarlingdata merged 1 commit intodevfrom
fix/issue-286-query-id-filter

Conversation

@erikdarlingdata
Copy link
Copy Markdown
Owner

Closes #286.

Root cause

FetchTopPlansAsync applies user filters (--query-id, --plan-id, --query-hash, --query-plan-hash, --module) only in Phase 4, AFTER Phase 3 has already selected TOP N plans by CPU across the entire database. When the user's target query isn't in the top N by CPU for the window, it gets cut from #top_plans before the filter runs — and the result set comes back empty even though Query Store / SSMS / sp_QuickieStore can see the query fine.

Fix

Push the filter into Phase 3's CTE so TOP N is selected from the filtered universe. Conditionally JOIN sys.query_store_query only when the filter touches query_hash or object_id — the unfiltered code path is unchanged and pays no extra cost.

The hash-tree and module-tree fetch methods already applied filters pre-TOP-N correctly; they were not touched.

Test plan

  • Build clean
  • 75/75 tests passing
  • Repro on a server: pick a low-CPU query, verify --query-id finds it
  • Spot-check unfiltered top-25 path looks unchanged

🤖 Generated with Claude Code

FetchTopPlansAsync applied user filters (--query-id, --plan-id, --query-hash,
--query-plan-hash, --module) only in Phase 4, AFTER Phase 3 had already
selected TOP N plans by CPU across the entire database. If the user's target
query wasn't in the top N by CPU during the time window, it was cut from
#top_plans before the filter ever ran — even though Query Store / SSMS /
sp_QuickieStore could see it fine.

Move the filter into Phase 3's CTE so TOP N is selected from the filtered
universe. Conditionally JOIN sys.query_store_query when the filter touches
query_hash or object_id (otherwise the join is skipped to avoid penalty in
the unfiltered path). Phase 4 filter clause is now redundant and removed.

The hash-tree and module-tree methods (FetchByQueryHashTree, FetchByPlanHashTree)
already applied filters pre-TOP-N — they're unaffected.

Closes #286.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@erikdarlingdata erikdarlingdata merged commit 6615d72 into dev Apr 26, 2026
2 checks passed
@erikdarlingdata erikdarlingdata deleted the fix/issue-286-query-id-filter branch April 26, 2026 11:01
erikdarlingdata added a commit that referenced this pull request Apr 27, 2026
Highlights since v1.8.0:
- Minimap for plan navigation (#276)
- Colored links by accuracy ratio divergence (#289)
- Distinct parallelism subtype icons (#285, #288)
- Query Store filter ordering fix (#287)
- xunit v2 -> v3 migration (#278)
- SqlClient 6 -> 7, ScriptDom 170 -> 180 (#279)
- System.CommandLine GA (#280)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant