Tuesday, October 9, 2012

Script to collect the wait stats:

-- Isolate top waits for server instance since last restart or statistics clearWITH Waits AS(SELECT wait_typewait_time_ms 1000. AS wait_time_s,100. wait_time_ms SUM(wait_time_msOVER() AS pct,ROW_NUMBER() OVER(ORDER BY wait_time_ms DESCAS rnFROM sys.dm_os_wait_statsWHERE wait_type NOT IN('CLR_SEMAPHORE','LAZYWRITER_SLEEP','RESOURCE_QUEUE','SLEEP_TASK','SLEEP_SYSTEMTASK','SQLTRACE_BUFFER_FLUSH','WAITFOR','LOGMGR_QUEUE','CHECKPOINT_QUEUE','REQUEST_FOR_DEADLOCK_SEARCH','XE_TIMER_EVENT','BROKER_TO_FLUSH','BROKER_TASK_STOP','CLR_MANUAL_EVENT','CLR_AUTO_EVENT','DISPATCHER_QUEUE_SEMAPHORE','FT_IFTS_SCHEDULER_IDLE_WAIT','XE_DISPATCHER_WAIT''XE_DISPATCHER_JOIN','SQLTRACE_INCREMENTAL_FLUSH_SLEEP'))SELECT W1.wait_type,CAST(W1.wait_time_s AS DECIMAL(122)) AS wait_time_s,CAST(W1.pct AS DECIMAL(122)) AS pct,CAST(SUM(W2.pctAS DECIMAL(122)) AS running_pctFROM Waits AS W1INNER JOIN Waits AS W2ON W2.rn <= W1.rnGROUP BY W1.rnW1.wait_typeW1.wait_time_sW1.pctHAVING SUM(W2.pct) - W1.pct 99 OPTION (RECOMPILE); -- percentage thresholdGO



This dynamic management view collects all the information since the time when the SQL Server services have been restarted. You can also manually clear the wait stats using the following command:
DBCC SQLPERF('sys.dm_os_wait_stats'CLEAR);

No comments: