Parameter Sniffing and the Recompile Strategy in SQL 2008

By Tom Nonmacher

Welcome to another blog post from SQLSupport.org, your hub for all information about SQL Server. Today, we'll be diving deep into a fascinating topic - Parameter Sniffing and the Recompile Strategy in SQL Server 2008. If you're a database administrator or developer, you're likely familiar with the term "parameter sniffing." However, if you're new to SQL, you may need a bit of a primer. In essence, parameter sniffing is a process where SQL Server's query optimizer uses the parameter values from the first execution of a stored procedure to create an execution plan that is then cached and reused for subsequent executions of the procedure, even when the parameters change.

This optimization technique can significantly improve the performance of your queries. However, it may lead to issues when subsequent queries have drastically different parameter values, resulting in an inefficient execution plan. This is where the recompile strategy comes into play. By instructing SQL Server to recompile the stored procedure every time it's executed, we can ensure that it always uses the most efficient execution plan based on the current parameters.


-- A sample code to use recompile strategy in SQL Server
CREATE PROCEDURE dbo.MyProcedure @Param INT
WITH RECOMPILE AS
BEGIN
    SELECT * FROM dbo.MyTable WHERE Column = @Param
END

The "WITH RECOMPILE" option in the stored procedure forces SQL Server to discard the previously cached execution plan and create a new one each time the procedure is executed. While this ensures an optimal execution plan, it also introduces additional overhead due to the recompilation, so it should be used judiciously.

The concept of parameter sniffing is not limited to Microsoft's SQL Server. Other databases, such as MySQL and DB2, also implement similar optimization mechanisms. For instance, in MySQL 5.6, you can achieve a similar effect by using the SQL_NO_CACHE hint in your queries.


-- A sample code for MySQL 5.6
SELECT SQL_NO_CACHE * FROM MyTable WHERE Column = Param

In DB2 10.5, you can use the REOPT ONCE option in your SQL statements to achieve a similar effect. It instructs DB2 to discard the previously used access plan and create a new one for each execution of the SQL statement.


-- A sample code for DB2 10.5
SELECT * FROM MyTable WHERE Column = Param WITH REOPT ONCE

In the world of cloud databases, Azure SQL Database also provides similar functionality through the OPTION (RECOMPILE) hint. This hint instructs Azure SQL Database to discard the previously cached execution plan and create a new one each time the query is executed.


-- A sample code for Azure SQL
SELECT * FROM MyTable WHERE Column = @Param OPTION (RECOMPILE)

Parameter sniffing and the recompile strategy are powerful tools in the hands of a skilled database administrator. They can significantly improve the performance of your queries when used correctly. However, like any powerful tool, they should be used judiciously and with a thorough understanding of their implications. We hope this post has shed some light on these topics and enabled you to make better use of these features in your database environment.




F07122
Please enter the code from the image above in the box below.