{"id":82009,"date":"2007-04-06T20:32:00","date_gmt":"2007-04-06T20:32:00","guid":{"rendered":"https:\/\/www.webstaging.red-gate.com\/simple-talk\/?p=73143"},"modified":"2018-12-12T13:43:42","modified_gmt":"2018-12-12T13:43:42","slug":"dynamic-management-function-query-to-view-disk-io-pressure","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/dynamic-management-function-query-to-view-disk-io-pressure\/","title":{"rendered":"Dynamic management function query to view disk I\/O pressure"},"content":{"rendered":"<p><P>&nbsp;<\/P> <P>The sys.dm_io_virtual_file_stats dynamic management function returns I\/O statistics for data and log files [MDF and LDF file], with two parameters, one for database_id and another for the file_id. This function will help you to identify I\/O file level.  <P>I was led here when doing a search because we were getting enormous amounts of disk queuing the other day during a round of testing, so I started hunting around, looking for some information on some query to get this information from SQL Server, and I found this article from Itzik Ben-Gan entitled <A href=\"http:\/\/www.sqlmag.com\/Articles\/Print.cfm?ArticleID=93327\" target=\"_blank\">Query DMFs to Analyze Performance Stats<\/A>.&nbsp; His stuff is always good, if some of it blows right past my non-mathematical brain quite often (as does a bit of that article) and it pointed me to some queries that are useful, but I also wanted to be able to take a &#8220;point in time&#8221; reading that I could compare with.&nbsp; This information, coupled with stuff from BOL <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms190326.aspx\" target=\"_blank\">here<\/A>&nbsp;got me going.&nbsp;  <P>It returns its readings since when the SQL Servers was started. I was wanting to use it to determine hotspots in the filesystem during some very heavy processing times, so I built a little snippet of code to create a table (based on the value of a variable) so you compare some segment of time.  <P>declare @resetbaseLine bit<BR>set @resetbaseLine = 0 <\/FONT> <P>set nocount on<BR>if @resetBaseLine = 1 or object_id(&#8216;tempdb..#baseline&#8217;) is null<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if object_id(&#8216;tempdb..#baseline&#8217;) is not null<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; drop table #baseline<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &#8211;initialize the baseline table<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; select db_name(mf.database_id) as databaseName, mf.physical_name, <BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; num_of_reads, num_of_bytes_read, io_stall_read_ms, num_of_writes, <BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; num_of_bytes_written, io_stall_write_ms, io_stall,size_on_disk_bytes,<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; getdate() as baselineDate<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;into #baseline<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; from sys.dm_io_virtual_file_stats(null,null) as divfs<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; join sys.master_files as mf<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; on mf.database_id = divfs.database_id<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; and mf.file_id = divfs.file_id<BR>&nbsp;&nbsp;&nbsp; end <\/FONT> <P>&#8211;output the values, subtracting the baseline from the &#8220;currentLine&#8221; values \ud83d\ude42<BR>select currentLine.databaseName, left(currentLine.physical_name,1) as drive<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,currentLine.physical_name as file_name<BR><\/FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,currentLine.io_stall &#8211; #baseline.io_stall as io_stall<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,currentLine.io_stall_read_ms &#8211; #baseline.io_stall_read_ms as io_stall_read_ms<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,currentLine.io_stall_write_ms &#8211; #baseline.io_stall_write_ms as io_stall_write_ms<BR><\/FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,currentLine.num_of_reads &#8211; #baseline.num_of_reads as num_of_reads<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,currentLine.num_of_bytes_read &#8211; #baseline.num_of_bytes_read as num_of_bytes_read<BR><\/FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,currentLine.num_of_writes &#8211; #baseline.num_of_writes as num_of_writes<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,currentLine.num_of_bytes_written &#8211; #baseline.num_of_bytes_written as num_of_bytes_written<BR><\/FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,currentLine.size_on_disk_bytes &#8211; #baseline.size_on_disk_bytes as size_change_on_disk_in_bytes<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , dateDiff(second,#baseline.baselineDate,currentLine.baselineDate) as seconds_since_baseline<BR>from (<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; select db_name(mf.database_id) as databaseName, mf.physical_name, <BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; num_of_reads, num_of_bytes_read, io_stall_read_ms, num_of_writes, <BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; num_of_bytes_written, io_stall_write_ms, io_stall,size_on_disk_bytes,<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; getdate() as baselineDate<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; from sys.dm_io_virtual_file_stats(null,null) as divfs<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; join sys.master_files as mf<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; on mf.database_id = divfs.database_id<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; and mf.file_id = divfs.file_id) as currentLine<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; join #baseline<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; on #baseLine.databaseName = currentLine.databaseName<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; and #baseLine.physical_name = currentLine.physical_name<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; order by currentLine.io_stall &#8211; #baseline.io_stall desc<\/FONT><\/P> <P>The query returns the following, offset from when the baseline was taken. <\/P> <UL> <LI><STRONG>databaseName<\/STRONG> &#8211; name of the database the file is located in  <LI><STRONG>drive<\/STRONG> &#8211; the drive the file is located on (gives you the ability to sort\/group by it)  <LI><STRONG>file_name<\/STRONG> &#8211; the name of the file from that part of the database is located on  <LI><STRONG>io_stall <\/STRONG>&#8211; total time in milliseconds that users waited for I\/O activity to the file  <LI><STRONG>io_stall_read_ms <\/STRONG>&#8211; total time in milliseconds that users waited for read to the file  <LI><STRONG>io_stall_write_ms<\/STRONG> &#8211; total time in milliseconds that users waited for writes to the file  <LI><STRONG>num_of_reads <\/STRONG>&#8211; total number of reads issued to the file  <LI><STRONG>num_of_bytes_read<\/STRONG> &#8211; total number of bytes read from the file  <LI><STRONG>num_of_writes <\/STRONG>&#8211; total number of&nbsp;writes issued to the file <\/LI> <LI><STRONG>num_of_bytes_written <\/STRONG>&#8211; total number of bytes written to the file  <LI><STRONG>size_change_on_disk_in_bytes <\/STRONG>&#8211; change in size of the file  <LI><STRONG>seconds_since_baseline <\/STRONG>&#8211; seconds since the baseline readings were taken <\/LI><\/UL> <P>The only real downside in this query is that it will show you almost too much information about your hotspots and you may have to go fix things!<\/P> <P><EM>Edit: added num_of_writes and fixed the num_of_bytes_written<\/EM><\/P> <P>Crossposted to: <A title=\"http:\/\/sqlblog.com\/blogs\/louis_davidson\/default.aspx\" href=\"http:\/\/sqlblog.com\/blogs\/louis_davidson\/default.aspx\">drsql.spaces.live.com<\/A><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; The sys.dm_io_virtual_file_stats dynamic management function returns I\/O statistics for data and log files [MDF and LDF file], with two parameters, one for database_id and another for the file_id. This function will help you to identify I\/O file level. I was led here when doing a search because we were getting enormous amounts of disk&#8230;&hellip;<\/p>\n","protected":false},"author":56085,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[19684],"class_list":["post-82009","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/56085"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=82009"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82009\/revisions"}],"predecessor-version":[{"id":82391,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82009\/revisions\/82391"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=82009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=82009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=82009"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=82009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}