Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS SQL DMVs for UDF performance stats - how to find top 10 worst UDFs

I heard in Microsoft SQL Server there are multiple ways to find "worst" stored procedures: by number of executions, by CPU worker time, by queue wait time etc.

I am looking for a way to find worst (slowest / most used) UDFs -- is there a DMV query for that?

For example this query from Five DMV Queries That Will Make You a Superhero returns top 25 cached SPs by total worker time, which effectively means most expensive stored procedures for CPU pressure.

How do I write something like this for UDFs?

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);
like image 797
Stop Putin Stop War Avatar asked Oct 12 '22 06:10

Stop Putin Stop War


1 Answers

Try this.

  • changing sys.procedures to sys.sql_modules
  • add further join to sys.objects
  • filter by sys.objects.type IN ('IF', 'TF', 'FN')

The code above restricts to stored procs only

Note: there is no "sys.functions"

like image 59
gbn Avatar answered Oct 14 '22 07:10

gbn