CPU utilization by database?

前端 未结 8 1172
情话喂你
情话喂你 2020-12-04 08:21

Is it possible to get a breakdown of CPU utilization by database?

I\'m ideally looking for a Task Manager type interface for SQL server, but instead

8条回答
  •  粉色の甜心
    2020-12-04 08:47

    Here's a query that will show the actual database causing high load. It relies on the query cache which might get flushed frequently in low-memory scenarios (making the query less useful).

    select dbs.name, cacheobjtype, total_cpu_time, total_execution_count from
        (select top 10
            sum(qs.total_worker_time) as total_cpu_time,  
            sum(qs.execution_count) as total_execution_count, 
            count(*) as  number_of_statements,  
            qs.plan_handle
        from  
            sys.dm_exec_query_stats qs 
        group by qs.plan_handle
        order by sum(qs.total_worker_time) desc
        ) a
    inner join 
    (SELECT plan_handle, pvt.dbid, cacheobjtype
    FROM (
        SELECT plan_handle, epa.attribute, epa.value, cacheobjtype
        FROM sys.dm_exec_cached_plans 
            OUTER APPLY sys.dm_exec_plan_attributes(plan_handle) AS epa
         /* WHERE cacheobjtype = 'Compiled Plan' AND objtype = 'adhoc' */) AS ecpa 
    PIVOT (MAX(ecpa.value) FOR ecpa.attribute IN ("dbid", "sql_handle")) AS pvt
    ) b on a.plan_handle = b.plan_handle
    inner join sys.databases dbs on dbid = dbs.database_id
    

提交回复
热议问题