IT tutorials
 
Database
 

SQL Server 2008 R2 : Query Analysis - Using sys.dm_exec_query_plan

12/5/2012 5:46:58 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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.

 
Others
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us