SELECT TOP 15 wait_type, wait_time_ms / 1000 AS wait_seconds, waiting_tasks_count, CAST(100.0 * wait_time_ms / NULLIF(SUM(wait_time_ms) OVER (), 0) AS decimal(5,2)) AS pctFROM sys.dm_os_wait_statsWHERE wait_type NOT LIKE 'SLEEP%' AND wait_type NOT LIKE 'BROKER%' AND wait_type NOT LIKE 'XE%' AND wait_type NOT IN ('CLR_AUTO_EVENT','CLR_MANUAL_EVENT','LAZYWRITER_SLEEP','CHECKPOINT_QUEUE', 'REQUEST_FOR_DEADLOCK_SEARCH','LOGMGR_QUEUE','ONDEMAND_TASK_QUEUE', 'DIRTY_PAGE_POLL','HADR_FILESTREAM_IOMGR_IOCOMPLETION','SP_SERVER_DIAGNOSTICS_SLEEP', 'QDS_PERSIST_TASK_MAIN_LOOP_SLEEP','QDS_CLEANUP_STALE_QUERIES_TASK_MAIN_LOOP_SLEEP', 'WAITFOR','FT_IFTS_SCHEDULER_IDLE_WAIT','DISPATCHER_QUEUE_SEMAPHORE','SQLTRACE_INCREMENTAL_FLUSH_SLEEP')ORDER BY wait_time_ms DESCThis query is part of the "SQL Server health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.
What the query shows
Every time a task in SQL Server has to wait for something (a disk read, a lock, a log write, a free CPU), the engine adds that time to a counter by wait type. This query reads sys.dm_os_wait_stats, removes the waits that are just idle background threads, and shows the top 15 by total time.
Run it as the first step of a performance investigation. It tells you whether the server spends its time on disk, on locks, on CPU or on the network, before you look at any single query.
How to read each column
- wait_type: what the tasks waited for.
- wait_seconds: wait_time_ms divided by 1000, in whole seconds. It includes signal wait, the time spent waiting for a CPU after the resource was ready.
- waiting_tasks_count: how many waits happened. Divide time by count to get the average wait.
- pct: share of the total among the rows that survived the filter, not of every wait on the server.
| Wait type | Usual meaning |
|---|---|
| PAGEIOLATCH_SH, PAGEIOLATCH_EX | Reading data pages from disk. Slow storage or not enough memory. |
| LCK_M_* | Waiting for a lock. Blocking between sessions. |
| WRITELOG | Waiting for the transaction log to be flushed to disk. |
| SOS_SCHEDULER_YIELD | Tasks giving up the CPU and queuing for it. Often CPU pressure. |
| CXPACKET, CXCONSUMER | Parallel queries coordinating threads. Often normal. |
| ASYNC_NETWORK_IO | The client is slow to consume the results. |
| RESOURCE_SEMAPHORE | Queries waiting for a memory grant. |
Healthy and worrying results
Some waiting is always present, so there is no clean list. Look at the top three and ask whether they match the complaint. If users say reports are slow and PAGEIOLATCH tops the list, check storage and memory. If the application freezes and LCK_M leads, go to the blocking template.
As a rule of thumb, one wait type above half of the filtered total deserves attention. Heavy CXPACKET alone is no proof of a problem, because parallelism is normal on a busy server. Do not tune settings from one wait name without confirming it with a query-level view.
What to do about it
The numbers are cumulative since startup, so a server that has been up for months hides today. To see what is happening now, take two readings and subtract.
SELECT wait_type, wait_time_ms, waiting_tasks_countINTO #w1FROM sys.dm_os_wait_stats; WAITFOR DELAY '00:05:00'; SELECT TOP 10 w2.wait_type, (w2.wait_time_ms - w1.wait_time_ms) / 1000 AS wait_seconds, w2.waiting_tasks_count - w1.waiting_tasks_count AS waitsFROM sys.dm_os_wait_stats w2JOIN #w1 w1 ON w1.wait_type = w2.wait_typeORDER BY w2.wait_time_ms - w1.wait_time_ms DESC;Resetting the counters is also possible, but it erases history for everyone who reads them, so prefer the two-readings approach.
DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR);Limits to know
- The counters are cumulative since the last restart or reset, so an old server averages out recent trouble.
- The filter in the query is a short list. Other benign waits, for example from availability groups, may still appear.
- A wait name points at a resource, not at the query that caused it. Use the top CPU and blocking templates to find the query.
- It needs VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later). Azure SQL Database has database-scoped wait statistics, so check Microsoft documentation there.
Run it on a schedule in DataRunner
Run this query once a day in DataRunner and send it by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets. Keeping the daily output lets you see when the top wait changed, which is often the day something went wrong. A restart shows as a sudden drop in all the numbers.