DBAtlas · Database operations atlas

Show active queries and their cost

SQL ServerSession

Command

SELECT r.session_id,
       r.status,
       r.command,
       DB_NAME(r.database_id) AS db,
       r.cpu_time,
       r.total_elapsed_time / 1000 AS elapsed_sec,
       r.reads, r.writes, r.logical_reads,
       r.wait_type,
       SUBSTRING(t.text,
         (r.statement_start_offset/2)+1,
         ((CASE r.statement_end_offset WHEN -1 THEN DATALENGTH(t.text)
            ELSE r.statement_end_offset END - r.statement_start_offset)/2)+1) AS running_statement
FROM   sys.dm_exec_requests r
  JOIN sys.dm_exec_sessions s ON s.session_id = r.session_id
  CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE  s.is_user_process = 1 AND r.session_id <> @@SPID
ORDER BY r.total_elapsed_time DESC;

What it does

Shows currently running user queries with CPU, elapsed time, reads/writes and the exact statement text.

When to use it

To find which query is consuming resources during a live slowdown.

Caution

statement_start/end_offset extracts the exact running statement (not the whole batch). Query only; no risk.

Open in DBAtlas →

Related