Have you ever experienced a situation where a SQL Server query runs perfectly one day but suddenly becomes much slower the next, even though nothing seems to have changed?
This is a common performance troubleshooting challenge. Looking at the current execution plan can help us understand how SQL Server is executing the query now, but how can we find out what changed and why the query was faster before?
This is where SQL Server Query Store becomes extremely useful. When enabled and collecting the relevant workload, Query Store keeps a history of queries, execution plans, and runtime statistics, allowing us to compare past and current performance and identify possible causes of a slowdown.
In this article, we’ll explore how to use Query Store to investigate query performance regressions, compare execution plans, and make better decisions when troubleshooting SQL Server performance issues.
Check that the history exists
Start by confirming Query Store’s state, capture settings and retention for the affected database. Do not assume that every query and every execution has been retained.
This read-only inspection runs in the database you want to investigate and requires appropriate permissions for the target version:
SELECT actual_state_desc,
desired_state_desc,
readonly_reason,
current_storage_size_mb,
max_storage_size_mb,
query_capture_mode_desc
FROM sys.database_query_store_options;
If the actual state differs from the intended state, investigate the reason before relying on newly collected data. Enabling collection today will not reconstruct missing historical observations.
Define two comparable windows
Choose a period when the operation behaved normally and a period when it was slow. Compare similar business conditions where possible: the same report scope, similar parameter values and a comparable workload.
A month-end run and a quiet afternoon may differ for reasons unrelated to a plan regression. Record deployments, index changes, data loads and maintenance around the same time.
Compare the query, its plans and its workload
Query Store keeps runtime statistics aggregated over time intervals. Use the query and plan identities to inspect execution counts, average duration, CPU and reads across the selected windows. Where available, wait categories can add context.
An average is not an individual execution trace. A higher average may reflect a different mixture of parameter values. When combining intervals, weight averages by execution counts instead of taking a simple average of averages.
Check whether a plan changed, but do not stop there. The same plan can become slower because it reads more data, waits longer or serves a different workload.
Form a specific hypothesis
Suppose the query moved to a different plan after a large data load. The useful hypothesis is not merely “the new plan is bad.” It is that the plan performs poorly for a representative workload and that a particular estimate or access choice helps explain the difference.
Reproduce the affected parameter sets in an approved environment. Compare the plans and results, and inspect estimates against actual row counts. This connects the historical symptom with a mechanism you can evaluate.
Treat plan forcing as a controlled intervention
Forcing a previously successful plan may be an appropriate mitigation when supported and validated. It is not a reason to skip investigating the underlying cause.
Test other important parameter sets, record the reason for forcing, assign an owner and define a review date. Monitor whether forcing succeeds and whether the workload changes. A plan that was useful last week may not remain useful indefinitely.
Preserve the investigation record
Save the time windows, query identifiers, relevant plans, measurements and the observed result of any intervention. That record makes the next regression easier to diagnose and prevents the same emergency decision becoming permanent by accident.
Query Store is most helpful when it supports a comparison with a clear question. Use the history to narrow the explanation, then validate the proposed repair against the workload that matters.
Technical references
- Microsoft Learn: Monitor performance using Query Store
- Microsoft Learn: How Query Store collects data
- Microsoft Learn: Query Store options view
Continue learning
For a broader look at the features introduced in SQL Server 2022, explore my course SQL Server 2022: What’s New – New and Enhanced Features on Udemy. Review the curriculum and preview to see whether it fits your next learning goal.
Read Also
- Query Store Enhancements in SQL Server 2022
- Mastering SQL Server Performance Tuning: Essential Tips and Techniques
- SQL Server 2022 Overview: A Hybrid Data Platform
Stay connected
Subscribe to the GnoelixiAI Hub newsletter on LinkedIn for practical insights on AI, data and technology.
Watch more on YouTube: SQLNetHub TV and my personal channel.
Originally published on SQLNetHub.
© SQLNetHub
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.