How to calculate CPU usage per transaction?

  • Hi,

    As below script output SQL Statement grouped by their cumulative CPU resource consumption. How can calculate CPU usage time per query?

    SELECT TOP 5 query_stats.query_hash AS "Query Hash",

    SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) AS "Avg CPU Time",

    MIN(query_stats.statement_text) AS "Statement Text"

    FROM

    (SELECT QS.*,

    SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,

    ((CASE statement_end_offset

    WHEN -1 THEN DATALENGTH(ST.text)

    ELSE QS.statement_end_offset END

    - QS.statement_start_offset)/2) + 1) AS statement_text

    FROM sys.dm_exec_query_stats AS QS

    CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_stats

    GROUP BY query_stats.query_hash

    ORDER BY 2 DESC;

    GO

    Thanks

    ananda

  • Using the sys.dm_exec_query_stats DMV, all you can do is what you've done. Take the average and split it up by execution count. If you want a precise measure, you'll need to audit executions using extended events.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • ananda.murugesan (1/20/2014)


    Hi,

    As below script output SQL Statement grouped by their cumulative CPU resource consumption. How can calculate CPU usage time per query?

    SELECT TOP 5 query_stats.query_hash AS "Query Hash",

    SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) AS "Avg CPU Time",

    MIN(query_stats.statement_text) AS "Statement Text"

    FROM

    (SELECT QS.*,

    SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,

    ((CASE statement_end_offset

    WHEN -1 THEN DATALENGTH(ST.text)

    ELSE QS.statement_end_offset END

    - QS.statement_start_offset)/2) + 1) AS statement_text

    FROM sys.dm_exec_query_stats AS QS

    CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_stats

    GROUP BY query_stats.query_hash

    ORDER BY 2 DESC;

    GO

    Thanks

    ananda

    Have you actually looked at the underlying views use in that query to see what's available? I'm sure that if you do, you can probably figure out and average like Grant says.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply