SQLSERVER排查CPU占用高的情况

时间:2021-07-09 12:43:01

--查询CPU耗时最多的50个查询
SELECT TOP 50 total_worker_time/execution_count/1000/1000 AS [CPU平均执行(秒)],
SUBSTRING(st.text, (qs.statement_start_offset/2)+1,
((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2) + 1) N'执行语句'
,st.text N'完整语句'
,total_worker_time/1000/1000 AS [总消耗CPU 时间(秒)]
,execution_count [运行次数]
,qs.total_worker_time/qs.execution_count/1000/1000 AS [平均执行CPU 时间(秒)]
,last_execution_time AS [最后一次执行时间]
,max_worker_time /1000/1000 AS [最大执行时间(秒)]
,total_physical_reads N'物理读取总次数'
,total_logical_reads/execution_count N'每次逻辑读次数'
,total_logical_reads N'逻辑读取总次数'
,total_logical_writes N'逻辑写入总次数'
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
WHERE last_execution_time>'2015-10-26 14:25:00'
ORDER BY total_worker_time/execution_count DESC;


--查询当前阻塞的请求
SELECT session_id ,status ,blocking_session_id
,wait_type ,wait_time ,wait_resource
,transaction_id
,SUBSTRING(st.text, (qs.statement_start_offset/2)+1,
((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_requests qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
WHERE status = N'suspended';