In SQL Server, how do CREATE PROCEDURE ... WITH RECOMPILE, OPTION (RECOMPILE) on one statement, and sp_recompile differ in what they recompile and when?
answer
- whole module, one statement, or a mark
- nothing cached at all
- temporary plan, then discarded
- local variable values are sniffed too
- a table name marks its dependents
basics
~20 sWITH RECOMPILE on a procedure stops SQL Server caching any plan for it, so every call recompiles the whole procedure. OPTION (RECOMPILE) recompiles one statement each run. sp_recompile only marks a module for recompilation on its next execution.
solid answer
~50 sSQL Server offers recompile control at different grains. `CREATE PROCEDURE ... WITH RECOMPILE` means the engine never caches a plan for the procedure, so every statement compiles on every call. `EXEC dbo.p ... WITH RECOMPILE` does that once: it compiles a new plan for that execution, uses it and discards it, and an existing cached plan stays. `OPTION (RECOMPILE)` on a statement builds a temporary plan for just that query and throws it away afterwards without replacing the cached one. It compiles using the current parameter values and, unlike the procedure-level option, the current values of local variables. `sp_recompile` runs nothing. It drops the module's plan so the next execution recompiles, and given a table it does that for every procedure, trigger and function that references it. Usually I reach for `OPTION (RECOMPILE)` on the one sensitive statement, because recompiling everything costs CPU on every call.
code
sql · 18 linesCREATE OR ALTER PROCEDURE dbo.usp_ObjectsByType
@type CHAR(2)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @since DATETIME = DATEADD(DAY, -30, GETDATE());
-- Only this statement gets a fresh plan, using @type and @since as they are now
SELECT name, create_date
FROM sys.objects
WHERE type = @type
AND create_date >= @since
OPTION (RECOMPILE);
END;
GO
EXEC dbo.usp_ObjectsByType @type = 'U';
EXEC dbo.usp_ObjectsByType @type = 'P' WITH RECOMPILE; -- one-off: whole procedure
EXEC sys.sp_recompile N'dbo.usp_ObjectsByType'; -- mark for the next rungo deeper
Recall the three tools: WITH RECOMPILE on a procedure, the OPTION (RECOMPILE) hint on a statement, and sp_recompile to mark a module for its next run.
Explain the cache effect of each tool: nothing cached, a temporary plan discarded, a plan dropped for the next run. Also explain why only the statement hint sniffs local variables.
Choose the narrowest tool that fixes the plan, weigh compile CPU at the procedure's call rate, and use sp_recompile on a table to reset only its dependents.
Set guidance on when recompile hints are acceptable in shared procedures, and how they are reviewed and measured.
## Why SQL Server has several recompile switches SQL Server compiles a stored procedure's statements into a plan and **caches** it, so later calls skip compilation. That is usually good. It goes wrong when the plan compiled for one set of parameter values is poor for another, or when the data has changed a lot. Diagnosing that sniffing problem is covered with SQL Server's plan and performance tooling. This lesson is about the module-level tools themselves, which work at different grains: | Tool | Scope | When it takes effect | What happens to the cache | |---|---|---|---| | `CREATE PROCEDURE ... WITH RECOMPILE` | Whole procedure | Every execution | No plan is cached for the procedure | | `EXEC dbo.p ... WITH RECOMPILE` | Whole procedure, one call | That execution only | New plan used then discarded; an existing cached plan remains | | `OPTION (RECOMPILE)` | One statement | Every execution of that statement | Temporary plan discarded; it does not replace a cached plan | | `sp_recompile` | A module, or every module referencing a table or view | The next execution | Existing plan dropped from the cache | ## WITH RECOMPILE on the procedure Declaring `WITH RECOMPILE` in `CREATE PROCEDURE` tells the Database Engine **not to cache a query plan** for the procedure. Consequences: - Every call compiles **every statement**, including the ones that would happily reuse a plan. Compile CPU becomes a per-call cost. - Because nothing is cached, the plan cache holds no plan for the procedure to inspect afterwards. - The option cannot be combined with `FOR REPLICATION`, and it does not apply to CLR procedures. The `EXEC ... WITH RECOMPILE` form is the one-off version, useful when you know a particular call has atypical parameters. The `EXECUTE` documentation describes it as expensive and advises using it sparingly. ## OPTION (RECOMPILE) on a statement The `RECOMPILE` query hint makes SQL Server generate a **new, temporary plan** for that statement, then discard it immediately after execution. Key details: 1. Only that statement pays the compile. The rest of the procedure keeps its cached plans, which mimics SQL Server's own **statement-level recompilation**. 2. The plan is compiled with the **current values of the procedure's parameters and of any local variables** in the statement. The procedure-level option uses parameter values but not local variable values, so a statement that filters on a variable computed inside the procedure is only optimized for that variable's actual value with the hint. 3. The temporary plan does **not replace** a plan cached for the same query run without the hint. That makes `OPTION (RECOMPILE)` the usual choice for the one query whose best plan depends heavily on its inputs, such as a catch-all search predicate. ## sp_recompile `sp_recompile` executes nothing. It **drops the existing plan** so that the module is recompiled the next time it runs: - Given a procedure, trigger or user-defined function name, it marks that module. - Given a **table or view** name, it marks **every procedure, trigger and function that references it**. - It needs `ALTER` permission on the object you name. It is the operational tool: after an index change or a large data load, or to clear a bad plan for one procedure without flushing the whole plan cache. ## Recompiles that happen anyway You do not need these tools for every change. SQL Server recompiles automatically when the instance restarts and when a table the procedure references undergoes physical design changes. It recompiles at **statement level**, so only the statement that triggered the recompile is compiled again, not the whole procedure. ## Choosing between them - Start with nothing. Plan reuse is the default for a reason. - If one statement is sensitive to its inputs and runs rarely enough that a compile per execution is affordable, add `OPTION (RECOMPILE)` to that statement. - Use procedure-level `WITH RECOMPILE` only when every statement is sensitive and calls are infrequent. It is rarely the best grain. - Use `sp_recompile` as a targeted reset, not as a scheduled job. - Watch the cost: on a procedure called thousands of times a second, any recompile option turns compile CPU into steady load.
- Why can OPTION (RECOMPILE) produce a better plan than WITH RECOMPILE for a statement filtering on a local variable?When a statement compiles with the `RECOMPILE` hint, SQL Server uses the current values of the procedure's parameters and of local variables in that statement. Procedure-level `WITH RECOMPILE` compiles each call with the parameter values, but a local variable computed inside the procedure is not sniffed that way. The hint therefore lets the optimizer see the variable's actual value when estimating rows.
- In SQL Server, what does sp_recompile do when you pass it a table name?It marks every stored procedure, trigger and user-defined function that references the table, so each is recompiled the next time it runs. Nothing executes immediately, and plans for unrelated modules are untouched. That makes it a targeted alternative to clearing the whole plan cache after an index or data change on that table. It needs `ALTER` permission on the object.
- OPTION (RECOMPILE) evicts the whole procedure's cached plan
- sp_recompile recompiles the procedure immediately
- WITH RECOMPILE on a procedure only recompiles the statements that need it
- Recompiling on every call is free once the procedure is small
- EXEC ... WITH RECOMPILE replaces the cached plan with the new one