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_ms includes 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 with DBCC 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:

WaitMeaningLook at
PAGEIOLATCH_SH/EXReading data pages from diskMissing indexes, scans, memory, storage latency (sys.dm_io_virtual_file_stats)
WRITELOGWaiting for log flush at commitLog disk latency, many tiny commits (batch them)
LCK_M_*Blocked on a lockHead blocker, long transactions, RCSI (sqlserver-locking-blocking-deadlocks)
CXPACKET / CXCONSUMERParallel query coordinationMAXDOP, cost threshold, skewed parallelism; not a problem by itself
SOS_SCHEDULER_YIELDTask used its quantum, waiting for CPUCPU-bound queries
RESOURCE_SEMAPHOREWaiting for a query memory grantLarge sorts/hashes, bad estimates, memory
ASYNC_NETWORK_IOClient is slow to consume resultsApplication reading row by row
LATCH_EX / PAGELATCH_*In-memory page or structure contentiontempdb allocation, last-page insert hot spots
THREADPOOLNo worker threads availableBlocking pile-up, too many connections
HADR_SYNC_COMMITWaiting for synchronous AG replicasqlserver-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 CXPACKET by setting MAXDOP 1 blindly; find the expensive queries first.
  • High PAGEIOLATCH is 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_stats for per-file stall.
  • Do not clear wait stats on a shared production server without telling the team.

Practice questions

  1. What does a high signal_wait_time_ms / wait_time_ms ratio indicate?
  2. WRITELOG dominates. What are three possible fixes?
  3. Why can RESOURCE_SEMAPHORE appear while the server has free memory?

References

sqlserver-architecture · sqlserver-monitoring-dmvs · sqlserver-locking-blocking-deadlocks · sqlserver-query-store