Dynamic management views
(DMVs) can return server state information that can be used to monitor
and diagnose database engine issues and help tune performance. The sys.dm_exec_query_plan
DMV returns the showplan information for a T-SQL batch whose query
execution plan resides in the plan cache. This can be any SQL batch, not
just the batch executed by the current user session. The sys.dm_exec_query_plan
DMV also provides the capability to retrieve the execution plan for
currently long-running processes to help diagnose why they may be
running slowly.
The showplan information provided by sys.dm_exec_query_plan is returned in a column called query_plan, which is of the xml data type. This column provides the same information as SET SHOWPLAN XML. The syntax of sys.dm_exec_query_plan is
sys.dm_exec_query_plan ( plan_handle )
In SQL Server 2008, the query plans for various types of T-SQL batches are cached in an area of memory called the plan cache. Each cached query plan is identified by a unique identifier called a plan handle. To view the showplan for one of these batches, you need to provide the plan handle for the batch to the sys.dm_exec_query_plan DMV.
The tricky part about using sys.dm_exec_query_plan
is determining the plan handle to use. First, you need to determine the
SPID for the process with the long-running query. This is usually
accomplished using sp_who2 or via the SSMS Activity Monitor.
When you have the SPID, you can use the sys.dm_exec_requests DMV to obtain the plan handle (assume in this case that the SPID is 58):
select plan_handle from sys.dm_exec_requests where session_id = 58
go
plan_handle
----------------------------------------------------------------------------
0x06000A00E96E6D2CB8A1F505000000000000000000000000
When you have the plan handle, you can pass it on to the sys.dm_exec_query_plan DMV to return the query plan:
SELECT query_plan
FROM sys.dm_exec_query_plan (0x06000A00E96E6D2CB8A1F505000000000000000000000000)
Alternatively, to prevent having to copy and paste the plan handle from the sys.dm_exec_requests query into the query against sys.dm_exec_query_plan, you can use the CROSS APPLY clause, as in the following query:
SELECT query_plan FROM sys.dm_exec_requests cp
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle)
where cp.session_id = 58
If you return the results to grid, you can right-click the data in the query_plan
column and save it to a file, which can then be loaded into SSMS to
view the graphical execution plan, just like the output from the SET SHOWPLAN_XML option.
To return the query plan for all currently running T-SQL batches, you can run the following:
SELECT query_plan FROM sys.dm_exec_requests cp
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle)
In addition to returning the query plans for the currently running T-SQL batches, SQL Server 2008 also provides the sys.dm_exec_query_stats and sys.dm_exec_cached_plans DMVs. The sys.dm_exec_cached_plans
DMV can be used to return information about all query plans currently
residing in the plan cache. For example, to retrieve a snapshot of all
query plans residing in the plan cache, you use the CROSS APPLY operator to pass the plan handles from sys.dm_exec_cached_plans to sys.dm_exec_query_plan, as follows:
SELECT * FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle)
To retrieve a snapshot of all
query plans that currently reside in the plan cache for which the server
has gathered statistics, use the CROSS APPLY operator to pass the plan handles from sys.dm_exec_query_stats to sys.dm_exec_query_plan as follows:
SELECT * FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle)
Because sys.dm_exec_query_plan provides the capability to view the query plan for any session, a user must be a member of the sysadmin fixed server role or have the VIEW SERVER STATE permission on the server to invoke it.
Note
The SET SHOWPLAN_ALL and SET SHOWPLAN_TEXT
options are deprecated features and may be removed in a future version
of SQL Server. It is recommended that you switch to using the SET SHOWPLAN_XML option instead.