Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Problems with plan guides

Author  Topic 

nwalter
Starting Member

39 Posts

Posted - 2009-05-26 : 16:00:52
I am trying to make a plan guide for a query that keeps being sent from a vendor app that basically pollutes my procedure cache with thousands and thousands of adhoc query plans. Unfortunately there is some odd spacing in the query and it would appear that my plan guide is not working because the difference in spacing.

The original query looks like this:
SELECT TOP 1000 p.PatientID FROM Db.dbo.tblPatient p  WHERE  PatientNumber like '12345678' AND  COALESCE(NonPatient,0) = 0  AND  (DeactivatedDateTime IS NULL) 


my code for creating the plan guide looks like this:
EXEC sp_get_query_template 
N'SELECT TOP 1000 p.PatientID FROM Db.dbo.tblPatient p WHERE PatientNumber like ''12345678'' AND COALESCE(NonPatient,0) = 0 AND (DeactivatedDateTime IS NULL) ',
@stmt OUTPUT,
@params OUTPUT;
EXEC sp_create_plan_guide
N'pg_tblPatient_PatientID',
@stmt,
N'TEMPLATE',
NULL,
@params,
N'OPTION(PARAMETERIZATION FORCED)';


When I create this and then query sys.plan_guides SQL server returns the following for the query text and scope batch:
select top 1000 p . PatientID from Db . dbo . tblPatient p where PatientNumber like '12345678' and coalesce ( NonPatient , @0 ) = @1 and ( DeactivatedDateTime is null )


Why is SQL not taking the query exactly as I have been giving it with all the spacing intact?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-26 : 16:24:15
Probably because of the last space in the string.
The plan cache is stored binary which means all letters count.
Even changing lower case to upper case will mess up the plan guide.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

nwalter
Starting Member

39 Posts

Posted - 2009-05-26 : 16:34:56
Yeah I got that much. I copied the string exactly as it was from both the plan cache and from profiler and pasted it into the template string there, the spaces are included also note the extra spaces before and after the WHERE and AND clauses. But then look at the plan as what was put into the sys.plan_guides, all of those spaces have been stripped out.
Go to Top of Page

mcrowley
Aged Yak Warrior

771 Posts

Posted - 2009-05-27 : 09:43:52
Are the variables parameterized? Or are all of the plans for PatientNumber like ''12345678''?
Go to Top of Page

nwalter
Starting Member

39 Posts

Posted - 2009-05-27 : 12:10:43
They are not parameterized at all. They are all dynamically generated ad-hoc SQL, PatientNumber is inserted into the SQL string by the app before it sends it to the server.
Go to Top of Page

mcrowley
Aged Yak Warrior

771 Posts

Posted - 2009-05-27 : 13:14:51
Then that is why you see the multiple plans in the cache. Any change in the "constants" will generate a new plan. If these are parameterized, you will see a serious reduction in the number of cached plans.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-27 : 13:18:30
And that's what I told him in my first response...
The query string changes because of the different data.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

nwalter
Starting Member

39 Posts

Posted - 2009-05-27 : 16:22:04
And that's not my question nor my problem, that is why I am trying to fix using a plan guide. Do either of you know what a template based plan guide is?

My problem is, when I try to create a plan guide for a query to force paramaterizion for just that query based on a template using sp_get_query_template I end up with a query template that is slighly different from my original query (spaces stripped) and therefore SQL server will never match that incoming query to the plan guide template that it created.

Looking at it again, it also looks like it's not properly parameterizing the string value when it creates the template which is yet another problem.

And yet another problem is it is changing the case of the template. I'm thinking that sp_get_query_template is a little buggy.
Go to Top of Page
   

- Advertisement -