SQL SERVER – Active Parallel Requests and Cached Parallel Query History

I Identify parallel running requests separately from parallel queries recorded in the cache. Those lists answer different performance questions.

Active parallel loom threads are inspected separately from preserved guides and worn tools.

During a performance health check, a client reported a slowdown without an obvious cause. Parallel queries were part of the investigation. We did not want to make every query serial by changing the server-wide setting.

Find parallel requests running now

SELECT TOP (10) r.session_id, r.request_id, DB_NAME(r.database_id) AS database_name,
       r.status, r.dop, r.parallel_worker_count, r.cpu_time, r.total_elapsed_time,
       SUBSTRING(t.text, r.statement_start_offset / 2 + 1,
         (CASE WHEN r.statement_end_offset = -1 THEN DATALENGTH(t.text)
               ELSE r.statement_end_offset END - r.statement_start_offset) / 2 + 1) AS statement_text
FROM sys.dm_exec_requests AS r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
WHERE r.session_id <> @@SPID AND r.dop > 1
ORDER BY r.cpu_time DESC, r.session_id, r.request_id;

This SQL Server 2016-and-later query shows requests with a degree of parallelism greater than one. CPU and elapsed time are milliseconds for the current request. A quiet instance can return no rows. That is a valid observation, not a failed demonstration.

Find frequently executed parallel cache entries

SELECT TOP (10) qs.execution_count, qs.last_execution_time, qs.last_dop,
       qs.last_elapsed_time, qs.last_logical_reads, qs.last_logical_writes,
       qs.last_rows, qs.plan_handle, t.text, p.query_plan
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS t
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS p
WHERE qs.last_dop > 1
ORDER BY qs.execution_count DESC, qs.last_execution_time DESC
OPTION (MAXDOP 1);

The original script inspected cached plans and execution statistics. That is history since each statement was compiled, not a list of work running now. last_dop describes the last execution, and last_elapsed_time uses microseconds.

Entries disappear when plans leave the cache. Multiple statements in a batch can have separate statistics. Use statement offsets when narrowing the batch text to one statement, and use Query Store when retained history is required.

Permission requirements depend on the server version. SQL Server 2022 and later use VIEW SERVER PERFORMANCE STATE for these server-level performance DMVs. Query text can contain business data, so share the output through the approved process.

Related reading

A cached execution is not a request running now, it is historical work retained with its cache entry.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Parallel, SQL CPU, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Using Query Hint ENABLE_PARALLEL_PLAN_PREFERENCE
Next Post
In-Memory OLTP Migration Checklist: Which Features Block You

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.