When would you use OPTION (RECOMPILE)?
Use OPTION (RECOMPILE) when one cached execution plan is unsuitable for different parameter values.
It tells SQL Server to create a new execution plan for the current execution and discard it afterward.
SELECT *
FROM Orders
WHERE CustomerId = @CustomerId
OPTION (RECOMPILE);
Suitable situations
-
Parameter values return very different numbers of rows.
-
Data distribution is highly uneven.
-
Optional search filters create different query patterns.
-
A query runs infrequently but requires the best plan each time.
-
A cached plan causes a confirmed parameter-sniffing problem.
Avoid or use carefully when
-
The query executes very frequently.
-
Compilation is complex or CPU-intensive.
-
One reusable plan already performs well for most values.
Important difference
-- Recompiles only this statement
OPTION (RECOMPILE);
-- Recompiles the entire procedure
WITH RECOMPILE;
Key point
OPTION (RECOMPILE) can improve execution performance but adds compilation CPU cost. Use it for specific, proven plan-reuse problems—not as a default solution.