Thursday, November 4, 2010

What’s my queries waiting for?

Blocking is a natural part of all RDBMS, not only SQL Server. However, very often the server is not able to continue processing because of other reasons such as waiting for disk IO, network IO or something else. Using this knowledge can be a key differentiator when analyzing a busy system and ease your work in finding the bottleneck.
There are several scripts out there, but this is the one I use:

SET arithabort OFF  
SET arithignore ON  
SET ansi_warnings OFF  
DBCC sqlperf ('sys.dm_os_wait_stats', 'CLEAR')
go
WAITFOR DELAY '00:00:20' -- every 10 seconds 
 
SELECT   
CASE  
    WHEN wait_type LIKE 'LCK%' THEN 'Locks'  
    WHEN wait_type LIKE 'PAGEIO%' THEN 'Page I/O Latch'  
    WHEN wait_type LIKE 'PAGELATCH%' THEN 'Page Latch (non-I/O)'  
    WHEN wait_type LIKE 'LATCH%' THEN 'Latch (non-buffer)'  
    WHEN wait_type LIKE 'IO_COMPLETION' THEN 'I/O Completion'  
    WHEN wait_type LIKE 'ASYNC_NETWORK_IO' THEN 'Network I/O (client fetch)'  
    WHEN wait_type LIKE 'CLR_%' OR wait_type LIKE 'SQLCLR%' THEN 'SQLCLR'  
    WHEN wait_type IN ('RESOURCE_SEMAPHORE', 'SOS_RESERVEDMEMBLOCKLIST') THEN 'Memory'  
    WHEN wait_type LIKE 'RESOURCE_SEMAPHORE_%' OR wait_type = 'CMEMTHREAD' THEN 'Compilation'  
    WHEN wait_type LIKE 'MSQL_XP' THEN 'XProc'  
    WHEN wait_type LIKE 'WRITELOG' THEN 'Writelog'  
    WHEN wait_type LIKE 'CXPACKET' THEN 'Parallelism Synch'  
    WHEN wait_type IN ('WAITFOR', 'LAZYWRITER_SLEEP', 'SQLTRACE_BUFFER_FLUSH', 'EXCHANGE',   
      'REQUEST_FOR_DEADLOCK_SEARCH', 'KSOURCE_WAKEUP', 'BROKER_TRANSMITTER', 'BROKER_EVENTHANDLER', 'BROKER_RECEIVE_WAITFOR',   
      'BROKER_TASK_STOP', 'ONDEMAND_TASK_QUEUE', 'CHKPT', 'DBMIRROR_WORKER_QUEUE', 'DBMIRRORING_CMD',  
    'SLEEP_TASK', 'CLR_MANUAL_EVENT', 'CLR_AUTO_EVENT'  
    )
      THEN 'IGNORABLE'  
    ELSE wait_type   
  END   
    ,waiting_tasks_count  
    ,wait_time_ms AS 'Total Waittime'  
    , wait_time_ms/waiting_tasks_count AS 'Average waits in ms'  
    ,max_wait_time_ms AS 'Maximum Waittime'  
    ,signal_wait_time_ms  
 
FROM sys.dm_os_wait_stats  
WHERE wait_type NOT IN ('WAITFOR', 'LAZYWRITER_SLEEP', 'SQLTRACE_BUFFER_FLUSH', 'EXCHANGE',   
      'REQUEST_FOR_DEADLOCK_SEARCH', 'KSOURCE_WAKEUP', 'BROKER_TRANSMITTER', 'BROKER_EVENTHANDLER', 'BROKER_RECEIVE_WAITFOR',   
      'BROKER_TASK_STOP', 'ONDEMAND_TASK_QUEUE', 'CHKPT', 'DBMIRROR_WORKER_QUEUE', 'DBMIRRORING_CMD',  
    'SLEEP_TASK', 'CLR_MANUAL_EVENT', 'CLR_AUTO_EVENT'  
    )  
ORDER BY wait_time_ms DESC  
 
SET arithabort ON  
SET arithignore OFF  
SET ansi_warnings ON 

No comments:

Post a Comment