SQL Server: Query Store and Recompile
Author Grant Fritchey answers a question related to SQL Server 2016's upcoming Query Store update concerning placing OPTION RECOMPILE in a query.
Join the DZone community and get the full member experience.Join For Free
one of the many advantages of sql cruise is the ability to have enough time during a presentation to be able to answer questions from the people there in great detail. one question came up while i was showing the new functionality of query store (available soon in sql server 2016, available right now in azure sql database.)
what happens to plan forcing when you have option recompile on a query?
great question. i have a favorite procedure i use to illustrate the functionality of parameter sniffing:
alter proc dbo.addressbycity @city nvarchar(30) as select a.addressid, a.addressline1, a.addressline2, a.city, sp.name as stateprovincename, a.postalcode from person.address as a join person.stateprovince as sp on a.stateprovinceid = sp.stateprovinceid where a.city = @city;
if this procedure is called with the value of 'mentor' you get an execution plan that looks like this:
if you remove that plan from cache and then call the procedure with the value of 'london' then the plan looks like this:
it’s parameter sniffing in action. one of the cool pieces of functionality that comes with the query store is the ability to choose a plan and make that plan get used, regardless of the parameter values. you do this through the following command:
you pass it a query_id and a plan_id from the information in the query store and now that plan will get used. but, what happens when you have attempted to fix your query using a query hint, such as recompile? let’s modify our procedure:
alter proc dbo.spaddressbycity @city nvarchar(30) as select a.addressid, a.addressline1, a.addressline2, a.city, sp.name as stateprovincename, a.postalcode from person.address as a join person.stateprovince as sp on a.stateprovinceid = sp.stateprovinceid where a.city = @city option (recompile);
if i run the procedure, regardless of the parameter values passed in, i get a new plan for each value. nothing is stored in cache, but, it is stored in the query store.
let’s pull the query_id and plan_id out of query store for this procedure:
select qsq.query_id, qsp.plan_id, cast(qsp.query_plan as xml) as sqlplan from sys.query_store_query as qsq join sys.query_store_plan as qsp on qsp.query_id = qsq.query_id where qsq.object_id = object_id('dbo.spaddressbycity');
currently, on my system, this brings back four rows, two distinct query_id and four different plan_id. this is because, when i modified the text of the query within the procedure, i kept the object_id the same, but, the query text is different (the recompile hint). this means a different record in the query store. we’ll use the newer query_id and pick one of the plans to force it:
exec sys.sp_query_store_force_plan 42, 44;
now, no matter what value i pass it, the recompile hint is effectively ignored. i have the same plan every time (in my case, the london plan.) ah, but is the recompile hint ignored? in fact, no, it isn’t. if i capture the extended event sql_statement_recompile, i can see every single time i execute this procedure that it’s going through a full recompile… which is then tossed and the plan i chose is forced upon the optimizer. every time.
this isn’t a revelation. it makes sense. however, the query store represents one more thing that we have to think through.
if you want to talk query tuning, execution plans, the query store, and more, i’ll be presenting an all day pre-conference seminar at sqlday in wroclaw poland on may 16th, 2016. let’s talk.
Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.