The symptom
The query is slow on SQL Server - a report takes minutes, an OLTP point lookup takes seconds, or the whole instance feels heavy. The server-wide DMVs below need VIEW SERVER STATE (on SQL Server 2022 and later, VIEW SERVER PERFORMANCE STATE is enough); the sys.dm_db_* views need VIEW DATABASE STATE in the database you are looking at.
Diagnose it
Start with the waits
Waits tell you what the instance is short of before you open a single plan. Most rows in sys.dm_os_wait_stats are background threads idling, so filter them out or they fill the top of the list:
SELECT TOP 15 wait_type,
CAST(wait_time_ms / 1000.0 AS decimal(12, 1)) AS wait_s,
CAST(100.0 * wait_time_ms / SUM(wait_time_ms) OVER () AS decimal(5, 1)) AS pct,
waiting_tasks_count,
wait_time_ms / NULLIF(waiting_tasks_count, 0) AS avg_wait_ms
FROM sys.dm_os_wait_stats
WHERE waiting_tasks_count > 0
AND wait_type NOT LIKE N'%SLEEP%'
AND wait_type NOT LIKE N'%QUEUE%'
AND wait_type NOT LIKE N'XE[_]%'
AND wait_type NOT LIKE N'BROKER[_]%'
AND wait_type NOT LIKE N'QDS[_]%'
AND wait_type NOT IN (
N'SOS_WORK_DISPATCHER', N'CHECKPOINT_QUEUE', N'DIRTY_PAGE_POLL',
N'REQUEST_FOR_DEADLOCK_SEARCH', N'HADR_FILESTREAM_IOMGR_IOCOMPLETION',
N'HADR_TIMER_TASK', N'HADR_WORK_QUEUE', N'CLR_AUTO_EVENT',
N'CLR_MANUAL_EVENT', N'WAITFOR', N'ONDEMAND_TASK_QUEUE',
N'FT_IFTS_SCHEDULER_IDLE_WAIT', N'PWAIT_ALL_COMPONENTS_INITIALIZED',
N'PWAIT_EXTENSIBILITY_CLEANUP_TASK')
ORDER BY wait_time_ms DESC;
The numbers are cumulative since the last restart, so a bad hour last month still counts. Run it twice a few minutes apart during the slowdown and compare. How to read the top rows: PAGEIOLATCH_SH means sessions are waiting for data pages to come from disk; SOS_SCHEDULER_YIELD means CPU-bound threads using up their scheduler quantum; CXPACKET is parallel queries (CXCONSUMER is the mostly harmless half of it); LCK_M_* is blocking; WRITELOG is commit latency on the transaction log; RESOURCE_SEMAPHORE is queries waiting for a memory grant; ASYNC_NETWORK_IO usually means the client is reading results slowly, not that the server is slow. If an unfamiliar wait sits on top with a huge avg_wait_ms and nothing else looks wrong, it is probably another idle wait for the exclusion list.
Find the statements that cost the most
Query Store keeps plans and runtimes across restarts. It is on by default for new databases from SQL Server 2022; on older versions, or on databases restored from them, turn it on:
ALTER DATABASE [MyDb] SET QUERY_STORE = ON;
SELECT TOP 10 q.query_id, p.plan_id,
SUM(rs.count_executions) AS executions,
CAST(SUM(rs.avg_duration * rs.count_executions) / 1000.0 AS decimal(18, 1)) AS total_ms,
CAST(SUM(rs.avg_cpu_time * rs.count_executions) / 1000.0 AS decimal(18, 1)) AS cpu_ms,
LEFT(qt.query_sql_text, 80) AS query_text
FROM sys.query_store_runtime_stats AS rs
JOIN sys.query_store_plan AS p ON p.plan_id = rs.plan_id
JOIN sys.query_store_query AS q ON q.query_id = p.query_id
JOIN sys.query_store_query_text AS qt ON qt.query_text_id = q.query_text_id
GROUP BY q.query_id, p.plan_id, qt.query_sql_text
ORDER BY total_ms DESC;
Durations in Query Store are in microseconds, hence the division. One query_id with two plan_id rows and very different times is a plan regression. Without Query Store history, the plan cache answers the same question for whatever is still cached:
SELECT TOP 10
qs.total_elapsed_time / 1000 AS elapsed_ms,
qs.total_worker_time / 1000 AS cpu_ms,
qs.execution_count,
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 sql_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;
Get the actual plan and the I/O
For the query in hand, run it and collect what actually happened. The estimated plan only shows the optimizer's guesses, and the guesses are usually what went wrong:
SET STATISTICS IO, TIME ON;
SET STATISTICS XML ON; -- returns the actual plan as an extra result set
-- your query here
The messages output gives logical reads per table plus CPU and elapsed time; the actual plan (or Include Actual Execution Plan in SSMS) gives actual row counts per operator. Look for the operator whose actual rows dwarf its estimate: that is where the optimizer was guessing wrong. Save the plan XML and open it in the free query plan viewer to see the costs without SSMS; if the SQL itself is hard to read, the SQL 포매터 tidies it first. The SQLly desktop app also opens .sqlplan files in its own plan viewer.
Check the missing-index hints and the index usage
SELECT TOP 10 d.statement, d.equality_columns, d.inequality_columns,
d.included_columns, gs.user_seeks, gs.avg_user_impact
FROM sys.dm_db_missing_index_details AS d
JOIN sys.dm_db_missing_index_groups AS g
ON g.index_handle = d.index_handle
JOIN sys.dm_db_missing_index_group_stats AS gs
ON gs.group_handle = g.index_group_handle
ORDER BY gs.avg_total_user_cost * gs.avg_user_impact * (gs.user_seeks + gs.user_scans) DESC;
Treat these as hints, not orders - the DMV cannot consolidate overlapping suggestions, lists columns in table order rather than the best key order, knows nothing about write cost, and empties on every restart. The mirror question matters too: sys.dm_db_index_usage_stats shows which indexes are never sought or scanned, and every unused index is a tax on every insert.
Fix it
Most slow SQL Server queries come down to one of four fixes, in this order:
Add the index the plan asked for. Build it with the equality columns as keys, inequality columns next, and the columns the SELECT touches as INCLUDEs so the plan stops doing Key Lookups. One index that covers the overlapping suggestions beats the three near-duplicates the DMV lists.
Refresh stale statistics. Bad estimates come from stats sampled long before the data changed:
UPDATE STATISTICS dbo.Orders WITH FULLSCAN;
Deal with parameter sniffing on its own terms. If the cached plan is perfect for one parameter value and terrible for another, OPTION (RECOMPILE) on the offending statement is often the cheapest correct answer, as long as the statement does not run thousands of times a second. Query Store can also force the plan that behaves well for the common case (sp_query_store_force_plan), and at compatibility level 160 (SQL Server 2022) Parameter Sensitive Plan optimization can cache more than one plan for a skewed equality predicate on its own.
Stop the bleeding from blocking. If waits show LCK_M_*, the slow query may be a victim, not a cause - the blocking and deadlocks guide covers finding and fixing the writer it waits behind.
The checklist
Work through it in order. Each step gives you the evidence for the next one.
- Run the wait-stats query and name the top wait: CPU, I/O, lock or log.
- Identify the 3 statements with the highest total elapsed time from Query Store or the plan cache.
- Run the worst one with SET STATISTICS IO, TIME ON and the actual execution plan.
- Compare estimated vs actual rows on the most expensive operator; if they diverge by orders of magnitude, refresh statistics first.
- Create one covering index for the dominant seek, keyed on the equality predicate.
- Re-run and record: logical reads before and after, and elapsed time.
- Re-check sys.dm_db_index_usage_stats a week later and drop indexes nothing uses.
Questions that come up
Where do I start when everything on the server is slow?
Start with sys.dm_os_wait_stats, not with a plan. The dominant wait tells you whether the problem is CPU, disk I/O, blocking or the log, and that decides which of the four fixes applies. If the top wait is LCK_M_*, a slow report is often just waiting on another session.
Are the missing-index DMV suggestions safe to create?
They are directions, not scripts. The DMV suggests one index per query shape and cannot merge overlapping suggestions, so blindly creating all of them leaves you with near-duplicate indexes that slow down every write. Take the top suggestion, consolidate the similar ones into a single index with INCLUDE columns, and measure.
Does an execution plan with a warning always mean a problem?
Not always, but always look. The common warnings - implicit conversion, spill to tempdb, missing join predicate - each cost real time on large inputs, and each has a targeted fix: a cast that matches the column type, an index that keeps the sort in memory, or a predicate that was lost in the WHERE clause.
How often should statistics be updated?
Let auto-update handle most tables and intervene where it is known to lag. Since compatibility level 130 (SQL Server 2016) the auto-update threshold on large tables is roughly SQRT(1000 x rows) modified rows instead of 20%, so big tables refresh far more often than they used to, but the refresh is sampled. Ascending key columns like order dates still fall behind: new rows land past the last histogram step, where the estimate is a guess (the legacy cardinality estimator guesses 1 row). A scheduled UPDATE STATISTICS ... WITH FULLSCAN on the handful of hot tables fixes most estimate problems without touching the rest.
Can I see execution plans without installing SSMS?
Yes. Save the plan as a .sqlplan file or paste the plan XML into SQLly's free web plan viewer at /tools/query-plan-viewer - it runs entirely in your browser. The SQLly desktop app has the same viewer built in next to the query editor.