获取SQL Server CPU使用数据的脚本
2009-09-10 13:33
453 查看
Getting CPU Utilization Data from SQL Server
from: http://sqlblog.com/blogs/ben_nevarez/archive/2009/07/26/getting-cpu-utilization-data-from-sql-server.aspx
When I installed the SQL Server Performance Dashboard for the first time, perhaps a couple of years ago, the first thing that caught my attention was the nice graph in the main screen showing the system CPU utilization. Then I immediately wanted to know where this report was getting this CPU information from. A few minutes later I found the undocumented sys.dm_os_ring_buffers DMV.
Even when the sys.dm_os_ring_buffers DMV returns information about memory management, error handling data, and some other data about the state of the server, it is its scheduler monitor health records which provide the system CPU utilization information.
To get this data the ring_buffer_type field needs to be used to filter on the RING_BUFFER_SCHEDULER_MONITOR value as shown in the following query, which works on both SQL Server 2005 and SQL Server 2008.
select timestamp, convert(xml, record) as record
from sys.dm_os_ring_buffers
where ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
and record like '%<SystemHealth>%'
The information is stored in XML format and some sample output is shown here
One record is stored every minute up to a maximum of 256 records (if the instance has been running long enough, that is, more than 4 hours). Clicking on any of the links will take you to the XML editor and will show an entry similar to this
<Record id="1434" type="RING_BUFFER_SCHEDULER_MONITOR" time="244409400">
<SchedulerMonitorEvent>
<SystemHealth>
<ProcessUtilization>44</ProcessUtilization>
<SystemIdle>50</SystemIdle>
<UserModeTime>337187500</UserModeTime>
<KernelModeTime>197812500</KernelModeTime>
<PageFaults>64252</PageFaults>
<WorkingSetDelta>21770240</WorkingSetDelta>
<MemoryUtilization>100</MemoryUtilization>
</SystemHealth>
</SchedulerMonitorEvent>
</Record>
The fields that show the CPU information are ProcessUtilization, which is the amount of CPU used by SQL Server, and SystemIdle, which is amount of idle CPU. The remaining percentage is the amount of CPU used by the other processes running on the server.
This is the query used by the Performance Dashboard using the XQuery value() method to get the required information from the XML record
declare @ts_now bigint
select @ts_now = cpu_ticks / convert(float, cpu_ticks_in_ms) from sys.dm_os_sys_info
select record_id,
dateadd(ms, -1 * (@ts_now - [timestamp]), GetDate()) as EventTime,
SQLProcessUtilization,
SystemIdle,
100 - SystemIdle - SQLProcessUtilization as OtherProcessUtilization
from (
select
record.value('(./Record/@id)[1]', 'int') as record_id,
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') as SystemIdle,
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') as SQLProcessUtilization,
timestamp
from (
select timestamp, convert(xml, record) as record
from sys.dm_os_ring_buffers
where ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
and record like '%<SystemHealth>%') as x
) as y
order by record_id desc
Note that this query also uses the sys.dm_os_sys_info DMV. While testing on SQL Server 2008 I found that the cpu_ticks_in_ms column is no longer available on this DMV. So, if you want to run this code in this version of SQL Server just replace the second line of the code with this line
select @ts_now = cpu_ticks / (cpu_ticks/ms_ticks) from sys.dm_os_sys_info;
Running the query will show something similar to this output
Finally, you could use this query (or the msdb.MS_PerfDashboard.usp_Main_GetCPUHistory stored procedure installed by the Performance Dashboard) to collect this CPU information periodically, for example, as a job running every 15 minutes.
Keep in mind that the Performance Dashboard is not needed in SQL Server 2008 as this version includes the new Data Collector, a feature that allows you to store the performance and diagnostics historic information of your SQL Server instances.
相关文章推荐
- MSSQL中,使用T-SQL脚本获取上周一到上周五的数据
- 使用SQL Server Management Studio 2008 将数据库里的数据导成脚本
- 使用SqlBulkCopy导入数据至MS SQL Server
- 使用SQL Server Analysis Services数据挖掘的关联规则实现商品推荐功能(一)
- 使用Oracle SQL Developer提示无法从套接字获取更多数据如何解决
- Biztalk中使用SQL适配器获取数据并用web服务发布的例子
- SQL Server BI Step by Step --- 使用SSIS进行简单的数据导入导出
- 使用Sql Server Management Studio 2008将数据导出到Sql文件中
- django cpu监控之九-----使用psutil获取CPU数据
- Sql Server cursor 的使用处理重复数据 动态拼接 SQL语句
- 使用T-SQL实现数据导出/导入(SQLSERVER—>SQLSERVER)
- Oracle数据库案例整理-执行Oracle脚本失败或异常-SQL命令中字符串参数使用双引号导致更新数据库表信息失败
- SQL Server BI Step by Step 2--- 使用SSIS进行简单的数据导入导出
- [转]在Microsoft SQL Server 2000 数据仓库中使用分区
- [SQL] 获取 Microsoft SQL Server 2008 的数据表结构
- 使用SqlDataReader获取只读数据
- 使用 SQL 语句从数据库一个表中随机获取一些数据
- linux系统使用python获取cpu信息脚本分享
- 使用SqlBulkCopy导入数据至MS SQL Server
- 如何使用 ADO Stream 对象访问和修改 SQL Server BLOB 数据