What is Parameter Sniffing?
Parameter Sniffing
SQL Server creates an optimal plan for a stored procedure by using the parameters that are passed the first time to the stored procedure is executed is called Parameter Sniffing.
SQL Server does this exercise to build the execution plan once and use it multiple times without recreating the execution plan again and again. The compilation process of the stored procedure can take some precious time for the execution of the procedure is very large. This entire exercise is done to save those repeated moments recompiling the stored procedures.
While this works out great in most of the cases, there is often a negative impact of the parameter sniffing issue for the stored procedure.
Let us see a very simple example. To begin with, let us create the following stored procedure.
2 3 4 5 6 | CREATE OR ALTER PROC GetCustomerOrders (@CustomerID INT ) AS SELECT * FROM WideWorldImporters.Sales.Orders WHERE CustomerID = @CustomerID GO |
Now let us enable the actual execution plan for your query so we can, later on, inspect the execution plans.
First, run the stored procedure with the first value.
1 | EXEC GetCustomerOrders 1060 |
Let us observe the execution plan. You will notice in the execution plan we see operators reading 4 rows out of potential 4 rows.
Next, run the stored procedure with a different value.
1 | EXEC GetCustomerOrders 90 |
Let us observe the execution plan. You will notice in the execution plan that the operator is reading 150 rows out of potential 4 rows.
Now let us look at the property of the execution plan by right-clicking on the left-most SELECT operator in the SSMS.
Pay attention to the parameter list. You will see here there is mention of the parameter @CustomerID. This will also demonstrate that the parameter is compiled with the value 1060 but currently it is running this with a value of 90.
This process of building the execution plan for the stored procedure with the initial run parameter is called Parameter Sniffing.
Performance Issue
In the real world, there is no performance issue of the execution plan of the new parameter is the same as the original execution plan. However, if the newer parameter is better suited for the different execution plan there can be huge potential performance degradation.
Let us understand this further by clearing the execution plan for our stored procedure by recompiling it.
1 | EXEC sp_recompile 'GetCustomerOrders' |
Now let us run the stored procedure again with the newer value of the 90 and you will see that the execution plan changes from the original execution plan. Earlier the execution plan displayed the count of the rows as 150 out of 4 which is not corrected as 150 of 150.
1 | EXEC GetCustomerOrders 90 |
If you now run the stored procedure with the earlier parameter where we got 4 rows, you will notice that that query now uses this newer plan and display 4 out of 150. This may be a potential performance issue as the query may get more resources allocated.
1 | EXEC GetCustomerOrders 1060 |
The simplest solution to overcome the parameter sniffing is to recompilation, however, it has also many different issues.
Post a Comment
0 Comments