Query SQL DB to find Long Running Scripts

How to query the SQL DB to find long running scripts?

Issue Description : When the database is having performance issues, it can be helpful to try and identify long running scripts to see queries are causing problems.


  • This query needs to be run with admin access to the database server:

SELECT creation_time





, execution_count

, total_worker_time

, total_elapsed_time

, total_elapsed_time / execution_count avg_elapsed_time

,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,

((CASE statement_end_offset


ELSE qs.statement_end_offset END

- qs.statement_start_offset)/2) + 1) AS statement_text

FROM sys.dm_exec_query_stats AS qs

CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st

ORDER BY total_elapsed_time / execution_count DESC;