dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/sqlserver/#47
Script #47
sqlserver
Monitoringread-onlyMin Version: 2008

SQL Server Wait Stats: Bottleneck Analysis

Lists top waits with percentages. The starting point to tell CPU, disk, locking or parallelism bottlenecks apart.

#wait-stats#bottleneck#dmv
sqlserver · sql
SELECT TOP 15
       wait_type,
       waiting_tasks_count,
       CAST(wait_time_ms / 1000.0 AS DECIMAL(18,1)) AS wait_sec,
       CAST(100.0 * wait_time_ms / SUM(wait_time_ms) OVER () AS DECIMAL(5,2)) AS pct
FROM sys.dm_os_wait_stats
WHERE wait_type NOT IN (
  'SLEEP_TASK','SLEEP_SYSTEMTASK','LAZYWRITER_SLEEP','SQLTRACE_BUFFER_FLUSH',
  'BROKER_TASK_STOP','BROKER_TO_FLUSH','XE_TIMER_EVENT','XE_DISPATCHER_WAIT',
  'REQUEST_FOR_DEADLOCK_SEARCH','CHECKPOINT_QUEUE','DIRTY_PAGE_POLL',
  'HADR_FILESTREAM_IOMGR_IOCOMPLETION','LOGMGR_QUEUE','WAITFOR','FT_IFTS_SCHEDULER_IDLE_WAIT',
  'SP_SERVER_DIAGNOSTICS_SLEEP','QDS_PERSIST_TASK_MAIN_LOOP_SLEEP','QDS_ASYNC_QUEUE',
  'SQLTRACE_INCREMENTAL_FLUSH_SLEEP','BROKER_EVENTHANDLER','CLR_AUTO_EVENT','CLR_MANUAL_EVENT')
  AND wait_time_ms > 0
ORDER BY wait_time_ms DESC;
Operational Notes & Remediation Guidance

Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 2008).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.