SQL Server 2022: Parameter Sensitive Plan Optimization

By Tom Nonmacher

SQL Server 2022 is here and it brings a host of powerful features, one of which is Parameter Sensitive Plan Optimization. This new feature assures the best possible query performance for your server by choosing the optimal execution plan based on the parameters provided. In SQL Server 2019, the plan chosen by the Query Optimizer could sometimes lead to suboptimal performance. However, SQL Server 2022 has overcome this issue with Parameter Sensitive Plan Optimization, which adjusts the execution plan based on the provided parameters.

This post will focus on how you can leverage Parameter Sensitive Plan Optimization in SQL Server 2022 to ensure optimal performance. Let's begin with T-SQL, the language of SQL Server. Suppose we have a stored procedure with a parameter that significantly impacts the choice of execution plan.

CREATE PROCEDURE FetchOrders (@OrderType int)
AS
SELECT * FROM Orders WHERE OrderType = @OrderType

In SQL Server 2019, the Query Optimizer might choose a plan that's optimal for one parameter value but not for others. But with SQL Server 2022, the Query Optimizer will choose an optimal plan based on the parameter value, leading to better performance.

Similar functionality can also be found in other database systems like MySQL 8.0 and DB2 11.5. In MySQL, you can use the optimizer hints to instruct the optimizer to choose a specific execution plan. Here's an example:

SELECT /*+ SET_VAR(optimizer_switch = 'index_merge_intersection=off') */
* FROM Orders WHERE OrderType = ?

In DB2 11.5, you can use the REOPT ONCE option to tell DB2 to choose the optimal plan based on the current parameter value. Here's how you can do it:

--#SET TERMINATOR @
CREATE PROCEDURE FetchOrders (IN OrderType int)
LANGUAGE SQL
BEGIN
DECLARE @sql VARCHAR(1000);
SET @sql = 'SELECT * FROM Orders WHERE OrderType = ? WITH REOPT ONCE';
PREPARE stmt FROM @sql;
EXECUTE stmt USING OrderType;
END @

Microsoft's cloud database services, Azure SQL and Azure Synapse, also support Parameter Sensitive Plan Optimization. In Azure SQL, you can use the OPTIMIZE FOR option to tell the Query Optimizer to optimize for a specific parameter value. In Azure Synapse, you can use the OPTIMIZE FOR UNKNOWN option to tell the Query Optimizer to choose the optimal plan based on the parameter value at runtime.

In conclusion, Parameter Sensitive Plan Optimization is a powerful feature that can significantly improve the performance of your SQL Server 2022 database. By allowing the Query Optimizer to choose the optimal execution plan based on the provided parameters, you can ensure that your queries run as efficiently as possible. So, make sure to leverage this feature in your SQL Server 2022 database.




66A9A7
Please enter the code from the image above in the box below.