Plan Guides
Plan guides let you optimize the performance of queries when you cannot or do not want to directly change the text of the actual query in SQL Server 2012. Plan guides influence the optimization of queries by attaching query hints or a fixed query plan to them. Plan guides can be useful when a small subset of queries in a database application provided by a third-party vendor are not performing as expected. In the plan guide, you specify the Transact-SQL statement that you want optimized and either an OPTION clause that contains the query hints you want to use or a specific query plan you want to use to optimize the query. When the query executes, SQL Server matches the Transact-SQL statement to the plan guide and attaches the OPTION clause to the query at run time or uses the specified query plan.
The total number of plan guides you can create is limited only by available system resources. Nevertheless, plan guides should be limited to mission-critical queries that are targeted for improved or stabilized performance. Plan guides should not be used to influence most of the query load of a deployed application.
Note
|
|---|
|
Plan guides cannot be used in every edition of Microsoft SQL Server. For a list of features that are supported by the editions of SQL Server, see Features Supported by the Editions of SQL Server 2012. Plan guides are visible in any edition. You can also attach a database that contains plan guides to any edition. Plan guides remain intact when you restore or attach a database to an upgraded version of SQL Server. |
Plan guides are scoped to the database in which they are created. Therefore, only plan guides that are in the database that is current when a query executes can be matched to the query. For example, if AdventureWorks2012 is the current database and the following query executes:
SELECT FirstName, LastName FROM Person.Person;
Only plan guides in the AdventureWorks2012 database are eligible to be matched to this query. However, if AdventureWorks2012 is the current database and the following statements are run:
USE DB1;
SELECT FirstName, LastName FROM Person.Person;
Only plan guides in DB1 are eligible to be matched to the query because the query is executing in the context of DB1.
For SQL- or TEMPLATE-based plan guides, SQL Server matches the values for the @module_or_batch and @params arguments to a query by comparing the two values character by character. This means you must provide the text exactly as SQL Server receives it in the actual batch.
When @type = 'SQL' and @module_or_batch is set to NULL, the value of @module_or_batch is set to the value of @stmt. This means that the value for statement_text must be provided in the identical format, character-for-character, as it is submitted to SQL Server. No internal conversion is performed to facilitate this match.
When both a regular (SQL or OBJECT) plan guide and a TEMPLATE plan guide can apply to a statement, only the regular plan guide will be used.
Note
|
|---|
|
The batch that contains the statement on which you want to create a plan guide cannot contain a USE database statement. |
Creating a plan guide on a module removes the query plan for that module from the plan cache. Creating a plan guide of type OBJECT or SQL on a batch removes the query plan for a batch that has the same hash value. Creating a plan guide of type TEMPLATE removes all single-statement batches from the plan cache within that database.
|
Task |
Topic |
|---|---|
|
Describes how to create a plan guide. |
|
|
Describes how to create a plan guide for parameterized queries. |
|
|
Describes how to control query parameterization behavior by using plan guides. |
Specify Query Parameterization Behavior by Using Plan Guides |
|
Describes how to include a fixed query plan in a plan guide. |
|
|
Describes how to specify query hints in a plan guide. |
|
|
Describes how to view plan guide properties. |
|
|
Describes how to use SQL Server Profiler to create and test plan guides. |
|
|
Describes how to validate plan guides. |
Note