Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How do I find long running queries in SQL server with process ID, process name , login time, user , start time and duration?

Asked by Edyth Ferrill May 22, 2024 41.5K views 2 answers
Share

About this question

Can anyone help me to find a below given details for long running query. ProcessID, process name, database, host ,user ,process login time, query start time and query duration. How to find long running queries in sql server?I am looking for a query or an SP which gives me this data.


Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Apr 18, 2024

To find long-running queries in SQL Server along with process ID, process name, login time, user, start time, and duration, you can use the following query:

SELECT
    s.session_id AS 'Process ID',
    s.program_name AS 'Process Name',
    s.login_time AS 'Login Time',
    s.login_name AS 'User',
    r.start_time AS 'Start Time',
    DATEDIFF(MINUTE, r.start_time, GETDATE()) AS 'Duration (Minutes)'
FROM
    sys.dm_exec_sessions AS s
JOIN
    sys.dm_exec_requests AS r
ON
    s.session_id = r.session_id
WHERE
    r.status IN ('running', 'runnable')
ORDER BY
    r.start_time;

This query retrieves information about active sessions (sys.dm_exec_sessions) and their corresponding requests (sys.dm_exec_requests). It filters out requests that are currently running or runnable. The duration of the query is calculated by finding the difference in minutes between the start time of the request and the current time. Finally, the results are ordered by the start time of the request.

Using this query, you can identify long-running queries along with additional details such as process ID, process name, login time, user, start time, and duration.

Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.