Summarize wait types in SQL Server
What the instance's threads spend their time waiting for, ranked, once the idle background waits are removed.
The task
sys.dm_os_wait_stats accumulates, since the last restart, every millisecond threads spent waiting for anything: locks, latches, I/O, network, memory grants. Most of the total belongs to background threads that wait by design, such as the lazy writer sleeping between passes or dispatchers waiting for work, so the filter matters more than the ranking. With those removed, the top few rows usually point at one class of pressure: I/O, locking, log writes, memory grants or CPU.
The snippet
Written for SQL Server. Versions for PostgreSQL and MySQL are under Variations.
SELECT TOP 20
wait_type,
waiting_tasks_count,
CAST(wait_time_ms / 1000.0 AS DECIMAL(18,1)) AS wait_seconds,
CAST(100.0 * wait_time_ms / SUM(wait_time_ms) OVER () AS DECIMAL(5,1)) AS pct_of_all,
CAST(signal_wait_time_ms * 100.0 / NULLIF(wait_time_ms, 0) AS DECIMAL(5,1)) AS signal_pct
FROM sys.dm_os_wait_stats
WHERE wait_time_ms > 0
AND wait_type NOT LIKE 'SLEEP[_]%'
AND wait_type NOT LIKE 'QDS[_]%'
AND wait_type NOT LIKE 'HADR[_]%'
AND wait_type NOT LIKE 'PARALLEL[_]REDO%'
AND wait_type NOT LIKE 'BROKER[_]%'
AND wait_type NOT LIKE 'XE[_]%'
AND wait_type NOT IN (
'SOS_WORK_DISPATCHER', 'DISPATCHER_QUEUE_SEMAPHORE', 'LAZYWRITER_SLEEP',
'SQLTRACE_BUFFER_FLUSH', 'SQLTRACE_INCREMENTAL_FLUSH_SLEEP', 'CHECKPOINT_QUEUE',
'LOGMGR_QUEUE', 'REQUEST_FOR_DEADLOCK_SEARCH', 'DIRTY_PAGE_POLL', 'ONDEMAND_TASK_QUEUE',
'PWAIT_EXTENSIBILITY_CLEANUP_TASK', 'PVS_PREALLOCATE', 'SP_SERVER_DIAGNOSTICS_SLEEP',
'FT_IFTS_SCHEDULER_IDLE_WAIT', 'CLR_AUTO_EVENT', 'CLR_MANUAL_EVENT', 'WAITFOR',
'KSOURCE_WAKEUP', 'CHKPT', 'SERVER_IDLE_CHECK', 'PREEMPTIVE_XE_DISPATCHER',
'WAIT_XTP_OFFLINE_CKPT_NEW_LOG', 'TRACEWRITE'
)
ORDER BY wait_time_ms DESC;
작동 방식
wait_time_ms counts time waiting including the small signal portion (time ready to run but waiting for a CPU); a high signal_pct alongside big totals hints at CPU scheduler pressure rather than a resource wait. Dominant wait types map to causes: PAGEIOLATCH to buffer pool misses, LCK_M_ to lock contention, WRITELOG to transaction log latency, CXPACKET/CXCONSUMER to parallelism, RESOURCE_SEMAPHORE to memory grant queuing, PAGELATCH_ to hot-page contention. The DMV is cumulative since the instance started. To measure a specific period, copy it into a table, wait, and diff; DBCC SQLPERF('sys.dm_os_wait_stats', CLEAR) also works but resets the numbers for everyone else watching the instance.
Variations
PostgreSQL: current wait event distribution
PostgreSQLSELECT wait_event_type, wait_event, COUNT(*) AS sessions
FROM pg_stat_activity
WHERE wait_event IS NOT NULL
GROUP BY wait_event_type, wait_event
ORDER BY sessions DESC;
MySQL: instrumented wait summary
MySQLSELECT EVENT_NAME, COUNT_STAR,
ROUND(SUM_TIMER_WAIT / 1e12, 3) AS total_wait_sec,
ROUND(AVG_TIMER_WAIT / 1e9, 3) AS avg_wait_ms
FROM performance_schema.events_waits_summary_global_by_event_name
WHERE COUNT_STAR > 0
AND EVENT_NAME <> 'idle'
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 20;
Questions that come up
I reset the waits and everything shows CXPACKET. Is parallelism bad?
Not by itself. CXPACKET means parallel plans are running and their threads are waiting on each other; since SQL Server 2016 SP2 and 2017 CU3, the harmless consumer-side part is reported separately as CXCONSUMER. It deserves attention when it dominates on an OLTP server whose queries should not need parallelism, often because bad estimates inflate their cost past cost threshold for parallelism (default 5, which most guidance says is too low). Find which queries go parallel before changing MAXDOP or the threshold.
Why exclude those specific wait types from the ranking?
They belong to background threads that sleep or wait for work by design: the lazy writer, Query Store and Service Broker queues, Extended Events dispatchers, availability group workers. They accumulate time whether the server is busy or idle; on the instance used to test this page they were over 99 percent of all wait time. The list is not exhaustive and new versions add new idle waits, so if an unfamiliar wait with a SLEEP, QUEUE or DISPATCHER flavour tops your results, look it up before chasing it.
Keep reading
Related on SQLly
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.