简体   繁体   中英

How to quickly get last_elapsed_time for a stored procedure in SQL

I am trying to make a log for an API with some SQL data.

I'd like to get the last_elapsed_time (the amount of time it took for the stored procedure to run).

I am able to get at this data with a cross apply, but it's very slow ~15 seconds.

To make a performant log this time needs to be drastically reduced.

select 
    'text' = st.text,
    'last_exec_time' = qs.last_execution_time,
    'last_elapsed_time' = qs.last_elapsed_time
from 
    sys.dm_exec_query_stats as qs
cross apply 
    sys.dm_exec_sql_text(qs.sql_handle) as st
where 
    st.text like '%name_of_stored_procedure%'

Is there a faster way to get the last_elapsed_time of a specific stored procedure?

You can try using the plan handle instead of the sql handle:

SELECT 
    'text' = dest.text,
    'last_exec_time' = qs.last_execution_time, 
    'last_elapsed_time' = qs.last_elapsed_time
FROM sys.dm_exec_cached_plans decp
CROSS APPLY sys.dm_exec_sql_text(decp.plan_handle) AS dest
INNER JOIN sys.dm_exec_query_stats qs on qs.plan_handle = decp.plan_handle
WHERE dest.objectid = OBJECT_ID('<your procedure name>')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM