Query Store, Force Plan, and Dropped Objects
Check out the ability to force your plans through, courtesy of the Query Store. See what it offers your databases and learn what to beware as you implement it.
Join the DZone community and get the full member experience.Join For Free
i love the query store. seriously. it’s a huge leap forward in the capabilities of azure sql database and sql server in support of performance monitoring and query optimization. one of my favorite aspects of the query store is the ability to force plans. frankly though, it’s also the scariest part of the query store. i do believe that plan forcing will be one of the most ill-used functions in sql server since the multi-statement table-valued user-defined function (don’t get me started). however, unlike the udf, this ill-use will be because of poor understanding on the part of the user, not a fundamental design issue. no, plan forcing and the query store are very well-constructed. let me give you an example of just how well constructed they are.
let’s imagine that have a situation such as bad parameter sniffing, where you’ve determined that from the more than one possible execution plans against a table that there is a preferred plan. enabling plan forcing to ensure that plan gets used is a no-brainer. let’s further imagine that you have a junior dba who is… let’s just say overly aggressive in their duties. they do silly things occasionally. what happens when your pretty plan, which uses a particular index, meets your junior dba who just dropped that index?
here’s the setup. we’re using the wideworldimporters database and we have this query:
select * from warehouse.stockitemtransactions as sit where sit.transactionoccurredwhen between '9/9/2015' and '9/11/2015';
this query, with the default configuration, will scan the existing table, so i’ll add an index:
create index transactionoccurredwhennci on warehouse.stockitemtransactions (transactionoccurredwhen);
for a limited range such as the one i’m passing above, i’ll get a plan with a key lookup operation, which runs faster than the scan, so i’m happy. for a broader range, i’m likely to see a scan again, but because most of my queries have a very narrow range, i’d sure like to be able to force the plan to always compile to the seek and key lookup. to do this i need to find the query_id and plan_id from the query store (assuming i’m not using the gui):
select qsp.plan_id, qsp.query_id, qsqt.query_sql_text, qsp.count_compiles from sys.query_store_plan as qsp join sys.query_store_query as qsq on qsq.query_id = qsp.query_id join sys.query_store_query_text as qsqt on qsqt.query_text_id = qsq.query_text_id where qsqt.query_sql_text like 'select * from warehouse.stockitemtransactions as sit%';
with those values, i can force the execution plan so that it will always use the plan i want:
exec sys.sp_query_store_force_plan 42460,463;
that’s it. i’m happy because i’m going to see the execution plan used over and over, despite any values passed during a recompile.
along comes our aggressive junior dba, who decides that there are “too many” indexes on the server. no, i don’t know what that means either, but they evidently read it on the internet or something so they drop the index we created before:
drop index transactionoccurredwhennci on warehouse.stockitemtransactions;
what now happens to our lovely execution plan and the plan forcing? we’ll take a look at two events in extended events, sql_statement_recompile and query_store_plan_forcing_failed. nothing happens immediately on dropping the index. the plans associated with that object, if any, are marked as invalid in the cache. the next time we call the query it’s going to recompile and we can see the event:
the most important part of the event is the recompile_cause, which is “schema changed.” however, i would also note the attach_activity_id.guid. i’ve chosen to enable causality tracking in this extended event session. this will cause all events associated with a common activity to get a guid and then a sequence. this is interesting because, after the recompile event, we get the query_store_plan_forcing_failed event:
the guid value is the same as the event above and the *.seq number is now 2, showing that, for these events, the recompile event occurred and then this event occurred. that makes perfect sense. the plan is marked for recompile, so, it’s going to be recompiled. i have enabled plan forcing though, so i have a particular plan that i want the optimizer to use. however, thanks to my “helpful” junior dba, the plan is now invalid. you even get the description of what happened in the message field for the event:
index ‘wideworldimporters.warehouse.stockitemtransactions.transactionoccurredwhennci’, specified in the use plan hint, does not exist. specify an existing index, or create an index with the specified name.
the first question now is, what happens with this query and the execution plan? is the new plan generated now that the index is missing get stored in cache? yes, it does. we can validate that by querying the cache, or, when capturing the actual execution plan, checking the “retrieved from cache” property.
because plan forcing is enabled, do we see a recompile every time this query is called? the answer to that question is slightly complex. under normal circumstances, no. as long as that plan remains in cache, it’s simply reused. no other recompiles occur. a normal recompile event will cause another attempt at applying the invalid execution plan and we would see yet another query_store_plan_forcing_failed event for each recompile on the query. however, during testing, joey d’antoni (who was helping me play with this when we discussed what would happen when a plan was made invalid) had severe memory pressure on his server. he saw intermittent recompiles with a cause message that said plan forcing had failed. so if your server is under extreme stress and you cause this issue, you might see different messages. just remember, the cause of the recompiles was not the plan forcing, but the memory pressure.
the fun thing is, as long as i don’t remove the plan forcing or take the query and plan out of the query store manually, if i recreate the index on my table with the same name and definition as that expected by the plan, the query store will simply reapply the plan and then successfully force it during any subsequent recompile situation. this is because query store is persisted with the database and barring outside activity, the information there will remain, just like the rest of the data in the database.
all of this means that query store works exactly the way we would expect, not forcing additional recompiles when you, or your junior dba, inadvertently invalidate a plan. it also works as expected in that forcing a plan is stored with your database so that, assuming you don’t remove that plan from the query store, it will simply be reapplied after you fix the problem. it’s fun to see the thought that went behind the design of the behavior of query store. however, please, use plan forcing judiciously.
Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.