| title | sys.dm_exec_query_statistics_xml (Transact-SQL) | ||||
|---|---|---|---|---|---|
| description | sys.dm_exec_query_statistics_xml (Transact-SQL) | ||||
| author | rwestMSFT | ||||
| ms.author | randolphwest | ||||
| ms.date | 11/16/2016 | ||||
| ms.service | sql | ||||
| ms.subservice | system-objects | ||||
| ms.topic | conceptual | ||||
| f1_keywords |
|
||||
| helpviewer_keywords |
|
||||
| dev_langs |
|
||||
| ms.assetid | fdc7659e-df41-488e-b2b5-0d79734dfecb |
[!INCLUDE sqlserver2016-asdb-asdbmi]
Returns query execution plan for in-flight requests. Use this DMV to retrieve showplan XML with transient statistics.
sys.dm_exec_query_statistics_xml(session_id)
session_id
Is the session id executing the batch to be looked up. session_id is smallint. session_id can be obtained from the following dynamic management objects:
| Column Name | Data Type | Description |
|---|---|---|
| session_id | smallint | ID of the session. Not nullable. |
| request_id | int | ID of the request. Not nullable. |
| sql_handle | varbinary(64) | Is a token that uniquely identifies the batch or stored procedure that the query is part of. Nullable. |
| plan_handle | varbinary(64) | Is a token that uniquely identifies a query execution plan for a batch that is currently executing. Nullable. |
| query_plan | xml | Contains the runtime Showplan representation of the query execution plan that is specified with plan_handle containing partial statistics. The Showplan is in XML format. One plan is generated for each batch that contains, for example ad hoc [!INCLUDEtsql] statements, stored procedure calls, and user-defined function calls. Nullable. |
This system function is available starting with [!INCLUDEsssql16-md] SP1. See KB 3190871
This system function works under both standard and lightweight query execution statistics profiling infrastructure. For more information, see Query Profiling Infrastructure.
Under the following conditions, no Showplan output is returned in the query_plan column of the returned table for sys.dm_exec_query_statistics_xml:
- If the query plan that corresponds to the specified session_id is no longer executing, the query_plan column of the returned table is null. For example, this condition may occur if there is a time delay between when the plan handle was captured and when it was used with sys.dm_exec_query_statistics_xml.
Due to a limitation in the number of nested levels allowed in the xml data type, sys.dm_exec_query_statistics_xml cannot return query plans that meet or exceed 128 levels of nested elements. In earlier versions of [!INCLUDEssNoVersion], this condition prevented the query plan from returning and generates error 6335. In [!INCLUDEssVersion2005] Service Pack 2 and later versions, the query_plan column returns NULL.
On [!INCLUDEssNoVersion], requires VIEW SERVER STATE permission on the server.
On [!INCLUDEssSDS] Premium Tiers, requires the VIEW DATABASE STATE permission in the database. On [!INCLUDEssSDS] Standard and Basic Tiers, requires the Server admin or an Azure Active Directory admin account.
The following example queries sys.dm_exec_requests to find the interesting query and copy its session_id from the output.
SELECT * FROM sys.dm_exec_requests;
GO Then, to obtain the live query plan and execution statistics, use the copied session_id with system function sys.dm_exec_query_statistics_xml.
--Run this in a different session than the session in which your query is running.
SELECT * FROM sys.dm_exec_query_statistics_xml(< copied session_id >);
GO Or combined for all running requests.
--Run this in a different session than the session in which your query is running.
SELECT * FROM sys.dm_exec_requests
CROSS APPLY sys.dm_exec_query_statistics_xml(session_id);
GO Trace Flags
Dynamic Management Views and Functions (Transact-SQL)
Database Related Dynamic Management Views (Transact-SQL)