Click here to Skip to main content
15,918,193 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
How do i know the long running queries inside a stored procedure in sql server


What I have tried:

I have tried the below query but it is showing all the long running queries in a data base.But I want only the long running quries inside a stored procedure.

SELECT st.text,
qp.query_plan,
qs.*
FROM (
SELECT TOP 50 *
FROM sys.dm_exec_query_stats
ORDER BY total_worker_time DESC
) AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp
WHERE qs.max_worker_time > 300
OR qs.max_elapsed_time > 300
Posted
Updated 30-Jun-16 1:25am
Comments
Daniel Jones 1-Jul-16 1:05am    
take a look at these links
https://www.simple-talk.com/sql/performance/which-of-your-stored-procedures-are-using-the-most-resources/
http://blog.sqlauthority.com/2009/01/02/sql-server-2008-2005-find-longest-running-query-tsql/

1 solution

Try this way:
SQL
SELECT st.text,
qp.query_plan,
qs.*
FROM (
SELECT TOP 50 *
FROM sys.dm_exec_procedure_stats
ORDER BY total_worker_time DESC
) AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp
WHERE qs.max_worker_time > 300
OR qs.max_elapsed_time > 300
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900