patternsqlMinor
SQL Guide Plan not being used?
Viewed 0 times
sqlusedbeingplanguidenot
Problem
I'm try to get force Parameterization on a simple adhoc SQL query.As explained in this article https://www.simple-talk.com/sql/performance/fixing-cache-bloat-problems-with-guide-plans-and-forced-parameterization/
But even trying to do this with the simplest query I cant get it to work
Plan XML:
Shows a compile and doesn't use the plan guide, is there something i'm missing here? Where am i going wrong?
But even trying to do this with the simplest query I cant get it to work
CREATE TABLE fruit
(
id BIGINT PRIMARY KEY(id)
,title VARCHAR(150)
)
INSERT INTO fruit VALUES ( 1, 'Apple') , ( 2, 'Banana'), ( 3, 'Orange'), ( 4, 'Pear')
DECLARE @params nvarchar(max);
DECLARE @stmt nvarchar(max);
EXEC sp_get_query_template N'SELECT title FROM fruit WHERE id = 4',@stmt OUTPUT, @params OUTPUT;
--SELECT @params
EXEC sp_create_plan_guide
N'fruitGuide',
@stmt,
N'TEMPLATE',
NULL,
@params,
N'OPTION(PARAMETERIZATION FORCED)';
GO
SELECT title FROM fruit WHERE id = 1Plan XML:
Shows a compile and doesn't use the plan guide, is there something i'm missing here? Where am i going wrong?
Solution
Your plan guide is being applied, and the query is being forced parameterized.
Unfortunately, this is not so easy to see in your particular situation, for a couple of reasons:
Reason 1: Post-execution plans do not show plan guide properties
A quick and reliable way to see that your query is using the plan guide is to request a pre-execution (estimated) plan.
So, without running the query, the execution plan shown in SSMS will have a root node with two extra properties (compared with the plan provided in the question):
These properties do not appear in post-execution plans, nor in prepared cached plans.
Side note
In the post-execution (actual) plan you provided, the only (undocumented) indication that parameterization was forced is that the first parameter marker is
Without the plan guide, your test query still qualifies for simple parameterization, but the parameter marker in the tooltip is
Reason 2: You have
With this configuration option enabled, the first time you run the adhoc query, no plan is cached, just a plan stub.
You would need to run the query twice to see the plan stub populated.
There still won't be a full plan (just a root
Notice also that the
Without the
Unfortunately, this is not so easy to see in your particular situation, for a couple of reasons:
Reason 1: Post-execution plans do not show plan guide properties
A quick and reliable way to see that your query is using the plan guide is to request a pre-execution (estimated) plan.
So, without running the query, the execution plan shown in SSMS will have a root node with two extra properties (compared with the plan provided in the question):
These properties do not appear in post-execution plans, nor in prepared cached plans.
Side note
In the post-execution (actual) plan you provided, the only (undocumented) indication that parameterization was forced is that the first parameter marker is
@0 in the Seek tooltip:Without the plan guide, your test query still qualifies for simple parameterization, but the parameter marker in the tooltip is
@1:Reason 2: You have
optimize for ad hoc workloads enabledWith this configuration option enabled, the first time you run the adhoc query, no plan is cached, just a plan stub.
You would need to run the query twice to see the plan stub populated.
There still won't be a full plan (just a root
SELECT node) but this node will have the TemplatePlanGuideDB and TemplatePlanGuideName properties populated:Notice also that the
ParameterizedPlanHandle property points to the plan handle of the fully parameterized (prepared) query plan in cache. The prepared plan does not contain the extra plan guide properties, as mentioned previously.Without the
adhoc option, the adhoc compiled plan will show the important plan guide properties.Context
StackExchange Database Administrators Q#156212, answer score: 7
Revisions (0)
No revisions yet.