title | titleSuffix | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | helpviewer_keywords | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Create and test plan guides |
SQL Server Profiler |
Use SQL Server Profiler to capture exact query text for use in the statement_text argument of the sp_create_plan_guide stored procedure in SQL Server. |
MikeRayMSFT |
mikeray |
03/14/2017 |
sql |
performance |
how-to |
|
[!INCLUDE SQL Server Azure SQL Database] When you are creating a plan guide, you can use [!INCLUDEssSqlProfiler] to capture the exact query text for use in the statement_text argument of the sp_create_plan_guide stored procedure. This helps make sure that the plan guide will be matched to the query at compile time. After the plan guide is created, [!INCLUDEssSqlProfiler] can also be used to test that the plan guide is, in fact, being matched to the query. Generally, you should test plan guides by using [!INCLUDEssSqlProfiler] to verify that your query is being matched to your plan guide.
If you run a query and capture the text exactly as it was submitted to [!INCLUDEssNoVersion] by using [!INCLUDEssSqlProfiler], you can create a plan guide of type SQL or TEMPLATE that will match the query text exactly. This makes sure that the plan guide is used by the query optimizer.
Consider the following query that is submitted by an application as a stand-alone batch:
SELECT COUNT(*) AS c
FROM Sales.SalesOrderHeader AS h
INNER JOIN Sales.SalesOrderDetail AS d
ON h.SalesOrderID = d.SalesOrderID
WHERE h.OrderDate BETWEEN '20000101' and '20050101';
Suppose you want this query to execute using a merge join operation, but SHOWPLAN indicates that the query is not using a merge join. You cannot change the query directly in the application, so instead you create a plan guide to specify that the MERGE JOIN query hint be appended to the query at compile time.
To capture the text of the query exactly as [!INCLUDEssNoVersion] receives it, follow these steps:
-
Start a [!INCLUDEssSqlProfiler] trace, making sure that the SQL:BatchStarting event type is selected.
-
Have the application run the query.
-
Pause the [!INCLUDEssSqlProfiler] Trace.
-
Click the SQL:BatchStarting event that corresponds to the query.
-
Right-click and select Extract Event Data.
[!IMPORTANT]
Do not try to copy the batch text by selecting it from the lower pane of the Profiler trace window. This might cause the plan guide that you create to not match the original batch. -
Save the event data to a file. This is the batch text.
-
Open the batch text file in Notepad and copy the text to the copy and paste buffer.
-
Create the plan guide and paste the copied text inside the quotation marks ('') specified for the @stmt argument. You must escape any single quotation marks in the @stmt argument by preceding them with another single quotation mark. Be careful not to add or remove any other characters when you insert these single quotation marks. For example, the date literal '20000101' must be delimited as ''20000101''.
Here is the plan guide:
EXEC sp_create_plan_guide
@name = N'MyGuide1',
@stmt = N'<paste the text copied from the batch text file here>',
@type = N'SQL',
@module_or_batch = NULL,
@params = NULL,
@hints = N'OPTION (MERGE JOIN)';
To verify that a plan guide is being matched to a query, follow these steps:
-
Start a [!INCLUDEssSqlProfiler] trace, making certain that the Showplan XML event type is selected (located under the Performance node).
-
Have the application run the query.
-
Pause the [!INCLUDEssSqlProfiler] Trace.
-
Find the Showplan XML event for the affected query.
[!NOTE]
The Showplan XML for Query Compile event cannot be used. PlanGuideDB does not exist in that event. -
If the plan guide is of type OBJECT or SQL, verify that the Showplan XML event contains the PlanGuideDB and PlanGuideName attributes for the plan guide that you expected to match the query. Or, in the case of a TEMPLATE plan guide, verify that the Showplan XML event contains the TemplatePlanGuideDB and TemplatePlanGuideName attributes for the expected plan guide. This verifies that the plan guide is working. These attributes are contained under the <StmtSimple> element of the plan.