Query to get top 30 high CPU quries
SELECT TOP 30
DB_NAME(st.dbid) AS 'Database Name',
SERVERPROPERTY('ServerName') AS 'Server Name',
s.session_id AS 'Session ID',
s.login_name AS 'Login Name',
getdate() as [TImenow],
qs.creation_time AS 'Creation Time',
qs.execution_count AS 'Execution Count',
qs.total_worker_time AS 'Total CPU Time (ms)',
qs.total_worker_time / qs.execution_count AS 'Average CPU Time (ms)',
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 'Query Text'
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
INNER JOIN sys.dm_exec_requests AS r ON qs.plan_handle = r.plan_handle
INNER JOIN sys.dm_exec_sessions AS s ON r.session_id = s.session_id
ORDER BY qs.total_worker_time DESC;
CPU details complete query
SELECT
DB_NAME(r.database_id) AS 'Database Name',
s.session_id,
r.STATUS,
r.blocking_session_id AS 'Blocked By',
r.wait_type,
r.wait_resource,
r.wait_time / (1000.0) AS 'Wait Time (in Sec)',
r.cpu_time,
r.logical_reads,
r.reads,
r.writes,
r.total_elapsed_time / (1000.0) AS 'Elapsed Time (in Sec)',
SUBSTRING(st.TEXT, (r.statement_start_offset / 2) + 1,
((CASE r.statement_end_offset WHEN -1 THEN DATALENGTH(st.TEXT) ELSE r.statement_end_offset END - r.statement_start_offset) / 2) + 1) AS statement_text,
COALESCE(QUOTENAME(DB_NAME(st.dbid)) + N'.' + QUOTENAME(OBJECT_SCHEMA_NAME(st.objectid, st.dbid)) + N'.' + QUOTENAME(OBJECT_NAME(st.objectid, st.dbid)), '') AS command_text,
r.command,
s.login_name,
s.host_name,
s.program_name,
s.host_process_id,
s.last_request_end_time,
s.login_time,
r.open_transaction_count
FROM sys.dm_exec_sessions AS s
INNER JOIN sys.dm_exec_requests AS r ON r.session_id = s.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
INNER JOIN sys.databases AS d ON r.database_id = d.database_id
WHERE r.session_id != @@SPID
ORDER BY r.cpu_time DESC, r.STATUS, r.blocking_session_id, s.session_id;
Post a Comment