declare @ts_now bigint
select @ts_now = cpu_ticks / convert(float, cpu_ticks_in_ms)
from sys.dm_os_sys_info with(nolock)
select top 1 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 with(nolock)
where ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
and record like '%<SystemHealth>%') as x
) as y
order by record_id desc
/*check current top 5 cpu query*/
select top 5
top_current_cpu=r.cpu_time
,r.total_elapsed_time
,r.logical_reads
,r.session_id
,s.login_name
,s.[host_name]
,s.status
,r.command
,dbname=db_name(r.database_id)
,s.[program_name]
,running_query_text= (select top 1 [text]
from master.sys.dm_exec_sql_text(r.sql_handle) )
,s.login_time
from master.sys.dm_exec_requests r with(nolock)
inner join master.sys.dm_exec_sessions s with(nolock)
on r.session_id = s.session_id
where r.session_id>50
and r.session_id<>@@spid
order by r.cpu_time desc
,r.session_id desc
,r.total_elapsed_time desc
/*check top 5 avg cpu*/
select top 5
total_worker_time/execution_count as top_cpu
,total_elapsed_time
,execution_count
,query_text= (select top 1 [text]
from master.sys.dm_exec_sql_text(qs.sql_handle) )
from sys.dm_exec_query_stats as qs with(nolock)
where qs.last_execution_time>=dateadd(mi,-30,getdate())
order by
total_worker_time/execution_count desc
,total_elapsed_time desc