Sql-server – How to get the whole execution log of stored procedure

sql-server-2008sql-server-2008-r2

I know that how many times it was executed from execution_count in sys.dm_exec_procedure_stats.But how these values are Calculated? Is there any way to know how these values get calculated? I want whole log when the stored procedure was first time executed to till last_execution_time logs.

Best Answer

But how these values are Calculated? Is there any way to know how these values get calculated?

Answer to this can be refer from MSDN stating below

sys.dm_exec_procedure_stats

Returns aggregate performance statistics for cached stored procedures. The view returns one row for each cached stored procedure plan, and the lifetime of the row is as long as the stored procedure remains cached. When a stored procedure is removed from the cache, the corresponding row is eliminated from this view. At that time, a Performance Statistics SQL trace event is raised similar to sys.dm_exec_query_stats

You can run something like below code to collect the data from this dmv as per the load to analyse the usage of resources during the execution of that SP:

SELECT CASE WHEN database_id = 32767 then 'Resource' ELSE DB_NAME(database_id)END AS DBName
      ,OBJECT_SCHEMA_NAME(object_id,database_id) AS [SCHEMA_NAME] 
      ,OBJECT_NAME(object_id,database_id)AS [OBJECT_NAME]
      ,cached_time
      ,last_execution_time
      ,execution_count
      ,total_worker_time / execution_count AS AVG_CPU
      ,total_elapsed_time / execution_count AS AVG_ELAPSED
      ,total_logical_reads / execution_count AS AVG_LOGICAL_READS
      ,total_logical_writes / execution_count AS AVG_LOGICAL_WRITES
      ,total_physical_reads  / execution_count AS AVG_PHYSICAL_READS
FROM sys.dm_exec_procedure_stats 
ORDER BY AVG_LOGICAL_READS DESC

You can modify the query above and gather the data to collect the metrics for the SP by scheduling it via SQL agent job. After a certain interval you will have sufficient data to make analysis. An example of such is illustrated here.

Hope this helps!