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.