Skip to content

Top waits since restart

What the server spends its time waiting on. Filters out the benign background waits.

SQL Server · SQL
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 DESC

This 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 typeUsual meaning
PAGEIOLATCH_SH, PAGEIOLATCH_EXReading data pages from disk. Slow storage or not enough memory.
LCK_M_*Waiting for a lock. Blocking between sessions.
WRITELOGWaiting for the transaction log to be flushed to disk.
SOS_SCHEDULER_YIELDTasks giving up the CPU and queuing for it. Often CPU pressure.
CXPACKET, CXCONSUMERParallel queries coordinating threads. Often normal.
ASYNC_NETWORK_IOThe client is slow to consume the results.
RESOURCE_SEMAPHOREQueries 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.

Waits during the next 5 minutes
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.

Frequently asked questions

What are wait stats in SQL Server?
They are counters of how long tasks waited for resources, by wait type, kept in sys.dm_os_wait_stats since the last restart. They show where the server spends its time waiting, such as disk, locks, log or CPU.
Which SQL Server waits can I ignore?
Idle and background waits such as SLEEP_TASK, LAZYWRITER_SLEEP, CHECKPOINT_QUEUE and the BROKER and XE families. This template already filters a list of them. Others may remain, so judge a wait by whether it matches the symptom.
What does PAGEIOLATCH_SH mean?
A task is waiting for a data page to be read from disk into memory. High values point to slow storage, missing indexes that force large reads, or too little memory for the working set.
How do I reset SQL Server wait stats?
Run DBCC SQLPERF with the sys.dm_os_wait_stats argument and CLEAR. It zeroes all counters for everyone, so for a clean measurement take two readings and subtract.