从SQL Server获取当前使用的OS CPU百分比

时间:2015-01-15 11:54:51

标签: sql sql-server

我搜索过但找不到它。是否有一个查询将使当前操作系统(windows)使用CPU百分比?就像我们都可以在任务管理器中看到的那样。

感谢。

1 个答案:

答案 0 :(得分:1)

取自here

    SELECT TOP 1
        record_time,
        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,
            record_time
        FROM (
            select 
                dateadd (ms, r.[timestamp] - sys.ms_ticks, getdate()) as record_time,  
                cast(r.record as xml) record  
            from sys.dm_os_ring_buffers r  
            cross join sys.dm_os_sys_info sys  
            where   
                ring_buffer_type='RING_BUFFER_SCHEDULER_MONITOR' 
                AND 
                record LIKE '%<SystemHealth>%'
            ) AS x
        ) AS y 

另外,请注意 - 正如Shanky所指出的那样 - 不支持sys.dm_os_ring_buffers,并且不保证将来的兼容性。这意味着它可能会在将来的SQL Server升级中发生变化。有关详细信息,请参阅here