我们有许多视图,存储过程,表值和标量函数。
我们怎样才能看到在SQL Server中调用这些内容的频率甚至是多少次?
我们是否需要编辑每一个以在每次调用时更新表以获取此信息,或者SQL服务器是否将此信息保存在某处?
答案 0 :(得分:9)
这是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仅在上次服务重启后保留信息,并且还可能在内存压力下丢弃信息。
另请注意,仅执行计数并不能说明整个情况。识别最昂贵的查询并改进它们通常会更好。我通常从最高逻辑读取开始(这是格伦的另一个):
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%识别使用这些非调用的存储过程。]