Get CPU utilization by database

Oopps! Upgrade your browser pretty please. Oopps! Upgrade your browser pretty please.

När man analyserar och felsöker en server som innehåller flera databaser är det väldigt intressant att veta vilken av dessa databaser som förbrukar mest CPU resurser. Då använder man som reglel dmv’en dm_exec_query_stats. Problemet med informationen som då får är att den är ackumelerad sedan SQL Servern startades om. Ofta vill man istället se nuläget och då kan man använda nedanstående SQL som härstämmar från ett av Glen Berrys skript. Helt enkelt sparar man resultatet i en temptabell, väntar en tid t.ex. 1 minut, sparar resultatet i en ny temptabell och sedan jämför dessa värden, istället för att bara presentera utnyttjandet sedan servern startades om. Nedan får man båda dessa värden i procent som ger en bättre bild av verkligheten just nu.

— Get CPU utilization by database

WITH DB_CPU_Stats AS (SELECT DatabaseID, DB_Name(DatabaseID) AS [DatabaseName], SUM(total_worker_time) AS [CPU_Time_Ms]  FROM sys.dm_exec_query_stats AS qs  CROSS APPLY (SELECT CONVERT(int, value) AS [DatabaseID]               FROM sys.dm_exec_plan_attributes(qs.plan_handle)               WHERE attribute = N’dbid’) AS F_DB  GROUP BY DatabaseID) SELECT ROW_NUMBER() OVER(ORDER BY [CPU_Time_Ms] DESC) AS [row_num],        DatabaseName, [CPU_Time_Ms],        CAST([CPU_Time_Ms] * 1.0 / SUM([CPU_Time_Ms]) OVER() * 100.0 AS DECIMAL(5, 2)) AS [CPUPercent] INTO #tmpDB_CPU_Stats_1 FROM DB_CPU_Stats WHERE DatabaseID > 4 — system databases AND DatabaseID <> 32767 — ResourceDB ORDER BY row_num OPTION (RECOMPILE);

WAITFOR DELAY ’00:01:00′;

WITH DB_CPU_Stats AS (SELECT DatabaseID, DB_Name(DatabaseID) AS [DatabaseName], SUM(total_worker_time) AS [CPU_Time_Ms]  FROM sys.dm_exec_query_stats AS qs  CROSS APPLY (SELECT CONVERT(int, value) AS [DatabaseID]               FROM sys.dm_exec_plan_attributes(qs.plan_handle)               WHERE attribute = N’dbid’) AS F_DB  GROUP BY DatabaseID) SELECT ROW_NUMBER() OVER(ORDER BY [CPU_Time_Ms] DESC) AS [row_num],        DatabaseName, [CPU_Time_Ms],        CAST([CPU_Time_Ms] * 1.0 / SUM([CPU_Time_Ms]) OVER() * 100.0 AS DECIMAL(5, 2)) AS [CPUPercent] INTO #tmpDB_CPU_Stats_2 FROM DB_CPU_Stats WHERE DatabaseID > 4 — system databases AND DatabaseID <> 32767 — ResourceDB ORDER BY row_num OPTION (RECOMPILE);

SELECT  #tmpDB_CPU_Stats_1.DatabaseName,  #tmpDB_CPU_Stats_1.CPUPercent,

 CAST((#tmpDB_CPU_Stats_2.CPU_Time_Ms – #tmpDB_CPU_Stats_1.CPU_Time_Ms) * 1.0 / SUM((#tmpDB_CPU_Stats_2.CPU_Time_Ms – #tmpDB_CPU_Stats_1.CPU_Time_Ms)) OVER() * 100.0 AS DECIMAL(5, 2)) AS [Current_CPUPercent] FROM          #tmpDB_CPU_Stats_1 INNER JOIN  #tmpDB_CPU_Stats_2 ON #tmpDB_CPU_Stats_1.DatabaseName = #tmpDB_CPU_Stats_2.DatabaseName ORDER BY #tmpDB_CPU_Stats_2.CPU_Time_Ms – #tmpDB_CPU_Stats_1.CPU_Time_Ms DESC

DROP TABLE #tmpDB_CPU_Stats_1 DROP TABLE #tmpDB_CPU_Stats_2