444.Plan guide

SQL Server 2005 introduces the sp_create_plan_guide system procedure for creating plan guide to optimize the performance of queries. This procedure can be used when you cannot or do not want to directly change the text of the query.
Plan guide influence optimization of queries by attaching query hints to them. In the sp_create_plan_guide statement, you specify the query that you want to optimize and the OPTION clause that contains the query hints you want to use to optimize the query. When the query executes, SQL Server matches the query to the plan guide and attached the OPTION clause to the query at run time.

Queries than can benefit from plan guides are generally parameter-based, and may be performing poorly because they use cached query plans whose parameter values do not represent a worst-case or most representative scenario. The OPTMIZE FOR and RECOMPILE query hints can be used to address this problem.
OPTMIZE FRO instructs SQL Server to use a particular value for parameter when the query is optimized. RECOMPILE instructs the server to discard a query plan after execution, forcing the query optimizer to recompile a new query plan the next time that the same query is executed.
Another common hit to use with plan guides is the USE PLAN query hint. This query hint applies when you are already aware of an existing execution plan that can be substituted for the one selected by the optimizer for a particular query because you know it perform better. USE PLAN forces SQL Server to use a particular query plan, specified explicitly in the hint syntax, when executing the query.
 Plan guides can be created to match queries that are executed in the following contexts:
OBJECT plan guides match queries that execute in the context of T-SQL stored procedures, multistatement table-valued functions, and DML triggers.
 SQL plan guides match queries that execute in the context of stand-along T-SQL statements and batches that are not part of a database object.
 TEMPLATE plan guides match stand-along queries that parameterize to a specified form. These plan are used to override the current PARAMATERIZATION database SET option of a database for a class of queries.

Code
原文地址:https://www.cnblogs.com/yang_sy/p/1459959.html