Query Store hints is another amazing feature added and is available in the preview mode in Azure SQL Database, managed instances, elastic pools and hyperscale databases as well. Query Store hints are just like the hints you use in your queries but the difference is you do not have to change the query code to apply these hints using Query store specifically designed stored procedure- sys.sp_query_store_set_hints. Cool, right?
Query Store hints are very useful when you need to use the hints and force the optimizer to use those hints but without changing the underneath query. This feature is used for specific queries in some circumstances. Some of the examples that can take the advantage of this feature are the parameter sensitive queries where recompile is needed to create an optimal plan each time it executes, set the MAXDOP value, memory grant size setting for bulk operations, etc. (Source: Microsoft Docs)
You also have the choice to choose the hints like forcing the legacy cardinality estimation for the queries not performing well on the newer versions of SQL Server, using different compatibility level for a particular query as the database compatibility level is another.
For the complete list of hints that are supported and not supported, please see it here.
Now, let’s see an example of a stored procedure where we can apply the Query Store hints. This stored procedure have sensitive parameters and we need to use recompile to make sure it generates the optimal plan for each execution. I am using Azure SQL Database Adventureworks for this demo. Query store is already enabled on this database by default. Let’s create the stored procedure.
IF EXISTS ( SELECT * FROM sys.objects WHERE type = 'P' AND name = 'Salesinformation' ) DROP PROCEDURE dbo.Salesinformation GO CREATE PROCEDURE dbo.Salesinformation @productID [int] AS BEGIN SELECT [SalesOrderID] ,[ProductID] ,[OrderQty] FROM [Sales].[SalesOrderDetailEnlarged] WHERE [ProductID] = @productID; END;
Let me clear out the Query Store data and plan cache before running the test queries.
ALTER DATABASE [AdventureWorks] SET QUERY_STORE CLEAR; /* Clean the procedure cache */ ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE
Execute the stored procedure with two different parameters (the first parameter results around 30K rowsand the second parameter results 380 rows)
/* Executing the sp resulting in many rows with this parameter */ EXEC dbo.Salesinformation 870 /* This parameter will result in only couple of rows, it reuses the same execution plan */ EXEC dbo.Salesinformation 942
If you observe the execution plan for both the executions, the parameter 942 only results in 380 rows but it is doing the clustered index scan, which is not optimal in this scenario.
To fix this usually, we will use recompile as a runtime hint or in the stored procedure header. With the Query Store hints, all we need to get is the query id and the hint information we want to apply. This is done in two steps. First, grab the query id by querying the system catalog views and filtering on the query text by using the like operator.
/* Find the query ID associated with the query Source: https://docs.microsoft.com/en-us/sql/relational-databases/performance/query-store-hints?view=azuresqldb-current */ SELECT query_sql_text ,q.query_id FROM sys.query_store_query_text qt INNER JOIN sys.query_store_query q ON qt.query_text_id = q.query_text_id WHERE query_sql_text LIKE N'%@productID int%' AND query_sql_text NOT LIKE N'%query_store%'; GO
The query_id is 1 for this stored procedure. Now, we can use the stored procedure sp_query_store_set_hints and pass the parameter values query_id and the hint (recompile) in this case to fix the problem.
/* Set the Query hint by using the sp sp_query_store_set_hints Source: https://docs.microsoft.com/en-us/sql/relational-databases/performance/query-store-hints?view=azuresqldb-current */ EXEC sp_query_store_set_hints @query_id = 1 ,@value = N'OPTION(RECOMPILE)'; GO
Now, execute the stored procedure again with the same parameters and see if the recompile occured.
/* Execute the sp again with two parameters */ EXEC dbo.Salesinformation 870 EXEC dbo.Salesinformation 942
The stored procedure is recompiled as the query hint has been applied and you can see the difference in the execution plans. For the parameter value with 380 records, it recompiled to create an execution plan using an index seek and a key lookup which is optimal for this execution.
To check for the query hints that are currently enabled, run the below query using the catalog views
/* Check for the Query hints that are enabled Source: https://docs.microsoft.com/en-us/sql/relational-databases/performance/query-store-hints?view=azuresqldb-current*/ SELECT query_hint_id ,query_id ,query_hint_text ,last_query_hint_failure_reason ,last_query_hint_failure_reason_desc ,query_hint_failure_count ,source ,source_desc FROM sys.query_store_query_hints; GO
The query_hint_id is shown as 1. As you update the hints on this query_id, this number increases. The query_hint_text always shows the currently active hint that has been applied. The history of the query_hints has not been displayed.
If you would like to use the MAXDOP hint set to 1 for the parallelism used in the execution plan. Update the hint to MAXDOP by running the stored procedure below. Run the stored procedure again with the parameters to see the hint being applied.
/* MAXDOP HINT */ EXEC sp_query_store_set_hints @query_id = 1 ,@value = N'OPTION(MAXDOP 1)'; GO /* Clean the procedure cache */ ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE EXEC dbo.Salesinformation 870 EXEC dbo.Salesinformation 942
If you observe the execution plans, the hint has been updated to MAXDOP. If you observe, the “NonParallelPlanReason” is shown as MAXDOPsetToOne and the recompile hint that has been previously applied is not active anymore. If you check the query store hints active list by using the system catalog views, updated MAXDOP hint is only shown but the Query_hint_id is being updated to 2.
We can use multiple query store hints in the same stored procedure like below and execute the stored procedure again with two parameters.
/* Multiple query hints */ EXEC sp_query_store_set_hints @query_id = 1 ,@value = N'OPTION(RECOMPILE,MAXDOP 1)'; GO /* Clean the procedure cache */ ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE EXEC dbo.Salesinformation 870 EXEC dbo.Salesinformation 942
If you observe the plan and the details, both the recompile and MAXDOP query hints have been applied together. If you check the status of the query hints, this time the number of the Query_hint_id will be 3.
To remove the query hints that have been active on the query_id, use sp_query_store_clear_hints by providing the query_id to remove the Query Store hints on specific query_id.
/* Remove the Query hints from Query Store */ EXEC sp_query_store_clear_hints @query_id = 1; GO
To make sure the Query Store hints are disabled on the query_id, recheck by running the below query:
/* Recheck for the Query hints to make sure Query hints are diabled */ SELECT query_hint_id ,query_id ,query_hint_text ,last_query_hint_failure_reason ,last_query_hint_failure_reason_desc ,query_hint_failure_count ,source ,source_desc FROM sys.query_store_query_hints; GO
In this blog post, we have only tested some of the Query Store hints that are supported. There are many other hints that are supported and do not forget to read the entire blog post here from Microsoft to know the details of the hints that are being supported and not supported.
In this post, we have seen what Query Store hints are and tried a couple of examples to see how the preview mode feature works. If you have a need to use any of the supported hints in your queries but are unable to edit and modify the text of the query because of any reasons like the third-party applications, upgraded the database to newer SQL Servers but have to use the older cardinality estimation on any particular query not performing well or use older compatibility level for any particular query without interfering with the database level compatibility, Query store hints can be very useful.
Thanks for reading!
4 thoughts on “Query Store Fundamentals: Query Store Hints”
Your example is exactly the situation Microsoft talk about in SQL2022 trying to fix. It just shows how difficult it can be on an active system to make queries work effectively.
Thanks for your views on this Chris. Would love to see what SQL Server 2022 is bringing for us with this Query Store.