Apply a Fixed Query Plan to a Plan Guide
Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance
You can apply a fixed query plan to a plan guide of type OBJECT or SQL. Plan guides that apply a fixed query plan are useful when you know about an existing execution plan that performs better than the one selected by the optimizer for a particular query.
The following example creates a plan guide for a simple ad hoc SQL statement. The desired query plan for this statement is provided in the plan guide by specifying the XML Showplan for the query directly in the
@hints parameter. The example first executes the SQL statement to generate a plan in the plan cache. For the purposes of this example, it is assumed that the generated plan is the desired plan and no additional query tuning is required. The XML Showplan for the query is obtained by querying the
sys.dm_exec_text_query_plan dynamic management views and is assigned to the
@xml_showplan variable. The
@xml_showplan variable is then passed to the
sp_create_plan_guide statement in the
@hints parameter. Or, you can create a plan guide from a query plan in the plan cache by using the sp_create_plan_guide_from_handle stored procedure.
USE AdventureWorks2012; GO SELECT City, StateProvinceID, PostalCode FROM Person.Address ORDER BY PostalCode DESC; GO DECLARE @xml_showplan nvarchar(max); SET @xml_showplan = (SELECT query_plan FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st CROSS APPLY sys.dm_exec_text_query_plan(qs.plan_handle, DEFAULT, DEFAULT) AS qp WHERE st.text LIKE N'SELECT City, StateProvinceID, PostalCode FROM Person.Address ORDER BY PostalCode DESC;%'); EXEC sp_create_plan_guide @name = N'Guide1_from_XML_showplan', @stmt = N'SELECT City, StateProvinceID, PostalCode FROM Person.Address ORDER BY PostalCode DESC;', @type = N'SQL', @module_or_batch = NULL, @params = NULL, @hints = @xml_showplan; GO
Submit and view feedback for