SQL Server消耗大部分CPU时间

时间:2018-06-06 12:11:50

标签: sql sql-server sql-server-2008 sql-server-2012 database-administration

在我的环境中,CPU利用率在特定时区很高。当我运行以下查询时,我知道sql server,所以记录id占用大部分CPU时间。

DECLARE @ms_ticks_now BIGINT
SELECT @ms_ticks_now = ms_ticks
FROM sys.dm_os_sys_info;
SELECT  record_id
       ,dateadd(ms, - 1 * (@ms_ticks_now - [timestamp]), GetDate()) AS EventTime
       ,SQLProcessUtilization
       ,SystemIdle
       ,100 - SystemIdle - SQLProcessUtilization AS OtherProcessUtilization
       ,TIMESTAMP
FROM (
       SELECT record.value('(./Record/@id)[1]', 'int') AS record_id
              ,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS SystemIdle
              ,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS SQLProcessUtilization
              ,TIMESTAMP
       FROM (
              SELECT TIMESTAMP
                     ,convert(XML, record) AS record
              FROM sys.dm_os_ring_buffers
              WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
                     AND record LIKE '%<SystemHealth>%'
              ) AS x
       ) AS y
ORDER BY record_id DESC

使用获得结果中的RecordID,我们可以得到运行的查询文本。如果我们可以请分享查询

由于 拉玛拉朱

1 个答案:

答案 0 :(得分:0)

我使用以下查询来查找查询,并查看前100个AvgDuration的计划,或者您可以按TotalCPU修改订单。只要尚未清除计划缓存,您就应该能够找到查询和last_execution_time。可以肯定的是,我在StackOverflow上的某个地方找到了此查询,尽管记不清了。

SELECT TOP 100 
    DB_NAME(st.dbid) as DatabaseName,
    total_worker_time / execution_count AS AvgCPU ,
    total_worker_time AS TotalCPU ,
    (total_elapsed_time / execution_count)/1000000 AS AvgDurationSeconds,
    total_elapsed_time AS TotalDuration ,
    total_elapsed_time / 1000000 as TotalDurationSeconds,
    total_logical_reads / execution_count AS AvgReads ,
    total_logical_reads AS TotalReads ,
    execution_count ,
    qs.creation_time AS plan_creation_time,
    qs.last_execution_time,
        CONVERT(VARCHAR(MAX),REPLACE(REPLACE(REPLACE(SUBSTRING(st.text, ( qs.statement_start_offset / 2 ) + 1, ( ( CASE qs.statement_end_offset
        WHEN -1 THEN DATALENGTH(st.text)
        ELSE qs.statement_end_offset
        END - qs.statement_start_offset ) / 2 ) + 1),CHAR(13), ''),CHAR(9),' '),CHAR(10),' ')) AS QueryText,
        query_plan
    FROM sys.dm_exec_query_stats AS qs
        CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
        CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp
    WHERE DB_NAME(st.dbid) IS NOT NULL
    /* Sorting options - uncomment the one you'd like to use: */
    --ORDER BY TotalReads DESC;
    --ORDER BY TotalCPU DESC;
    ORDER BY AvgDurationSeconds DESC;
    --ORDER BY TotalDuration DESC;
    --ORDER BY execution_count DESC;