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.

97 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!