programming4us
programming4us
DATABASE

SQL Server 2008 : Managing Query Performance - Forcing a Specific Execution Plan

2/19/2011 3:59:05 PM
Plan forcing was introduced in SQL Server 2005 to provide a way for you to supply an entire execution plan that SQL Server will use to execute a query. Plan forcing is useful when the query optimizer will not produce an optimal execution plan for a query, even though you know a better execution plan is available. If you have a query that performed better prior to an upgrade, you may be able to capture the old execution plan and force the optimizer to use it. You can supply the execution plan in XML format following a query by supplying the USE PLAN option, as shown in Listing 1.
Example 1. Syntax Used to Specify an Execution Plan by Supplying the USE PLAN Query Hint
SELECT *
FROM Table1
JOIN Table2
ON Table1.Column = Table2.Column
OPTION (USE PLAN 'N
<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
...
</ShowPlanXML>')


In order to supply the execution plan, you must first capture it in XML format. There are a few ways you can capture the XML execution plan to supply the query.

  • You can use the SHOWPLAN_XML SET statement to return the XML execution plan instead of running the query, as shown in the following code:

    SET SHOWPLAN_XML ON
    GO

    SELECT * FROM Table
    SET SHOWPLAN_XML OFF
    GO

  • You can use the STATISTICS XML SET statement to return the XML execution plan following the query result set, as shown in the following code:

    SET STATISTICS XML ON
    GO

    SELECT * FROM Table
    SET STATISTICS XML OFF
    GO

  • You can use the query_plan column in the sys.dm_exec_query_plan Dynamic Management Function, as shown in the following query:

    SELECT est.text, eqp.query_plan
    FROM sys.dm_exec_cached_plans ecp
    CROSS APPLY sys.dm_exec_query_plan(ecp.plan_handle) eqp
    CROSS APPLY sys.dm_exec_sql_text(ecp.plan_handle) est

  • You can use the Showplan XML, Showplan XML Statistics Profile, and Showplan XML For Query Compile event classes in SQL Server Profiler.

  • You can use the Display Estimated Execution Plan and Include Actual Execution Plan options in SQL Server Management Studio; right-click the execution plan, and select Show Execution Plan XML from the context menu.

If the XML execution plan is invalid in any way, the query will fail. Certain underlying database schema changes may cause the execution plan to become invalid. Therefore, it is extremely important to test any queries that have the USE PLAN query hint after making database changes. Just as with any option you use to override the query optimizer, you should apply the USE PLAN query hint with extreme caution, and then only after you have exhausted your other options, such as creating proper indexes and updating statistics. Since the USE PLAN query hint forces a query to utilize a specified execution plan, the query optimizer will no longer be able to dynamically adjust to changes within the database.

Other  
 
video
 
Video tutorials
- How To Install Windows 8

- How To Install Windows Server 2012

- How To Install Windows Server 2012 On VirtualBox

- How To Disable Windows 8 Metro UI

- How To Install Windows Store Apps From Windows 8 Classic Desktop

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010
programming4us programming4us
programming4us
 
 
programming4us