Tuesday, October 25, 2011

Disk Latency SQL Script

Below is a script that I use to analyze disk latency and read/write ratios for my database server disks. The script is a combination of a couple of scripts from two different sources.

I'm borrowing the latency code from Listing 7-7 from Professional SQL Server 2008 Internals and Troubleshooting by Wrox Press. I'm borrowing the read/write ratios from Listing 2.1 from SQL Server Hardware from Redgate Books.

I added in some additional columns for drive letters, file extensions and disk latency thresholds.
I find the output very useful in determining if my storage system is keeping up with my SQL Server.

SELECT DISTINCT DB_NAME(Database_id) AS DB, FILE_ID,
io_stall_read_ms / num_of_reads as 'AVG read Transfer/ms',
CASE WHEN b.groupid = 1 THEN
CASE WHEN io_stall_read_ms / num_of_reads < 10 THEN 'Target'
          WHEN io_stall_read_ms / num_of_reads between 10 and 20 THEN 'Acceptable'
          WHEN io_stall_read_ms / num_of_reads > 20 THEN 'Unacceptable'
          ELSE 'N/A' END
ELSE 'N/A' END AS ReadRange,
CAST(100. * num_of_reads / ( num_of_reads + num_of_writes ) AS DECIMAL(10,1)) AS [# Reads Pct] ,
CAST(100. * num_of_bytes_read / ( num_of_bytes_read + num_of_bytes_written ) AS DECIMAL(10,1)) AS [Read Bytes Pct] ,
io_stall_write_ms / num_of_writes AS 'AVG write Transfer/ms',

CASE WHEN b.groupid = 0 THEN
CASE WHEN io_stall_write_ms / num_of_writes < 10 THEN 'Target'
          WHEN io_stall_write_ms / num_of_writes between 10 and 20 THEN 'Acceptable'
          WHEN io_stall_write_ms / num_of_writes > 20 THEN 'Unacceptable'
          ELSE 'N/A' END
ELSE 'N/A' END AS WriteRange,
CAST(100. * num_of_writes / ( num_of_reads + num_of_writes ) AS DECIMAL(10,1)) AS [# Write Pct] ,
CAST(100. * num_of_bytes_written / ( num_of_bytes_read + num_of_bytes_written ) AS DECIMAL(10,1)) AS [Written Bytes Pct],
LEFT(b.filename,3) AS DriveLetter, RIGHT(b.filename,3) AS FileExtension

FROM sys.dm_io_virtual_file_stats (-1,-1) a
INNER JOIN sys.sysaltfiles b on a.Database_id = b.dbid and a.file_id = b.fileid
WHERE num_of_reads > 0
AND num_of_writes > 0

No comments:

Post a Comment