使用sys.dm_io_virtual_file_stats了解你的数据库IO

2024-01-13 16:18

本文主要是介绍使用sys.dm_io_virtual_file_stats了解你的数据库IO,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

因为sys.dm_io_virtual_file_stats 返回数据和日志文件的 I/O 统计信息,包括对文件发出的读取/写入次数以及总字节数,所以这个函数既可以看到IOPS也可以看到吞吐量,还可以计算出单个IO的大小。另外也可以看到IO的等待时间。能够帮助我们很好的了解数据库的IO状况。
配合Perfmon一起使用可以很快的找到IO瓶颈。
下面的文章来自微软的以为高级工程师:
http://blogs.msdn.com/b/dpless/archive/2010/12/01/leveraging-sys-dm-io-virtual-file-stats.aspx

One of the DMVs I try to utilize on any engagement where customers are complaining about disk issues is the sys.dm_io_virtual_file_stats DMV where you can look at the IO stalls for both reads and writes. The sys.dm_io_virtual_file_stats DMV will show an IO Stall when any wait occurs to access a physical data file. IO Stalls are recorded at the file level and you can also obtain the IO Stalls at the database level directly out of the DMV.

By getting this information it is very easy to ORDER BY io_stall_read_ms, io_stall_write_ms, or by io_stall which is an accumulation of reads and writes.

One addition step I have made in the script below is mapping to the sys.master_files catalog view and using the substring function to get the physical disk drive letter. You will now be able to see IO Stall activity at the file, database, and the drive letter. You can then use Reporting Services or simply use Excel to get a quick view of which of these is absorbing most of the IO Stall impact.

If you use Excel 2007, one of the interesting strategies is to use the Chart Advisor from Live Labs.
http://www.officelabs.com/projects/chartadvisor/Pages/default.aspx

This analysis can help make decisions around table partitioning and potentially file and index placement. Of course, this will all depend on the customer's SAN and other constraints.

Note: Mount points will make getting the drive letter less effective. If you are using mount points then just ignore the drive letter column.

SELECT a.io_stall, a.io_stall_read_ms, a.io_stall_write_ms, a.num_of_reads,
a.num_of_writes,
--a.sample_ms, a.num_of_bytes_read, a.num_of_bytes_written, a.io_stall_write_ms,
( ( a.size_on_disk_bytes / 1024 ) / 1024.0 ) AS size_on_disk_mb,
db_name(a.database_id) AS dbname,
b.name, a.file_id,
db_file_type = CASE
WHEN a.file_id = 2 THEN
'Log'
ELSE
'Data'
END,
UPPER(SUBSTRING(b.physical_name, 1, 2))AS disk_location
FROM sys.dm_io_virtual_file_stats (NULL, NULL) a
JOIN sys.master_files bON a.file_id = b.file_id
AND a.database_id = b.database_id
ORDER BY a.io_stall DESC

For those looking at disk issues, I have pasted the general guidance on the avg. reads/sec and avg. writes/sec values for perfmon. By using the script above and the guidance here on perfmon, you should be able to take the next steps in addressing disk performance issues with your customers.

I/O Bottlenecks

SQL Server performance depends heavily on the I/O subsystem. Unless your database fits into physical memory, SQL Server constantly brings database pages in and out of the buffer pool. This generates substantial I/O traffic. Similarly, the log records need to be flushed to the disk before a transaction can be declared committed. And finally, SQL Server uses TempDB for various purposes such as to store intermediate results, to sort, to keep row versions and so on. So a good I/O subsystem is critical to the performance of SQL Server.

Access to log files is sequential except when a transaction needs to be rolled back while access to data files, including TempDB, is randomly accessed. So as a general rule, you should have log files on a separate physical disk than data files for better performance. The focus of this paper is not how to configure your I/O devices but to describe ways to identify if you have I/O bottleneck. Once an I/O bottleneck is identified, you may need to reconfigure your I/O subsystem.
If you have a slow I/O subsystem, your users may experience performance problems such as slow response times, and tasks that abort due to timeouts.
You can use the following performance counters to identify I/O bottlenecks. Note, these AVG values tend to be skewed (to the low side) if you have an infrequent collection interval. For example, it is hard to tell the nature of an I/O spike with 60-second snapshots. Also, you should not rely on one counter to determine a bottleneck; look for multiple counters to cross check the validity of your findings.

  • Physical Disk Object: Avg. Disk Queue Length represents the average number of physical read and write requests that were queued on the selected physical disk during the sampling period. If your I/O system is overloaded, more read/write operations will be waiting. If your disk queue length frequently exceeds a value of 2 during peak usage of SQL Server, then you might have an I/O bottleneck.
  • Avg. Disk Sec/Read is the average time, in seconds, of a read of data from the disk. Any number:
    • Less than 10 ms - very good
    • Between 10 - 20 ms - okay
    • Between 20 - 50 ms - slow, needs attention
    • Greater than 50 ms - Serious I/O bottleneck
  • Avg. Disk Sec/Write is the average time, in seconds, of a write of data to the disk. Please refer to the guideline in the previous bullet.
  • Physical Disk: %Disk Time is the percentage of elapsed time that the selected disk drive was busy servicing read or write requests. A general guideline is that if this value is greater than 50 percent, it represents an I/O bottleneck.
  • Avg. Disk Reads/Sec is the rate of read operations on the disk. You need to make sure that this number is less than 85 percent of the disk capacity. The disk access time increases exponentially beyond 85 percent capacity.
  • Avg. Disk Writes/Sec is the rate of write operations on the disk. Make sure that this number is less than 85 percent of the disk capacity. The disk access time increases exponentially beyond 85 percent capacity.

When using above counters, you may need to adjust the values for RAID configurations using the following formulas.

  • Raid 0 -- I/Os per disk = (reads + writes) / number of disks
  • Raid 1 -- I/Os per disk = [reads + (2 * writes)] / 2
  • Raid 5 -- I/Os per disk = [reads + (4 * writes)] / number of disks
  • Raid 10 -- I/Os per disk = [reads + (2 * writes)] / number of disks

For example, you have a RAID-1 system with two physical disks with the following values of the counters.

  • Disk Reads/sec -  80
  • Disk Writes/sec - 70
  • Avg. Disk Queue Length - 5

In that case, you are encountering (80 + (2 * 70))/2 = 110 I/Os per disk and your disk queue length = 5/2 = 2.5 which indicates a border line I/O bottleneck."

另外可以利用下面的脚本定期IO信息做比对分析:

SET NOCOUNTON

DECLARE @IOStatsTABLE (
[database_id][smallint]NOT NULL,
[file_id][smallint]NOT NULL,
[num_of_reads][bigint]NOT NULL,
[num_of_bytes_read][bigint]NOT NULL,
[io_stall_read_ms][bigint]NOT NULL,
[num_of_writes][bigint]NOT NULL,
[num_of_bytes_written][bigint]NOT NULL,
[io_stall_write_ms][bigint]NOT NULL)
INSERTINTO @IOStats
SELECT database_id,

                vio
.file_id,
                num_of_reads
,
                num_of_bytes_read
,
                io_stall_read_ms
,
                num_of_writes
,
                num_of_bytes_written
,
                io_stall_write_ms
FROM sys.dm_io_virtual_file_stats(NULL,NULL) vio
DECLARE @StartTime datetime, @DurationInSecsint

SET @StartTime= GETDATE()
WAITFOR DELAY'00:05:00'
SET @DurationInSecs= DATEDIFF(ss, @startTime, GETDATE())
SELECT DB_NAME(vio.database_id)AS [Database],
        mf
.nameAS [Logical name],
        mf
.type_descAS [Type],
(vio.io_stall_read_ms- old.io_stall_read_ms)/ CASE(vio.num_of_reads-old.num_of_reads)WHEN 0THEN 1ELSE vio.num_of_reads-old.num_of_readsEND AS[Ave read speed(ms)],
        vio
.num_of_reads- old.num_of_readsAS [Noof reads over period],
CONVERT(DEC(14,2),(vio.num_of_reads- old.num_of_reads)/ (@DurationInSecs *1.00))AS [Noof reads/sec],
CONVERT(DEC(14,2),(vio.num_of_bytes_read- old.num_of_bytes_read)/ 1048576.0)AS [Tot MBread over period],
CONVERT(DEC(14,2),((vio.num_of_bytes_read- old.num_of_bytes_read)/ 1048576.0)/ @DurationInSecs)AS [Tot MBread/sec],
(vio.num_of_bytes_read- old.num_of_bytes_read)/ CASE(vio.num_of_reads-old.num_of_reads)WHEN 0THEN 1ELSE vio.num_of_reads-old.num_of_readsEND AS[Ave read size(bytes)],
(vio.io_stall_write_ms- old.io_stall_write_ms)/ CASE(vio.num_of_writes-old.num_of_writes)WHEN 0THEN 1ELSE vio.num_of_writes-old.num_of_writesEND AS[Ave write speed (ms)],
        vio
.num_of_writes- old.num_of_writesAS [Noof writes over period],
CONVERT(DEC(14,2),(vio.num_of_writes- old.num_of_writes)/ (@DurationInSecs *1.00))AS [Noof writes/sec],
CONVERT(DEC(14,2),(vio.num_of_bytes_written- old.num_of_bytes_written)/1048576.0)AS [Tot MB writtenover period],
CONVERT(DEC(14,2),((vio.num_of_bytes_written- old.num_of_bytes_written)/1048576.0)/ @DurationInSecs)AS [Tot MB written/sec],
(vio.num_of_bytes_written-old.num_of_bytes_written)/ CASE(vio.num_of_writes-old.num_of_writes)WHEN 0THEN 1ELSE vio.num_of_writes-old.num_of_writesEND AS[Ave write size (bytes)],
        mf
.physical_nameAS [Physicalfile name],
        size_on_disk_bytes
/1048576AS [File sizeon disk(MB)]
FROM sys.dm_io_virtual_file_stats(NULL,NULL) vio,
        sys
.master_files mf,
        @IOStats old
WHERE mf.database_id= vio.database_idAND
        mf
.file_id= vio.file_idAND
        old
.database_id= vio.database_idAND
        old
.file_id= vio.file_idAND
((vio.num_of_bytes_read- old.num_of_bytes_read)+ (vio.num_of_bytes_written- old.num_of_bytes_written))> 0
ORDERBY ((vio.num_of_bytes_read- old.num_of_bytes_read)+ (vio.num_of_bytes_written- old.num_of_bytes_written))DESC
GO

这篇关于使用sys.dm_io_virtual_file_stats了解你的数据库IO的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/602055

相关文章

使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)

《使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)》在现代软件开发中,处理JSON数据是一项非常常见的任务,无论是从API接口获取数据,还是将数据存储为JSON格式,解析... 目录1. 背景介绍1.1 jsON简介1.2 实际案例2. 准备工作2.1 环境搭建2.1.1 添加

如何使用celery进行异步处理和定时任务(django)

《如何使用celery进行异步处理和定时任务(django)》文章介绍了Celery的基本概念、安装方法、如何使用Celery进行异步任务处理以及如何设置定时任务,通过Celery,可以在Web应用中... 目录一、celery的作用二、安装celery三、使用celery 异步执行任务四、使用celery

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3

详谈redis跟数据库的数据同步问题

《详谈redis跟数据库的数据同步问题》文章讨论了在Redis和数据库数据一致性问题上的解决方案,主要比较了先更新Redis缓存再更新数据库和先更新数据库再更新Redis缓存两种方案,文章指出,删除R... 目录一、Redis 数据库数据一致性的解决方案1.1、更新Redis缓存、删除Redis缓存的区别二

oracle数据库索引失效的问题及解决

《oracle数据库索引失效的问题及解决》本文总结了在Oracle数据库中索引失效的一些常见场景,包括使用isnull、isnotnull、!=、、、函数处理、like前置%查询以及范围索引和等值索引... 目录oracle数据库索引失效问题场景环境索引失效情况及验证结论一结论二结论三结论四结论五总结ora

Jsoncpp的安装与使用方式

《Jsoncpp的安装与使用方式》JsonCpp是一个用于解析和生成JSON数据的C++库,它支持解析JSON文件或字符串到C++对象,以及将C++对象序列化回JSON格式,安装JsonCpp可以通过... 目录安装jsoncppJsoncpp的使用Value类构造函数检测保存的数据类型提取数据对json数

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

springboot整合 xxl-job及使用步骤

《springboot整合xxl-job及使用步骤》XXL-JOB是一个分布式任务调度平台,用于解决分布式系统中的任务调度和管理问题,文章详细介绍了XXL-JOB的架构,包括调度中心、执行器和Web... 目录一、xxl-job是什么二、使用步骤1. 下载并运行管理端代码2. 访问管理页面,确认是否启动成功

使用Nginx来共享文件的详细教程

《使用Nginx来共享文件的详细教程》有时我们想共享电脑上的某些文件,一个比较方便的做法是,开一个HTTP服务,指向文件所在的目录,这次我们用nginx来实现这个需求,本文将通过代码示例一步步教你使用... 在本教程中,我们将向您展示如何使用开源 Web 服务器 Nginx 设置文件共享服务器步骤 0 —