How to Find and Fix Slow Queries in SQL Server

SQL Server 5 min readUpdated Oct 30, 2026
3 views0 comments

“The database is slow” is a feeling, not a diagnosis. Before you add indexes or buy more hardware, you need to know which queries are slow, how slow they are and why. This guide shows a simple, repeatable method: measure, read the plan, change one thing, measure again.

Step 1: Find the queries that cost the most

SQL Server keeps running totals for every query whose plan is in cache. This query lists the ten statements that have consumed the most total time since the plan was cached:

SELECT TOP 10
       qs.execution_count,
       qs.total_elapsed_time / 1000                          AS total_ms,
       qs.total_elapsed_time / qs.execution_count / 1000     AS avg_ms,
       qs.total_logical_reads / qs.execution_count           AS avg_logical_reads,
       DB_NAME(st.dbid)                                      AS database_name,
       SUBSTRING(st.text, (qs.statement_start_offset / 2) + 1,
         ((CASE qs.statement_end_offset WHEN -1 THEN DATALENGTH(st.text)
           ELSE qs.statement_end_offset END - qs.statement_start_offset) / 2) + 1) AS statement_text
FROM   sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
ORDER BY qs.total_elapsed_time DESC;

Two things to keep in mind. The times are stored in microseconds, which is why we divide by 1000. And these numbers disappear when the server restarts or a plan leaves the cache, so they only show recent history.

Total time matters more than the slowest single run. A query that takes 50 ms but runs a million times is a bigger problem than one that takes 30 seconds once a day.

Step 2: Turn on Query Store for a permanent record

Query Store (SQL Server 2016 and later) records queries, plans and runtime statistics inside the database, so history survives restarts. It is on by default for new databases in SQL Server 2022. Enable it on older databases:

ALTER DATABASE SalesDB SET QUERY_STORE = ON;
ALTER DATABASE SalesDB SET QUERY_STORE (OPERATION_MODE = READ_WRITE, QUERY_CAPTURE_MODE = AUTO);

In SQL Server Management Studio, expand the database and open Query Store. The reports Top Resource Consuming Queries and Regressed Queries show the worst queries and any query that suddenly became slower, which usually means its plan changed. You can also ask with T-SQL:

SELECT TOP 10
       q.query_id,
       SUM(rs.count_executions)                                         AS executions,
       SUM(rs.avg_duration * rs.count_executions) / SUM(rs.count_executions) / 1000 AS avg_ms,
       qt.query_sql_text
FROM   sys.query_store_query AS q
JOIN   sys.query_store_query_text AS qt     ON qt.query_text_id = q.query_text_id
JOIN   sys.query_store_plan AS p            ON p.query_id = q.query_id
JOIN   sys.query_store_runtime_stats AS rs  ON rs.plan_id = p.plan_id
GROUP BY q.query_id, qt.query_sql_text
ORDER BY SUM(rs.avg_duration * rs.count_executions) DESC;

Step 3: Measure one query properly

Pick the worst query and run it in a test window with statistics turned on:

SET STATISTICS IO ON;
SET STATISTICS TIME ON;

-- your query here

SET STATISTICS IO OFF;
SET STATISTICS TIME OFF;

On the Messages tab, note two numbers: logical reads (pages read from memory, the best measure of the work done) and elapsed time. Write them down. These are your “before” numbers.

Step 4: Read the actual execution plan

Press Ctrl+M in SSMS (Include Actual Execution Plan) and run the query again. A Plan tab appears. Read it from right to left, and look for these warning signs:

What you see What it usually means
A Table Scan or Clustered Index Scan on a big table for a selective filter No suitable index, or the filter cannot use one
A Key Lookup executed thousands of times The index is missing some columns the query needs; consider adding them with INCLUDE
Thick arrows where the estimated rows are far from the actual rows Outdated statistics, or the optimizer is guessing wrongly
A yellow warning triangle: “spill to tempdb” Not enough memory granted for a sort or hash
A warning about implicit conversion Comparing columns of different types, which prevents index use

Step 5: Apply the usual fixes

Make the filter “sargable”

A filter is sargable when SQL Server can use an index to find the rows. Wrapping the column in a function destroys that:

-- slow: the function runs on every row, the index is useless
WHERE YEAR(OrderDate) = 2026

-- fast: the column is left alone, a range is used
WHERE OrderDate >= '2026-01-01' AND OrderDate < '2027-01-01'

Match data types

-- column is VARCHAR, the parameter is NVARCHAR: implicit conversion, scan
WHERE CustomerCode = N'AB-1001'

-- same type: index seek
WHERE CustomerCode = 'AB-1001'

Add or adjust an index

If the plan shows a scan for a selective filter, an index on the filtered columns is the classic answer. The next tutorial in this series explains how to design it.

Refresh statistics

If estimated and actual rows differ a lot:

UPDATE STATISTICS dbo.Orders WITH FULLSCAN;

Stop SELECT *

Ask only for the columns you need. It reduces reads, memory and network traffic, and lets narrow indexes cover the query.

Parameter sniffing: fast for some values, slow for others

SQL Server builds a plan using the first parameter values it sees and reuses it. If those values were unusual, the plan is bad for everyone else. Signs: the same stored procedure is fast one day and slow the next, or fast in SSMS and slow from the application. Options, from least to most invasive: update statistics, add OPTION (RECOMPILE) to the statement, or use Query Store to force the good plan:

EXEC sp_query_store_force_plan @query_id = 42, @plan_id = 17;

Step 6: Measure again

Run the query with STATISTICS IO, TIME once more and compare with your “before” numbers. If logical reads dropped from 2,000,000 to 300, you have proof. If nothing changed, undo your change. Do not keep fixes that you cannot measure.

Golden rules

  1. Change one thing at a time, so you know what helped.
  2. Test on a copy of the data, with realistic volume. A query that is fast on 1,000 rows may be slow on 50 million.
  3. Keep your before-and-after numbers in a note. They justify the change to others and to your future self.
Did you find this helpful?Tap the heart to let us know. It takes one second and no account.

More tutorials like this

Leave a Comment

Your email address will not be published. Required fields are marked *

Bigland Network IT

Practical tutorials for people who run, build and fix things: operating systems, databases, development, networking and security.

Topics

More

Site

© 2026 Bigland Network. All rights reserved.Always test commands on a non-production system first.
Scroll to Top