Sunday, April 12, 2020

I/O Statistics by file for the current database

 Understanding SQL Server File I/O Performance with `dm_io_virtual_file_stats`

If you're managing a SQL Server database, keeping an eye on file input/output (I/O) performance is crucial for maintaining optimal speed and responsiveness. The `sys.dm_io_virtual_file_stats` dynamic management view (DMV) is an incredibly powerful tool for gaining deep insights into how your database files are behaving. It provides detailed metrics on reads, writes, I/O stalls, and more, allowing you to pinpoint potential bottlenecks.

Combined with `sys.database_files`, this DMV gives you a comprehensive overview of your database's physical and logical file performance. Below is a useful SQL query that leverages these views to present a clear picture of your database's I/O activity:

SELECT DB_NAME(DB_ID()) AS [Database Name]
 ,df.name AS [Logical Name]
 ,vfs.[file_id]
 ,df.type_desc
 ,df.physical_name AS [Physical Name]
 ,CAST(vfs.size_on_disk_bytes / 1048576.0 AS DECIMAL(10, 2)) AS [Size on Disk (MB)]
 ,vfs.num_of_reads
 ,vfs.num_of_writes
 ,vfs.io_stall_read_ms
 ,vfs.io_stall_write_ms
 ,CAST(100. * vfs.io_stall_read_ms / (vfs.io_stall_read_ms + vfs.io_stall_write_ms) AS DECIMAL(10, 1)) AS [IO Stall Reads Pct]
 ,CAST(100. * vfs.io_stall_write_ms / (vfs.io_stall_write_ms + vfs.io_stall_read_ms) AS DECIMAL(10, 1)) AS [IO Stall Writes Pct]
 ,(vfs.num_of_reads + vfs.num_of_writes) AS [Writes + Reads]
 ,CAST(vfs.num_of_bytes_read / 1048576.0 AS DECIMAL(10, 2)) AS [MB Read]
 ,CAST(vfs.num_of_bytes_written / 1048576.0 AS DECIMAL(10, 2)) AS [MB Written]
 ,CAST(100. * vfs.num_of_reads / (vfs.num_of_reads + vfs.num_of_writes) AS DECIMAL(10, 1)) AS [# Reads Pct]
 ,CAST(100. * vfs.num_of_writes / (vfs.num_of_reads + vfs.num_of_writes) AS DECIMAL(10, 1)) AS [# Write Pct]
 ,CAST(100. * vfs.num_of_bytes_read / (vfs.num_of_bytes_read + vfs.num_of_bytes_written) AS DECIMAL(10, 1)) AS [Read Bytes Pct]
 ,CAST(100. * vfs.num_of_bytes_written / (vfs.num_of_bytes_read + vfs.num_of_bytes_written) AS DECIMAL(10, 1)) AS [Written Bytes Pct]
FROM sys.dm_io_virtual_file_stats(DB_ID(), NULL) AS vfs
INNER JOIN sys.database_files AS df WITH (NOLOCK) ON vfs.[file_id] = df.[file_id]
OPTION (RECOMPILE);
  

Key Metrics Explained:

  • Size on Disk (MB): The current size of the database file.
  • num_of_reads / num_of_writes: Total number of reads/writes since the last SQL Server restart or database online event.
  • io_stall_read_ms / io_stall_write_ms: The total time, in milliseconds, that users waited for reads/writes to complete. High values here can indicate I/O bottlenecks.
  • IO Stall Reads Pct / IO Stall Writes Pct: Percentage of I/O stall time attributed to reads or writes.
  • MB Read / MB Written: Total megabytes read from or written to the file.
  • # Reads Pct / # Write Pct: Percentage of total I/O operations (reads + writes) that are reads or writes.
  • Read Bytes Pct / Written Bytes Pct: Percentage of total I/O bytes transferred that are reads or writes.

For more detailed information on these system views, you can refer to the official Microsoft documentation:

By regularly monitoring these metrics, you can proactively identify and address I/O performance issues, ensuring your SQL Server databases run smoothly and efficiently.

Popular Posts