Guides

SQL Server wait stats: measure waits over a window, not since restart

By Scott Coffey, Senior Database Developer. Updated 2026-10-30.

The server is slow right now. You run the usual query against sys.dm_os_wait_stats, sort by wait_time_ms, and the top of the list says PAGEIOLATCH_SH or CXPACKET. That may have nothing to do with today. The view is cumulative since the instance last started, so on a server that has been up for a month, one bad night of index rebuilds can outweigh the problem users are calling about.

The fix is to measure waits over a window: take a snapshot, wait, take another, and look only at the difference. The queries below are read-only except for the optional history table in step 5. They need VIEW SERVER STATE, or VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later.

Step 1: check how old the numbers are

SELECT sqlserver_start_time,
       DATEDIFF(HOUR, sqlserver_start_time, SYSDATETIME()) AS hours_since_start
FROM sys.dm_os_sys_info;

If the answer is hundreds of hours, the cumulative totals are an average over everything that happened in that time: backups, maintenance, month-end reports, and quiet weekends. They are useful for a long-term profile of the server. They are not evidence about the last ten minutes. The counters can also have been reset by someone running DBCC SQLPERF with CLEAR, which sqlserver_start_time will not show.

Step 2: snapshot, wait, and diff

SET NOCOUNT ON;
DECLARE @Seconds int = 30;   -- sample window
DECLARE @Delay char(8) = CONVERT(char(8), DATEADD(SECOND, @Seconds, CAST('00:00:00' AS time)), 108);

IF OBJECT_ID('tempdb..#ws1') IS NOT NULL DROP TABLE #ws1;
SELECT wait_type, waiting_tasks_count, wait_time_ms, signal_wait_time_ms
INTO #ws1
FROM sys.dm_os_wait_stats;

WAITFOR DELAY @Delay;

SELECT TOP (20)
       w.wait_type,
       w.waiting_tasks_count - ISNULL(a.waiting_tasks_count, 0) AS tasks,
       w.wait_time_ms        - ISNULL(a.wait_time_ms, 0)        AS wait_ms,
       w.signal_wait_time_ms - ISNULL(a.signal_wait_time_ms, 0) AS signal_ms
FROM sys.dm_os_wait_stats AS w
LEFT JOIN #ws1 AS a ON a.wait_type = w.wait_type
WHERE w.wait_time_ms - ISNULL(a.wait_time_ms, 0) > 0
  AND w.wait_type NOT IN (N'WAITFOR', N'SLEEP_TASK', N'LAZYWRITER_SLEEP',
        N'CHECKPOINT_QUEUE', N'DIRTY_PAGE_POLL', N'LOGMGR_QUEUE',
        N'XE_TIMER_EVENT', N'XE_DISPATCHER_WAIT', N'REQUEST_FOR_DEADLOCK_SEARCH',
        N'SOS_WORK_DISPATCHER', N'DISPATCHER_QUEUE_SEMAPHORE',
        N'SQLTRACE_INCREMENTAL_FLUSH_SLEEP', N'BROKER_TO_FLUSH',
        N'HADR_FILESTREAM_IOMGR_IOCOMPLETION')
ORDER BY wait_ms DESC;

Everything stays in a session-scoped temp table, so nothing is written to a user database and nothing is reset. Run it while the problem is happening. Thirty seconds is a reasonable starting window; stretch it to a few minutes if the slowdown comes and goes.

Step 3: filter the idle waits

Without a filter, the top of a short sample is almost always background threads waiting for work: the checkpoint and lazy writer, Extended Events timers, Service Broker, and the deadlock monitor. Those waits are normal and say nothing about user pain. The list above is trimmed for readability. The free wait-stats-delta.sql script carries a longer exclusion list, including the Query Store, availability group, and in-memory OLTP idle waits, and adds percentage of the window and average wait per task.

One quirk to expect: a wait is added to the counters when it ends. A background wait that lasted twenty minutes can land in a 30-second window all at once, so a single huge number in a short sample is often one long idle wait finishing, not a burst of trouble. Check tasks next to wait_ms.

Step 4: read the result

signal_ms is time a task spent ready to run but waiting for a CPU after its resource became available. The rest, wait_ms - signal_ms, is time spent waiting on the resource itself. A high signal share alongside SOS_SCHEDULER_YIELD points toward CPU pressure.

The usual suspects, as starting points rather than verdicts:

  • LCK_M_*: blocking. Find the head blocker with the blocking guide.
  • PAGEIOLATCH_SH / PAGEIOLATCH_EX: reading data pages from disk, often scans that do not fit in memory.
  • WRITELOG: waiting for transaction log writes, often many tiny commits or slow log storage.
  • CXPACKET / CXCONSUMER: parallel queries. Usually a symptom; look at the queries, not only at MAXDOP.
  • ASYNC_NETWORK_IO: the client is slow to consume results, typically an application reading row by row.
  • RESOURCE_SEMAPHORE: queries waiting for a memory grant.

To see which session owns a wait, SQL Server 2016 and later expose per-session totals that reset when the session ends:

SELECT session_id, wait_type, waiting_tasks_count,
       wait_time_ms, signal_wait_time_ms
FROM sys.dm_exec_session_wait_stats
WHERE session_id = 91          -- the session you are investigating
ORDER BY wait_time_ms DESC;

Step 5: keep a history instead of clearing

It is tempting to run DBCC SQLPERF('sys.dm_os_wait_stats', CLEAR) and start fresh. Avoid it on shared servers: it wipes the baseline for everyone, including monitoring tools that compute their own deltas. A small table in a DBA utility database, filled by a SQL Server Agent job every 15 minutes, gives you deltas for any past window:

IF OBJECT_ID(N'dbo.WaitStatsSnapshot') IS NULL
CREATE TABLE dbo.WaitStatsSnapshot (
    capture_time        datetime2(0) NOT NULL,
    wait_type           nvarchar(60) NOT NULL,
    waiting_tasks_count bigint       NOT NULL,
    wait_time_ms        bigint       NOT NULL,
    signal_wait_time_ms bigint       NOT NULL,
    CONSTRAINT PK_WaitStatsSnapshot PRIMARY KEY (capture_time, wait_type)
);

-- Agent job step (T-SQL), every 15 minutes
INSERT dbo.WaitStatsSnapshot
SELECT SYSDATETIME(), wait_type, waiting_tasks_count, wait_time_ms, signal_wait_time_ms
FROM sys.dm_os_wait_stats
WHERE wait_time_ms > 0;
WITH s AS (
    SELECT capture_time, wait_type, wait_time_ms, signal_wait_time_ms,
           LAG(wait_time_ms)        OVER (PARTITION BY wait_type ORDER BY capture_time) AS prev_wait_ms,
           LAG(signal_wait_time_ms) OVER (PARTITION BY wait_type ORDER BY capture_time) AS prev_signal_ms
    FROM dbo.WaitStatsSnapshot
)
SELECT capture_time, wait_type,
       wait_time_ms - prev_wait_ms          AS wait_ms,
       signal_wait_time_ms - prev_signal_ms AS signal_ms
FROM s
WHERE prev_wait_ms IS NOT NULL
  AND wait_time_ms >= prev_wait_ms          -- skip intervals that span a restart or clear
  AND wait_time_ms - prev_wait_ms > 0
ORDER BY capture_time DESC, wait_ms DESC;

Apply the same idle-wait filter here, and add a cleanup step to the job that deletes rows older than your retention period. Now "what were we waiting on at 2 PM yesterday" is a query, not a guess. The SQL Rig agent-job-create-template.sql file is a starting point for the job itself.