Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15889

sql_handle for a Stored Proc does not exist in dm_exec_query_stats

$
0
0

I am in the process of creating a script that would return the resource usage of stored procs in SQL 2005 as there is no "dm_exec_procedure_stats" DMV available in SQL 2005. I have used the following script to achieve it. However when i compare the results, I get less number of rows in SQL 2005. I can see the plan_handle of the proc in exec_cached_plans, but i don't see a sql_handle for it in dm_exec_query_stats.

Under what circumstances would we encounter such a situation.Please find the queries below

-- WITH Procedure Stats
SELECT @@servername
      ,getdate()
      ,CASE WHEN q.dbid = 32767 then 'Resource' ELSE DB_NAME(q.dbid)END AS DBName
	  ,OBJECT_NAME(objectid)AS [OBJECT_NAME]
      , p.cacheobjtype
      , p.objtype
      ,(highest_cpu_queries.total_worker_time/highest_cpu_queries.execution_count) AS AverageCPU
      , highest_cpu_queries.execution_count
     , highest_cpu_queries.last_execution_time
FROM sys.dm_exec_procedure_stats  AS highest_cpu_queries 
inner join sys.dm_exec_cached_plans p on highest_cpu_queries.plan_handle = p.plan_handle     
CROSS APPLY sys.dm_exec_sql_text(highest_cpu_queries.plan_handle) AS q 
WHERE q.dbid=DB_ID()
ORDER BY AverageCPU DESC
-- WITH Query Stats
SELECT @@servername as ServerName
,getdate() as [Date]
,CASE WHEN dbid = 32767 then 'Resource' ELSE DB_NAME(dbid)END AS DBName
,OBJECT_NAME(objectid)AS [OBJECT_NAME]
,cp.cacheobjtype
,cp.objtype
,SUM(total_worker_time) / MAX(usecounts) AS AVG_CPU
,SUM(total_elapsed_time) / MAX(usecounts) AS AVG_ELAPSED
,MAX(usecounts) AS [execution_count]
,MAX(last_execution_time) AS 'last_execution_time'
FROM sys.dm_exec_query_stats qs  
join sys.dm_exec_cached_plans cp on qs.plan_handle = cp.plan_handle 
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) 
WHERE objtype = 'Proc'  and dbid=DB_ID()
AND text NOT LIKE '%CREATE FUNC%' 
GROUP BY cp.plan_handle,[DBID],objectid,cp.cacheobjtype,cp.objtype 
ORDER BY AVG_CPU DESC

Regards,

Danda


Viewing all articles
Browse latest Browse all 15889

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>