Reporting CPU Usage in SQL Server
Hello everyone,
In this article, I will try to give information about reporting CPU usage in SQL Server.
In SQL Server you may want to report CPU usage in some cases.
You can easily do this using the code below.
SELECT DB_NAME(st.dbid) AS DatabaseName,
OBJECT_SCHEMA_NAME(st.objectid, dbid) AS SchemaName,
cp.objtype AS ObjectType,
OBJECT_NAME(st.objectid, dbid) AS Objects,
MAX(cp.usecounts) AS Total_Execution_count,
SUM(qs.total_worker_time) AS Total_CPU_Time,
SUM(qs.total_worker_time) / (MAX(cp.usecounts) * 1.0) AS Avg_CPU_Time
FROM sys.dm_exec_cached_plans cp
INNER JOIN sys.dm_exec_query_stats qs
ON cp.plan_handle = qs.plan_handle
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
WHERE DB_NAME(st.dbid) IS NOT NULL
GROUP BY DB_NAME(st.dbid),
OBJECT_SCHEMA_NAME(objectid, st.dbid),
cp.objtype,
OBJECT_NAME(objectid, st.dbid)
ORDER BY SUM(qs.total_worker_time) DESC;
Good luck to everyone in business and life.