SQL Server Long-Running Queries: Find Active Sessions
This script queries sys.dm_exec_requests and sys.dm_exec_sql_text to show all currently executing queries on the SQL Server instance, sorted by how long they have been running, along with CPU time, I/O activity, blocking status, and wait type information.
Purpose and Overview
A query that normally completes in seconds but has now been running for many minutes is a routine call-out for a DBA. The cause could be blocking from another session, a missing index, parameter sniffing producing a bad plan, or a sudden increase in data volume. The first step in diagnosing the problem is knowing what is running right now. SQL Server exposes this through the sys.dm_exec_requests dynamic management view, which holds one row for every active request currently executing on the instance. Combined with sys.dm_exec_sql_text, which returns the actual T-SQL text for a given SQL handle, this query gives you immediate visibility into what every session is doing, how long it has been running, and whether it is waiting on something.
Code Breakdown
The complete script is shown below, followed by an explanation of each part.
1SELECT
2 req.session_id,
3 req.start_time,
4 req.command,
5 DB_NAME(req.database_id) AS database_name,
6 req.status,
7 req.cpu_time,
8 req.total_elapsed_time,
9 req.logical_reads,
10 req.writes,
11 req.blocking_session_id,
12 req.wait_resource,
13 req.wait_type,
14 req.last_wait_type,
15 sqltext.text AS query_text
16FROM sys.dm_exec_requests AS req
17CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
18WHERE req.session_id <> @@SPID
19-- AND req.database_id = DB_ID()
20ORDER BY total_elapsed_time DESC;
sys.dm_exec_requests
sys.dm_exec_requests is a dynamic management view that returns one row per currently active request. It is real-time — each execution of this query reflects the state of the server at that moment. Key columns used in this script:
session_id— the SPID (server process ID) of the session running the requeststart_time— when the request arrivedcommand— the type of command, such asSELECT,UPDATE,BACKUP DATABASE, orDBCCdatabase_id— the ID of the database the request is running against; wrapped inDB_NAME()to return the human-readable namestatus— the request state; Microsoft lists six values:background,rollback,running,runnable,sleepingandsuspended.runnablemeans the request is ready and waiting for CPUcpu_time— cumulative CPU milliseconds consumed by this request so fartotal_elapsed_time— wall-clock milliseconds since the request started; this is the primary sort column and the most useful indicator of how long a query has been runninglogical_reads— number of logical read operations performed so far; high values can point to large scans or a missing indexwrites— number of write operations; high values can come from large data modifications or work spilled to tempdbblocking_session_id— the session blocking this request. NULL or 0 means the request isn't blocked, or the blocker can't be identified. Negative values are special cases Microsoft documents (an orphaned distributed transaction, deferred recovery, or a latch owner that can't be tracked), not session IDswait_resource— the specific resource (page, key, object) the request is waiting onwait_type— the current wait type, such asLCK_M_S(shared lock wait) orPAGEIOLATCH_SH(I/O wait)last_wait_type— the most recent wait type, useful when the current wait_type is blank but the request was recently waiting
CROSS APPLY sys.dm_exec_sql_text
sys.dm_exec_sql_text is a table-valued function that accepts a sql_handle and returns the full batch text of the SQL statement. Using CROSS APPLY calls it once per row in sys.dm_exec_requests, returning the text column which holds the T-SQL being executed. sql_handle is nullable, and a request with no handle returns no row from the function, so CROSS APPLY drops it. Use OUTER APPLY instead to keep those requests with a NULL query_text.
Filtering and Ordering
WHERE req.session_id <> @@SPID removes the monitoring query itself from the result set. Without this filter, the script would always show itself as an active request.
The commented-out line -- AND req.database_id = DB_ID() is a useful optional filter. Uncomment it to limit results to the database you are currently connected to, which helps focus attention on a specific application database when the server has many active sessions across multiple databases.
ORDER BY total_elapsed_time DESC puts the longest-running queries at the top, making it easy to spot the most time-consuming sessions immediately.
Key Benefits and Use Cases
- Instantly shows which queries have been running longest on the instance
- Identifies blocked sessions via
blocking_session_id— a positive value is the session doing the blocking - Wait type columns explain why a query is not progressing — lock waits, I/O waits, memory waits, and more
- CPU time and logical reads columns reveal resource-intensive queries without needing a profiler or extended events session
- The
commandcolumn distinguishes SELECT, DML, backup, DBCC, and other operation types at a glance - No configuration required — it reads a built-in dynamic management view; in Azure SQL Database the view is limited to your own connection
Performance Considerations
- Read-only: the query reads dynamic management views, not user tables, so it is light enough to run repeatedly during an incident.
- Snapshot isolation: DMV data reflects the state at the moment of the query execution. On a busy server, run the query multiple times a few seconds apart to see whether a session is progressing or stuck.
- sys.dm_exec_query_stats for history:
sys.dm_exec_requestsonly shows currently running queries. For historical query performance — finding the top CPU consumers over the past hour — usesys.dm_exec_query_stats, which retains cumulative stats for cached query plans. - Extended Events: For continuous monitoring or capturing queries that complete quickly, an Extended Events session targeting
sql_statement_completedorrpc_completedprovides much better coverage than polling this DMV. - Required permissions: with
VIEW SERVER STATEa user sees every executing session; without it, only the current session. From SQL Server 2022 the view requiresVIEW SERVER PERFORMANCE STATEinstead.VIEW SERVER STATEcan't be granted in Azure SQL Database.
Practical Tips
To also see percent complete for the operations that report it — Microsoft lists commands including BACKUP DATABASE, RESTORE DATABASE, DBCC CHECKDB, DBCC SHRINKFILE, ALTER INDEX REORGANIZE and ROLLBACK — and to identify who is running each query, extend the query with two joins:
1SELECT
2 req.session_id,
3 req.start_time,
4 req.command,
5 DB_NAME(req.database_id) AS database_name,
6 req.status,
7 req.percent_complete,
8 DATEADD(MILLISECOND, req.estimated_completion_time, GETDATE()) AS estimated_finish_time,
9 req.cpu_time,
10 req.total_elapsed_time,
11 req.logical_reads,
12 req.blocking_session_id,
13 req.wait_type,
14 SUBSTRING(t.text, (req.statement_start_offset / 2) + 1,
15 ((CASE req.statement_end_offset
16 WHEN -1 THEN DATALENGTH(t.text)
17 ELSE req.statement_end_offset
18 END - req.statement_start_offset) / 2) + 1) AS current_statement,
19 s.login_name,
20 s.host_name,
21 c.client_net_address
22FROM sys.dm_exec_requests AS req
23INNER JOIN sys.dm_exec_sessions AS s
24 ON req.session_id = s.session_id
25INNER JOIN sys.dm_exec_connections AS c
26 ON req.session_id = c.session_id
27CROSS APPLY sys.dm_exec_sql_text(req.sql_handle) AS t
28WHERE req.session_id <> @@SPID
29ORDER BY req.total_elapsed_time DESC;
This extended version adds login_name, host_name, and client_net_address from sys.dm_exec_sessions and sys.dm_exec_connections, which helps identify which application or user is responsible for a long-running query. The SUBSTRING expression cuts the current statement out of the batch. The offsets are in bytes, starting at 0, while text is Unicode at two bytes per character, so each offset is halved, the start gets + 1 because SUBSTRING counts from 1, and an end offset of -1 means the statement runs to the end of the batch.
estimated_finish_time adds estimated_completion_time to the current time. Microsoft documents that column as "Internal only", so treat the result as a rough guide and rely on percent_complete for the operations listed above.
To kill a specific long-running session after identifying it, use KILL <session_id>. Always verify the session belongs to a query that is safe to terminate before doing so.
Conclusion
Querying sys.dm_exec_requests is the fastest way to see what SQL Server is executing right now. Sorted by elapsed time and joined to sys.dm_exec_sql_text, this script gives you the query text, resource consumption, blocking status, and wait type for every active session in a few seconds. It requires no setup and is light enough to run repeatedly during a live performance incident. Use the extended version with sys.dm_exec_sessions and sys.dm_exec_connections when you also need to identify the application, login, or host responsible for a slow query.
References
- DBA-Scripts on GitHub by Bulent Gucuk — Source repository containing the active queries script adapted for this article
- Microsoft Docs: sys.dm_exec_requests — Full column reference for the active requests dynamic management view
- Microsoft Docs: sys.dm_exec_sql_text — Reference for the table-valued function that returns SQL batch text from a sql_handle
- Microsoft Docs: sys.dm_exec_query_stats — Cached query plan statistics for historical performance analysis
Posts in this series
- SQL Server Table Row Count Report: Complete T-SQL Script
- Generate sp_spaceused Commands for All User Tables in T-SQL
- SQL Server Blocking Detection: Find Blocked Sessions
- SQL Server Database Size Report for All Databases
- SQL Server Long-Running Queries: Find Active Sessions
- SQL Server TempDB Usage and Contention Report
- SQL Server Database File Space Usage Report
- SQL Server Get Table and Index Storage Size Report Script
- Age Open Transactions with sys.dm_tran_active_transactions
- Decode PLE and Buffer Cache Hit Ratio via cntr_type Codes
- Spot Low-Memory Conditions with sys.dm_os_process_memory
- Track Azure SQL Database Load with sys.dm_db_resource_stats
- Inventory Open Cursors per Session with sys.dm_exec_cursors