How to trace and tuning the SQL Query running slow in SQL Server

SQL Server Slow Query: Complete Trace and Tuning Approach

This guide presents a structured SQL Server slow-query investigation: identify long-running requests, modules, waits, and blocking; examine execution plans, IO, CPU, indexes, and statistics; address parameter sensitivity, memory grants, and tempdb spills; use Query Store and cached-query DMVs for history and costly statements; assess server-wide waits, memory, and disk IO; then apply, validate, and monitor fixes.

What will follow:

Module → Session → Query → Wait → Execution Plan → IO/CPU → Index/Statistics → Blocking → Fix → Validate

  1. Try to find the Query which is running based on elapsed time
SELECT
r.session_id,
r.status,
r.command,
r.cpu_time,
r.total_elapsed_time,
r.reads,
r.writes,
r.logical_reads,
r.wait_type,
r.wait_time,
r.wait_resource,
r.blocking_session_id,
DB_NAME(r.database_id) AS database_name,
s.host_name,
s.program_name,
s.login_name,
t.text AS sql_text
FROM sys.dm_exec_requests r
JOIN sys.dm_exec_sessions s
ON r.session_id = s.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE r.session_id <> @@SPID
ORDER BY r.total_elapsed_time DESC;

2. Identify the application/module

SELECT
session_id,
login_name,
host_name,
program_name,
database_id,
status,
last_request_start_time,
last_request_end_time
FROM sys.dm_exec_sessions
WHERE is_user_process = 1;

3. Check the blocking session if it is blocked or not

SELECT
session_id,
blocking_session_id,
wait_type,
wait_time,
wait_resource
FROM sys.dm_exec_requests
WHERE blocking_session_id <> 0;

4. Check what the query is waiting for

SELECT
session_id,
wait_type,
wait_time,
wait_resource,
blocking_session_id
FROM sys.dm_exec_requests
WHERE session_id = <SPID>;

5. Get the actual execution plan

Query → Include Actual Execution Plan in SQL Server Management Studio

SET STATISTICS IO ON;
SET STATISTICS TIME ON;
-- Query here
SET STATISTICS TIME OFF;
SET STATISTICS IO OFF;

6. Check the execution plan – Like Table Scan, Clustered Index Scan, Index Scan, Index seek, Compare estimated rows vs actual rows, check statistics io, check cpu and elapsed time with statistic time on

7. Check indexes

SELECT
i.name,
i.type_desc,
c.name AS column_name,
ic.key_ordinal,
ic.is_included_column
FROM sys.indexes i
JOIN sys.index_columns ic
ON i.object_id = ic.object_id
AND i.index_id = ic.index_id
JOIN sys.columns c
ON ic.object_id = c.object_id
AND ic.column_id = c.column_id
WHERE i.object_id = OBJECT_ID('dbo.Orders')
ORDER BY i.index_id, ic.key_ordinal;

8. Check indexes usages in SQL Server

SELECT
OBJECT_NAME(i.object_id) AS table_name,
i.name,
us.user_seeks,
us.user_scans,
us.user_lookups,
us.user_updates
FROM sys.indexes i
LEFT JOIN sys.dm_db_index_usage_stats us
ON us.object_id = i.object_id
AND us.index_id = i.index_id
AND us.database_id = DB_ID()
WHERE i.object_id > 100
ORDER BY us.user_scans DESC;

9. Check statistics

DBCC SHOW_STATISTICS ('dbo.Orders', 'IX_Orders_CustomerID_OrderDate');
OR
SELECT
OBJECT_NAME(s.object_id) AS table_name,
s.name AS statistics_name,
STATS_DATE(s.object_id, s.stats_id) AS last_updated
FROM sys.stats s
WHERE s.object_id = OBJECT_ID('dbo.Orders');
UPDATE STATISTICS dbo.Orders IX_Orders_CustomerID_OrderDate;
or
UPDATE STATISTICS dbo.Orders WITH FULLSCAN;

10. Use of option recompile

If some procedure run quick in some parameter and slow in some parameter then we use recompile option to make execution plan on recompile

11. Use of Query Store

For historical problems, Query Store is one of the first places I would look.

Check whether it’s enabled:

SELECT
actual_state_desc,
desired_state_desc,
current_storage_size_mb,
max_storage_size_mb
FROM sys.database_query_store_options;

Query Store lets you investigate:

  • Query history
  • Runtime
  • Execution count
  • Multiple plans
  • Plan regression
  • CPU
  • Duration
  • Logical I/O

12. Check top expensive cached queries

SELECT TOP 20
qs.execution_count,
qs.total_worker_time / 1000 AS total_cpu_ms,
qs.total_elapsed_time / 1000 AS total_elapsed_ms,
qs.total_logical_reads,
qs.total_logical_writes,
qs.total_worker_time /
NULLIF(qs.execution_count,0) / 1000 AS avg_cpu_ms,
qs.total_elapsed_time /
NULLIF(qs.execution_count,0) / 1000 AS avg_elapsed_ms,
qs.total_logical_reads /
NULLIF(qs.execution_count,0) AS avg_logical_reads,
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 qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY avg_elapsed_ms DESC;

13. Check tempdb, sort, hash and memory grants

Execution plans may show:

  • Sort
  • Hash Match
  • Spill to tempdb
  • Memory warning
  • Excessive memory grant
SELECT * FROM sys.dm_exec_query_memory_grants;

14. Check server-level resource pressure

Check over all waits

SELECT TOP 20
wait_type,
waiting_tasks_count,
wait_time_ms,
signal_wait_time_ms
FROM sys.dm_os_wait_stats
WHERE wait_type NOT LIKE 'SLEEP%'
ORDER BY wait_time_ms DESC;

Check memory

SELECT *
FROM sys.dm_os_process_memory;

Check Disk I/O

SELECT
DB_NAME(vfs.database_id) AS database_name,
mf.name,
mf.physical_name,
vfs.num_of_reads,
vfs.io_stall_read_ms,
vfs.num_of_writes,
vfs.io_stall_write_ms
FROM sys.dm_io_virtual_file_stats(NULL,NULL) vfs
JOIN sys.master_files mf
ON vfs.database_id = mf.database_id
AND vfs.file_id = mf.file_id;