如何监视每个时间段每个存储过程的调用计数?


10

为了诊断某些性能问题,我想更好地了解某些过程与系统性能相比被调用的次数。有没有一种方法可以获取某个时间范围内每个过程被调用的次数?

Answers:


17

您可以从动态管理视图(DMV)中获得此(以及更多)。要获取特定存储过程的统计信息,请尝试以下查询。

SELECT
    OBJECT_NAME(qt.objectid)
  , qs.execution_count AS [Execution Count]
  , qs.execution_count / DATEDIFF(Second, qs.creation_time, GETDATE()) AS [Calls/Second]
  , qs.total_worker_time / qs.execution_count AS [AvgWorkerTime]
  , qs.total_worker_time AS [TotalWorkerTime]
  , qs.total_elapsed_time / qs.execution_count AS [AvgElapsedTime]
  , qs.max_logical_reads
  , qs.max_logical_writes
  , qs.total_physical_reads
  , DATEDIFF(Minute, qs.creation_time, GETDATE()) AS [Age in Cache]
FROM
    sys.dm_exec_query_stats AS qs
CROSS APPLY 
    sys.dm_exec_sql_text(qs.[sql_handle]) AS qt
WHERE
    qt.[dbid] = DB_ID()
AND qt.objectid = OBJECT_ID('StoredProcedureName')
OPTION (RECOMPILE);

要查看最常执行的过程:

SELECT
    OBJECT_NAME(qt.objectid)
  , qs.execution_count AS [Execution Count]
  , qs.execution_count / DATEDIFF(Second, qs.creation_time, GETDATE()) AS [Calls/Second]
  , qs.total_worker_time / qs.execution_count AS [AvgWorkerTime]
  , qs.total_worker_time AS [TotalWorkerTime]
  , qs.total_elapsed_time / qs.execution_count AS [AvgElapsedTime]
  , qs.max_logical_reads
  , qs.max_logical_writes
  , qs.total_physical_reads
  , DATEDIFF(Minute, qs.creation_time, GETDATE()) AS [Age in Cache]
FROM
    sys.dm_exec_query_stats AS qs
CROSS APPLY 
    sys.dm_exec_sql_text(qs.[sql_handle]) AS qt
WHERE
    qt.[dbid] = DB_ID()
ORDER BY
    qs.execution_count DESC
OPTION (RECOMPILE);

报告的值是自上次重新启动以来的累积值。如果要在固定时间内进行测量,请使用以下命令重置等待状态。

DBCC SQLPERF("sys.dm_os_wait_stats",CLEAR);

如果要测量一天中的固定时间跨度,则可以通过代理作业将查询输出馈送到表中,或者a)计算两次运行之间的值,或者b)将等待统计信息重置为代理作业的最后一步。

或者,捕获探查器跟踪并通过Clear Trace运行它。


似乎我得到了许多结果集,但不完全是。例如,我在object_name响应列下看到了几个相同的对象,但是除了一些例外之外,大多数细节都是相同的。匹配的列详细信息:ExecutionCount,Call / Second,AgeInCache。不匹配的列详细信息:AvgWorkerTime,TotalWorkerTime,AvgElapesedTime。是什么导致多对多结果?
kstubs '18 -10-3
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.