Query tuning in SQL Server reduces CPU, I/O, and latency by analyzing execution plans, indexing, updating statistics, and rewriting SQL.
High logical reads, missing indexes, outdated statistics, and non-SARGable predicates force the optimizer into expensive scans. Checking these areas pinpoints the bottleneck.
Run SET SHOWPLAN_XML ON
or click the "Include Actual Execution Plan" button in SSMS. Inspect operators with the highest cost percentage.
Key lookups, hash joins, and table scans indicate missing or fragmented indexes.High Estimated Subtree Cost
suggests the query deserves tuning.
Enable SET STATISTICS TIME, IO ON;
. The server prints CPU time and logical reads per statement, guiding you to the worst offenders.
After bulk loads or when sys.stats
shows modification_counter > 20%
. Fresh statistics let the optimizer choose better plans.
Covering indexes that include filtered columns remove extra lookups.Keep them narrow; extra columns bloat storage and slow writes.
Yes. Replacing SELECT *
with explicit column lists, converting OR
to UNION ALL
, and using window functions often slash logical reads.
Turn on Query Store: ALTER DATABASE MyDB SET QUERY_STORE = ON;
. It captures plans and runtime stats so regressions are easy to spot.
1) Inspect the plan first.2) Fix indexes or statistics before query hints. 3) Rewrite queries to be SARGable. 4) Lock in gains with Query Store forced plans.
.
No significant impact for OLTP workloads; data is stored asynchronously. Keep size capped with MAX_STORAGE_SIZE_MB
.
Rebuild when fragmentation > 30% or page density < 70%. Reorganize for 5–30% fragmentation to save resources.
Yes. In Query Store, find the stable plan ID and click "Force Plan" to lock it until you unforce it.