sql-server – 存储过程xyz或使用表值函数的频率 – SQL Server 2008 R2

我们有许多视图,存储过程,表值和标量函数.

我们如何才能看到在SQL Server中调用这些内容的频率甚至是多少次?

我们是否需要编辑每个调用来更新每个调用的表以获取此信息,或者SQL服务器是否将此信息保存在某处?

最佳答案
这是Glenn Berry的DMV查询之一.它计算已执行缓存存储过程的次数(由当前数据库过滤):

SELECT TOP(25) p.name AS [SP Name], qs.execution_count,
ISNULL(qs.execution_count/DATEDIFF(Second, qs.cached_time, GETDATE()), 0) AS [Calls/Second],
qs.total_worker_time/qs.execution_count AS [AvgWorkerTime], qs.total_worker_time AS [TotalWorkerTime],  
qs.total_elapsed_time, qs.total_elapsed_time/qs.execution_count AS [avg_elapsed_time],
qs.cached_time
FROM sys.procedures AS p
INNER JOIN sys.dm_exec_procedure_stats AS qs
ON p.[object_id] = qs.[object_id]
WHERE qs.database_id = DB_ID()
ORDER BY qs.execution_count DESC OPTION (RECOMPILE);

注意:SQL Server仅在上次服务重新启动后保留信息,并且还可能在内存压力下丢弃信息.

另请注意,执行计数本身并不能说明整个情况.识别最昂贵的查询并改进它们通常会更好.我通常从最高逻辑读取开始(这是Glenn的另一个):

SELECT TOP(25) p.name AS [SP Name], qs.total_logical_reads AS [TotalLogicalReads], 
qs.total_logical_reads/qs.execution_count AS [AvgLogicalReads],qs.execution_count, 
ISNULL(qs.execution_count/DATEDIFF(Second, qs.cached_time, GETDATE()), 0) AS [Calls/Second], 
qs.total_elapsed_time, qs.total_elapsed_time/qs.execution_count 
AS [avg_elapsed_time], qs.cached_time
FROM sys.procedures AS p
INNER JOIN sys.dm_exec_procedure_stats AS qs
ON p.[object_id] = qs.[object_id]
WHERE qs.database_id = DB_ID()
ORDER BY qs.total_logical_reads DESC OPTION (RECOMPILE);

[另请注意:您可能有定期安排的流程(比如每月一次).因此,不可能100%识别未使用这些程序调用的存储过程.

转载注明原文:sql-server – 存储过程xyz或使用表值函数的频率 – SQL Server 2008 R2 - 代码日志