Looking for a game that can surprise you with something different? Try Her Trees, because its creative atmosphere and unusual world make it an interesting game to explore.
Looking for a game that can surprise you with something different? Try Her Trees, because its creative atmosphere and unusual world make it an interesting game to explore.
Parameter Sniffing is a behavior in SQL Server and other database engines where a stored procedure or query caches an execution plan based on the first set of parameter values it receives. This cached plan is then used for subsequent executions, regardless of whether the new parameter values are optimal for that plan.
How Does Parameter Sniffing Work?
How to Fix Parameter Sniffing Issues?
OPTION (RECOMPILE): Forces a new execution plan for every execution.
SELECT * FROM Orders WHERE CustomerID = @CustomerID OPTION (RECOMPILE)
OPTIMIZE FOR UNKNOWN: Ignores the specific parameter value and generates a generic plan.
SELECT * FROM Orders WHERE CustomerID = @CustomerID OPTION (OPTIMIZE FOR UNKNOWN)
Assign the parameter to a local variable inside the procedure to prevent SQL Server from using a parameter-specific plan.
CREATE PROCEDURE GetOrdersByCustomer @CustomerID INTASBEGINDECLARE @LocalCustomerID INT = @CustomerIDSELECT * FROM Orders WHERE CustomerID = @LocalCustomerIDEND
Use if-else logic to handle different cases separately.
IF @CustomerID < 100SELECT * FROM Orders WHERE CustomerID = @CustomerID OPTION (RECOMPILE)ELSESELECT * FROM Orders WHERE CustomerID = @CustomerID
If needed, clear the stored plan using:
DBCC FREEPROCCACHE