SQL SERVER – Stored Procedure Timing and Cached Measurements

SET STATISTICS TIME reports CPU and elapsed execution time for Stored Procedures. A customer wanted to measure a run without adding statements manually.

A whole assembly is inspected separately from its three internal component workpieces.

-- Substitute an approved procedure and arguments; it may change data.
-- SET STATISTICS TIME ON;
-- EXEC dbo.YourProcedure;
-- SET STATISTICS TIME OFF;
SELECT DB_NAME(database_id) AS DatabaseName,OBJECT_NAME(object_id,database_id) AS ProcedureName,
       execution_count,last_elapsed_time/1000.0 AS LastElapsedMs,
       total_elapsed_time/1000.0/NULLIF(execution_count,0) AS AverageElapsedMs
FROM sys.dm_exec_procedure_stats WHERE database_id=DB_ID()
ORDER BY AverageElapsedMs DESC;

Enable the setting for a controlled run with known parameters, then inspect Messages and disable it afterward. The four old timing lines were illustrative. Nested statements and compilation can produce multiple messages. There is no universal four-line output contract.

Waits can increase elapsed time. Parallel-worker CPU can exceed wall-clock duration. Network transfer, rendering and application processing also need separate end-to-end measurements.

The DMV reports cached procedure history in milliseconds. Entries and counters last only while the plan remains cached. It does not retain permanent history. Use completed events or suitable persisted monitoring when a retained execution record is required.

Reference: Cached stored-procedure timings.

Related reading

Cached procedure history is not a measurement of one new execution, it is accumulated evidence while the plan remains cached.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Execution Plan, SQL Server Management Studio, SQL Statistics, SQL Stored Procedure
Previous Post
SQL SERVER – Using NOEXPAND with Indexed View
Next Post
SQL SERVER – High Frequency Cached Query Counts and Statement Metrics

Related Posts

1 Comment. Leave new

  • Alejandro Ariel Abaca
    August 11, 2021 3:51 pm

    Hello, Pinal. Ariel from Argentina here. Does this trick work with another type of code too, like functions and views?

    Thanks in advance. Regards. Ariel

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.