0

I have several db in my instance in SQL Server. I wonder which db consume more core from my computer.

How to find this information?

3
  • To clarify, you want to know which database(s) on the instance are consuming the most CPU on the server?
    – rvsc48
    Commented Apr 5 at 17:26
  • @rvsc48 Yes, maybe ı need a script
    – Merve
    Commented Apr 8 at 6:25
  • 1
    Databases don't consume CPU, sessions running queries and various SQL Server tasks consume CPU. You should monitor the sessions if that's your goal. Commented Apr 8 at 11:50

1 Answer 1

2

Yes, just as Sean states, however, here is a query which may help to point to which databases' queries are the most on the instance by querying aggregate performance statistics for cached query plans by db.

You'll want to pay attention to how old the plan cache is, since it's a volatile place, and prone to clearing out. It also has a lot of blind spots around queries and procedures with recompile hints, etc. If you have Query Store enabled, I'd suggest using that instead.

WITH
    DB_CPU_Stats AS
(
    SELECT
        F_DB.database_id,
        database_name = DB_NAME(F_DB.database_id),
        cpu_time_seconds = SUM(qs.total_worker_time) / 60000000.,
        min_creation_time = MIN(creation_time),
        max_creation_time = MAX(creation_time),
        total_execution_count = SUM(execution_count)
    FROM sys.dm_exec_query_stats AS qs
    CROSS APPLY
    (
        SELECT
            database_id = CONVERT(integer, p.value)
        FROM sys.dm_exec_plan_attributes(qs.plan_handle) AS p
        WHERE attribute = N'dbid'
    ) AS F_DB
    GROUP BY
        F_DB.database_id
)
SELECT
    DB_CPU_Stats.database_name,
    DB_CPU_Stats.cpu_time_seconds,
    DB_CPU_Stats.min_creation_time,
    DB_CPU_Stats.max_creation_time,
    oldest_plan_days = DATEDIFF(DAY, DB_CPU_Stats.min_creation_time, SYSDATETIME()),
    DB_CPU_Stats.total_execution_count,
    [CPUPercent] = 
        CAST(DB_CPU_Stats.cpu_time_seconds * 1.0 / 
          SUM(DB_CPU_Stats.cpu_time_seconds) OVER () * 100.0 AS decimal(5, 2))
FROM DB_CPU_Stats
WHERE DB_CPU_Stats.database_id > 4 
AND   DB_CPU_Stats.database_id < 32767 
ORDER BY
    ROW_NUMBER() OVER 
    (
        ORDER BY
            DB_CPU_Stats.cpu_time_seconds DESC
    )
OPTION (RECOMPILE);
1
  • Thanks for the edits Erik - appreciate.
    – rvsc48
    Commented Apr 8 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.