5

我听说在 Microsoft SQL Server 中有多种方法可以找到“最差”的存储过程:按执行次数、CPU 工作时间、队列等待时间等。

我正在寻找一种方法来找到最差(最慢/最常用)的 UDF - 是否有 DMV 查询?

例如,五个 DMV 查询将使您成为超级英雄的查询返回按总工作时间计算的前 25 个缓存 SP,这实际上意味着 CPU 压力最昂贵的存储过程。

我如何为 UDF 写这样的东西?

SELECT TOP(25) p.name AS [SP Name], qs.total_worker_time AS [TotalWorkerTime], 
qs.total_worker_time/qs.execution_count AS [AvgWorkerTime], 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_worker_time DESC OPTION (RECOMPILE);
4

3 回答 3

2

这是查找性能最差的 TOP 100 SQL 语句的另一种方法,其对象名称包含这些语句,例如存储过程、触发器和函数。这在上下文中运行,因此您需要将 SSMS 切换到要在其上运行它的数据库,或者放置USE DB语句。此外,如果您重新启动数据库,DMV 统计数据将重置,因此它可能会返回“0”行......


SELECT  TOP 100
            [Object_Name] = object_name(st.objectid),
            creation_time, 
            last_execution_time, 
            total_cpu_time = total_worker_time / 1000, 
            avg_cpu_time = (total_worker_time / execution_count) / 1000,
            min_cpu_time = min_worker_time / 1000,
            max_cpu_time = max_worker_time / 1000,
            last_cpu_time = last_worker_time / 1000,
            total_time_elapsed = total_elapsed_time / 1000 , 
            avg_time_elapsed = (total_elapsed_time / execution_count) / 1000, 
            min_time_elapsed = min_elapsed_time / 1000, 
            max_time_elapsed = max_elapsed_time / 1000, 
            avg_physical_reads = total_physical_reads / execution_count,
            avg_logical_reads = total_logical_reads / execution_count,
            execution_count, 
            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 qs
CROSS APPLY 
            sys.dm_exec_sql_text(qs.sql_handle) st
WHERE
            Object_Name(st.objectid) IS NOT NULL
            AND st.dbid = DB_ID()
ORDER BY 
            db_name(st.dbid), 
            total_worker_time / execution_count  DESC


于 2011-05-12T02:53:30.253 回答
2

试试这个。

上面的代码仅限于存储过程

注意:没有“sys.functions”

于 2011-05-14T11:48:11.867 回答
0

SQL Server 2016 及更高版本(和 SQL Azure)具有 DMV sys.dm_exec_function_stats

返回有关标量函数的信息,包括内存中函数和 CLR 标量函数。不返回有关表值函数以及使用标量 UDF 内联内联的标量函数的信息。

SELECT 
    OBJECT_NAME(object_id) AS ScalarFunction, 
    execution_count,
    total_worker_time, 
    total_logical_reads, 
    total_elapsed_time,
    min_worker_time,  -- Note: microseconds
    max_worker_time,  -- Note: microseconds
    AvgWorkerTimePerExecution_microsecs = total_worker_time / (1. * execution_count)
FROM 
    sys.dm_exec_function_stats WITH(NOLOCK) 
WHERE 
    database_id = DB_ID()
    AND OBJECT_NAME(object_id) IS NOT NULL
ORDER BY 
    total_worker_time DESC 
OPTION(RECOMPILE); 

注意:所有时间指标都以微秒为单位

于 2021-10-25T06:14:10.403 回答