dm_os_performance_counters说明
该视图用于查看数据库的性能指标,但是不同的指标类型(cntr_type)计算方法有所不同。
大概有以下不同类型:
select object_name,counter_name,instance_name,cntr_value,
case cntr_type
when 65792 then '所见即所得,无需计算'
when 65536 then '所见即所得,无需计算'
when 272696576 then '累计值'
when 1073874176 then '需要除以同名 Base对应的值'
when 537003264 then '需要除以同名 Base对应的值'
end as counter_comments
from sys.dm_os_performance_counters
where cntr_type not in (1073939712);
1、PERF_COUNTER_LARGE_RAWCOUNT:65792:
PERF_COUNTER_LARGE_RAWCOUNT 计数器类型的 cntr_types 列值为 65792。这些计数器显示最后观察到的值,而不是平均值。它通常用于监视对象计数 这意味着,如果计数器类型为 65792,那么在查询视图时在 counter_value 列中得到的值就是该计数器的当前值,不需要额外计算。也就是"所见即所得"。
如:查看Page life expectancy
SELECT *
FROM sys.dm_os_performance_counters
WHERE counter_name = 'Page Life expectancy'
AND object_name LIKE '%buffer manager%';
该类型的计数器类型有:*General Statistics User connections, Buffer Manager Page life expectancy and Database pages, Databases – Data and Log file size (KB), Log file used size (KB), Percent Log used, Memory Manager – Free Memory (KB),*等等。
查看SQL:
select distinct object_name,counter_name from sys.dm_os_performance_counters where cntr_type=65792
2、PERF_LARGE_RAW_BASE:1073939712
PERF_LARGE_RAW_BASE 计数器类型的 cntr_types 列值为 1073939712。这些计数器收集最后观察到的值,该计数器值用作进一步计算的分母
。此类型的计数器仅用于计算通过视图可用的其他计数器,属于此计数器类型的所有计数器的名称中都包含单词 base,因此清楚地表明这不是提供有用信息的计数器,它只是进一步计算的基值。
通常用于计算命中率相关信息,如:
SELECT *
FROM sys.dm_os_performance_counters
WHERE counter_name = 'Buffer cache hit ratio base'
Buffer Cache Hit Ratio % = 100 * Buffer Cache Hit Ratio /Buffer Cache Hit Ratio Base
= 100 * 2,135 / 3,573
= 59.75%
该类型的计数器类型有:Buffer Cache Hit Ratio Base, Log Cache Hit Ratio Base, Average Latch Wait Time Base, Cache Hit Ratio Base, CPU usage % base, and more
查看SQL:
select distinct object_name,counter_name from sys.dm_os_performance_counters where cntr_type=1073939712
3、PERF_AVERAGE_BULK :1073874176
PERF_AVERAGE_BULK 计数器类型的 cntr_types 列值为 1073874176。 cntr_value 列值是累积的。要计算计数器的当前值,必须监视 PERF_AVERAGE_BULK 及其对应的 PERF_LARGE_RAW_BASE 计数器(1073939712),同时取两个样本,并使用这些值进行计算。
如:计算平均等待时间
SELECT *
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE '%Average Wait Time%'
AND instance_name = 'database'
时间点 T1:
时间点T2:
即:Average Wait Time (ms) = (53736 ms -52939 ms)/(23-18) = 797 ms / 5 = 159.4 ms
该类型的计数器类型有:*Average Wait Time (ms), Average Latch Wait Time (ms), Update conflict ratio, Avg. Length of Batched Writes, Avg. Time to Write Batch (ms), Avg. Time Between Batches (ms)*等等。
4、PERF_LARGE_RAW_FRACTION :537003264
PERF_LARGE_RAW_FRACTION 计数器类型的 cntr_types 列值为 537003264。这些计数器用来计算命中率,即两个值之间的分数 - PERF_LARGE_RAW_FRACTION 计数器及其对应的 PERF_LARGE_RAW_BASE 计数器值的比值。
如:查看缓存命中率
SELECT *
FROM sys.dm_os_performance_counters
WHERE counter_name like 'Buffer cache hit ratio%'
Buffer Cache Hit Ratio % = 100 * Buffer Cache Hit Ratio /Buffer Cache Hit Ratio Base = 100* 564/564 = 100.
该类型的计数器类型有:Buffer Cache Hit Ratio, Log Cache Hit Ratio, Worktables From Cache Ratio, Cache Hit Ratio, CPU usage %, and Rem Req Cache Hit Ratio等。
5、PERF_COUNTER_BULK_COUNT:272696576
PERF_COUNTER_BULK_COUNT 计数器类型的 cntr_types 列值为 272696576。这些计数器显示的值是累积的,它是自上次 SQL Server 实例重新启动以来的累积值,因此需要对其实际值进行采样,与 PERF_AVERAGE_BULK 计数器类型相同。
但是,了解采样周期有多长很重要。否则,将无法计算每秒的值。通常使用 5 分钟的时间段,要计算每秒速率,请计算两个样本值之间的差异,然后将其除以样本之间的秒数。
如:计算Page lookups/秒
DECLARE @PageLookups1 BIGINT;
SELECT @PageLookups1 = cntr_value
FROM sys.dm_os_performance_counters
WHERE counter_name = 'Page lookups/sec';
WAITFOR DELAY '00:00:10';
SELECT (cntr_value - @PageLookups1) / 10 AS 'Page lookups/sec'
FROM sys.dm_os_performance_counters
WHERE counter_name = 'Page lookups/sec';
该类型的计数器类型有:Page lookups/sec, Free list stalls/sec, Lazy writes/sec, Page reads/sec, Page writes/sec, Logins/sec等等。