Rewrite Query Store grid query using sp_QuickieStore patterns (#143)#145
Merged
erikdarlingdata merged 1 commit intodevfrom Mar 25, 2026
Merged
Rewrite Query Store grid query using sp_QuickieStore patterns (#143)#145erikdarlingdata merged 1 commit intodevfrom
erikdarlingdata merged 1 commit intodevfrom
Conversation
Multi-phase materialization approach for drastically better performance on large Query Store datasets: Phase 1: Pre-filter matching interval IDs into #intervals (clustered PK). All subsequent phases use EXISTS semi-join against this tiny table instead of re-evaluating the time predicate. Phase 2: Aggregate runtime_stats by plan_id into #plan_stats (clustered PK on plan_id). Uses EXISTS against #intervals — semi-join avoids materializing the full interval join. Phase 3: Rank best plan per query_id, materialize TOP N into #top_plans. Still no nvarchar(max) columns touched. Phase 4: Hydrate only the TOP N winners with text, plan XML, and metadata. Uses TRY_CONVERT for safe plan XML handling. Additional improvements: - Both time filter paths now use rsi.start_time (indexed) instead of rs.last_execution_time (requires scanning all runtime_stats rows) - OPTION(RECOMPILE) on aggregation phases prevents parameter sniffing on date range parameters producing stale plans - Clustered PKs on temp tables enable index seeks in subsequent joins Tested on SQL2022: ~22ms total for all 4 phases (24 intervals, 546 plans). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This was referenced Mar 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to PR #144 — the initial two-phase approach wasn't enough. This rewrites the query using patterns from sp_QuickieStore for significantly better performance on large Query Store datasets.
Four-phase materialization approach:
#intervals#plan_stats#top_plansAdditional improvements:
rsi.start_time(indexed) — thehoursBackfallback previously filtered onrs.last_execution_timewhich required scanning all runtime_statsPerformance
Tested on SQL2022 with PerformanceMonitor Query Store data: ~22ms total for all 4 phases (24 intervals, 546 plan aggregations, 5 winners hydrated).
Test plan
🤖 Generated with Claude Code