Overview
Every time a task cannot continue it records a wait type and a duration. Wait statistics tell you what the server spends its time waiting on, which is the best first view of a performance problem: tune what the server waits for, not what you guess.
Core concepts
- A task cycles between running (on a scheduler), runnable (ready, waiting for CPU: signal wait) and suspended (waiting for a resource: resource wait).
wait_time_msincludes the signal part (signal_wait_time_ms); high signal wait means CPU pressure. - Instance level:
sys.dm_os_wait_stats(cumulative since start or since cleared withDBCC SQLPERF('sys.dm_os_wait_stats', CLEAR)). Session level:sys.dm_exec_session_wait_stats. Current waits:sys.dm_os_waiting_tasks,sys.dm_exec_requests.wait_type. Per query over time: Query Store wait stats (sqlserver-query-store). - Many waits are benign background waits (
SLEEP_*,LAZYWRITER_SLEEP,XE_*,*_QUEUE,BROKER_*,WAITFOR); filter them out.
Common waits and what to check:
| Wait | Meaning | Look at |
|---|---|---|
PAGEIOLATCH_SH/EX | Reading data pages from disk | Missing indexes, scans, memory, storage latency (sys.dm_io_virtual_file_stats) |
WRITELOG | Waiting for log flush at commit | Log disk latency, many tiny commits (batch them) |
LCK_M_* | Blocked on a lock | Head blocker, long transactions, RCSI (sqlserver-locking-blocking-deadlocks) |
CXPACKET / CXCONSUMER | Parallel query coordination | MAXDOP, cost threshold, skewed parallelism; not a problem by itself |
SOS_SCHEDULER_YIELD | Task used its quantum, waiting for CPU | CPU-bound queries |
RESOURCE_SEMAPHORE | Waiting for a query memory grant | Large sorts/hashes, bad estimates, memory |
ASYNC_NETWORK_IO | Client is slow to consume results | Application reading row by row |
LATCH_EX / PAGELATCH_* | In-memory page or structure contention | tempdb allocation, last-page insert hot spots |
THREADPOOL | No worker threads available | Blocking pile-up, too many connections |
HADR_SYNC_COMMIT | Waiting for synchronous AG replica | sqlserver-always-on network/latency |
Worked example
After the blocking test on the test instance, filtered of benign waits (excerpt):
SELECT TOP 8 wait_type, waiting_tasks_count, wait_time_ms
FROM sys.dm_os_wait_stats
WHERE wait_type NOT LIKE 'SLEEP%' AND wait_type NOT LIKE 'XE%' AND wait_type NOT LIKE '%QUEUE%'
AND wait_type NOT IN ('LAZYWRITER_SLEEP','WAITFOR','DIRTY_PAGE_POLL' /* ... maintain a longer list */)
AND waiting_tasks_count > 0
ORDER BY wait_time_ms DESC;The list included LATCH_EX (21,799 waits), PREEMPTIVE_OS_FLUSHFILEBUFFERS (file flush), and the lock waits LCK_M_S (12 waits, 9.8 s), LCK_M_U and LCK_M_X produced by the blocking and deadlock tests. LCK_M_S 9.8 s matches the 8 s reader wait plus the earlier tests.
Workflow: snapshot twice (before and after 5-15 minutes under load), subtract, rank by delta, then map the top wait to a cause using the table above.
Pitfalls and production notes
- Totals since startup mix history with the present; use deltas.
- Do not tune
CXPACKETby setting MAXDOP 1 blindly; find the expensive queries first. - High
PAGEIOLATCHis often a query problem (scans) before it is a storage problem. - Combine waits with the plan and Query Store; a wait has no meaning without the statement that caused it.
- Cloud and containers add I/O latency; check
sys.dm_io_virtual_file_statsfor per-file stall. - Do not clear wait stats on a shared production server without telling the team.
Practice questions
- What does a high
signal_wait_time_ms / wait_time_msratio indicate? WRITELOGdominates. What are three possible fixes?- Why can
RESOURCE_SEMAPHOREappear while the server has free memory?
References
- Microsoft Learn, sys.dm_os_wait_stats
- SQLskills wait types library: https://www.sqlskills.com/help/waits/
Related
sqlserver-architecture · sqlserver-monitoring-dmvs · sqlserver-locking-blocking-deadlocks · sqlserver-query-store