
Plan Guide Matching Requirements
Plan guides are scoped to the database in which they are created. Therefore, only plan guides that exist in the database that is current when a query executes can be matched to the query. For example, if AdventureWorks is the current database and the following query executes:
SELECT * FROM Person.Contact;
Only plan guides in the AdventureWorks database are eligible to be matched to this query.
However, if AdventureWorks is the current database and the following statements are run:
USE DB1;
GO
SELECT * FROM Person.Contact;
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 exactly the same format, character-for-character, as it is submitted to SQL Server. No internal conversion is performed to facilitate this match.
Generally, you should test plan guides using SQL Server Profiler to verify that your query is being matched to your plan guide. Testing SQL- or TEMPLATE-based plan guides by running batches from the SQL Server Management Studio may give unexpected results. For more information, see Using SQL Server Profiler to Create and Test Plan Guides.
Note: |
|---|
|
The batch that contains the statement on which you want to create a plan guide cannot contain a USE database statement.
|