How to Tune Queries in SQL Server

Galaxy Glossary

How do I tune a slow query in SQL Server?

Query tuning in SQL Server reduces CPU, I/O, and latency by analyzing execution plans, indexing, updating statistics, and rewriting SQL.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why is my SQL Server query slow?

High logical reads, missing indexes, outdated statistics, and non-SARGable predicates force the optimizer into expensive scans. Checking these areas pinpoints the bottleneck.

How do I see the execution plan?

Run SET SHOWPLAN_XML ON or click the "Include Actual Execution Plan" button in SSMS. Inspect operators with the highest cost percentage.

Which operators signal trouble?

Key lookups, hash joins, and table scans indicate missing or fragmented indexes.High Estimated Subtree Cost suggests the query deserves tuning.

How can I measure CPU and I/O?

Enable SET STATISTICS TIME, IO ON;. The server prints CPU time and logical reads per statement, guiding you to the worst offenders.

When should I update statistics?

After bulk loads or when sys.stats shows modification_counter > 20%. Fresh statistics let the optimizer choose better plans.

What index helps most queries?

Covering indexes that include filtered columns remove extra lookups.Keep them narrow; extra columns bloat storage and slow writes.

Is query rewrite worth it?

Yes. Replacing SELECT * with explicit column lists, converting OR to UNION ALL, and using window functions often slash logical reads.

How do I persist plan history?

Turn on Query Store: ALTER DATABASE MyDB SET QUERY_STORE = ON;. It captures plans and runtime stats so regressions are easy to spot.

Best practices recap

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.

.

Why How to Tune Queries in SQL Server is important

How to Tune Queries in SQL Server Example Usage


-- Original slow query
SELECT c.name, SUM(o.total_amount) AS lifetime_value
FROM Customers c
JOIN Orders o ON o.customer_id = c.id
WHERE o.order_date >= '2023-01-01'
GROUP BY c.name
ORDER BY lifetime_value DESC;

-- Tuned version
/* 1. Covering index */
CREATE INDEX IX_Orders_Cust_Date_Amount
    ON Orders(customer_id, order_date)
    INCLUDE(total_amount);

/* 2. Filtered stats */
UPDATE STATISTICS Orders;

/* 3. Verify performance */
SET STATISTICS IO, TIME ON;
SELECT c.name, SUM(o.total_amount) AS lifetime_value
FROM Customers c
JOIN Orders o ON o.customer_id = c.id
WHERE o.order_date >= '2023-01-01'
GROUP BY c.name
ORDER BY lifetime_value DESC;

How to Tune Queries in SQL Server Syntax


-- Show estimated plan without executing
SET SHOWPLAN_XML ON;
GO

-- Collect runtime stats
SET STATISTICS TIME ON;
SET STATISTICS IO ON;
GO

-- Update all statistics on a table
UPDATE STATISTICS Orders;

-- Rebuild or create an index
CREATE INDEX IX_Orders_Customer_Date
    ON Orders(customer_id, order_date DESC)
    INCLUDE(total_amount);

-- Force a specific plan via hint
SELECT total_amount
FROM Orders WITH (INDEX(IX_Orders_Customer_Date))
WHERE customer_id = 42;

Common Mistakes

Frequently Asked Questions (FAQs)

Does Query Store slow down production?

No significant impact for OLTP workloads; data is stored asynchronously. Keep size capped with MAX_STORAGE_SIZE_MB.

When should I rebuild vs. reorganize an index?

Rebuild when fragmentation > 30% or page density < 70%. Reorganize for 5–30% fragmentation to save resources.

Can I force the old plan back?

Yes. In Query Store, find the stable plan ID and click "Force Plan" to lock it until you unforce it.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.