Last active
February 18, 2024 02:06
-
-
Save petesql/6dee542d010e9cda0ecd15e43b35c8be to your computer and use it in GitHub Desktop.
SQL Server Get CPU Utilization History
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Get CPU Utilization History for last 4hrs (one minute intervals) | |
DECLARE @ts_now bigint = (SELECT ms_ticks FROM sys.dm_os_sys_info WITH (NOLOCK)); | |
SELECT | |
SQLProcessUtilization, | |
SystemIdle As SystemIdleProcess, | |
100 - SystemIdle - SQLProcessUtilization AS OtherProcessCPUUtilization, | |
DATEADD(ms, -1 * (@ts_now - timestamp), GETDATE()) AS EventTime | |
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 WITH (NOLOCK) | |
WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' | |
AND record LIKE N'%<SystemHealth>%') AS x) AS y | |
ORDER BY record_id DESC OPTION (RECOMPILE); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment