Stored procedure recompilation can be both a performance killer and a performance booster—depending on how and why it happens. Many developers notice slow or inconsistent procedure performance without realizing that recompilation is the real reason.
This blog explains what recompilation is, when it happens, and how to control it properly.
1️⃣ What Is Stored Procedure Recompilation?
Recompilation occurs when SQL Server discards an existing execution plan and creates a new one before executing a stored procedure.
📌 Recompilation = New plan generation
2️⃣ Why SQL Server Recompiles Stored Procedures
SQL Server recompiles procedures to ensure correct and optimized execution plans.
Common Triggers
✔ Schema changes
✔ Statistics updates
✔ Temp table changes
✔ Parameter value changes
✔ OPTION (RECOMPILE)
3️⃣ Common Recompilation Causes (With Examples)
🔹 1. Temp Tables Inside Stored Procedures
CREATE TABLE #TempData (Id INT);
📌 SQL Server recompiles after temp table creation because structure becomes known only at runtime.
🔹 2. Statistics Update
When statistics change significantly:
UPDATE STATISTICS Users;
📌 Existing plans may be invalidated.
🔹 3. Schema Changes
ALTER TABLE Users ADD LastLogin DATETIME;
📌 Forces recompilation of dependent procedures.
🔹 4. OPTION (RECOMPILE)
SELECT * FROM Users
OPTION (RECOMPILE);
📌 Forces recompilation every execution.
4️⃣ Signs of Excessive Recompilation
❌ High CPU usage
❌ Inconsistent execution times
❌ Frequent plan cache misses
❌ Slow stored procedure execution
5️⃣ When Recompilation Is Actually Useful
✔ Fix parameter sniffing
✔ Highly variable data
✔ Ad-hoc reporting queries
Example
ALTER PROCEDURE dbo.GetOrders
WITH RECOMPILE
AS
BEGIN
SELECT * FROM Orders WHERE Status = @Status;
END
6️⃣ Controlling Recompilation
✅ Use OPTION (RECOMPILE) Selectively
SELECT * FROM Orders
WHERE OrderDate >= @Date
OPTION (RECOMPILE);
Use only for problematic queries.
✅ Use Local Variables to Reduce Recompilation
DECLARE @LocalStatus VARCHAR(20) = @Status;
✅ Separate Logic into Multiple Procedures
Avoid complex branching inside one procedure.
7️⃣ Temp Tables vs Table Variables Revisited
| Scenario | Better Choice |
|---|---|
| Small data | Table Variable |
| Large data | Temp Table |
| Frequent recompilation issue | Table Variable (sometimes) |
📌 Choose carefully based on data volume.
8️⃣ How to Detect Recompilation
🔍 Use DMVs
SELECT *
FROM sys.dm_exec_query_stats;
Check:
execution_counttotal_compile_time
9️⃣ Recompilation vs Parameter Sniffing
| Recompilation | Parameter Sniffing |
|---|---|
| New plan created | Plan reused badly |
| CPU intensive | Performance inconsistency |
| Sometimes helpful | Often problematic |
📌 Recompilation is sometimes the solution to parameter sniffing.
🔟 Best Practices to Manage Recompilation
✔ Avoid unnecessary temp tables
✔ Keep procedures simple
✔ Use OPTION (RECOMPILE) only where needed
✔ Monitor compile vs execution time
✔ Test with real data
📌 Balanced Stored Procedure Example
CREATE PROCEDURE dbo.GetActiveOrders
@Status VARCHAR(20)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @LocalStatus VARCHAR(20) = @Status;
SELECT OrderId, OrderDate
FROM Orders
WHERE Status = @LocalStatus;
END
✍️ Conclusion
Stored procedure recompilation is not always bad, but uncontrolled recompilation is dangerous. Understanding when and why SQL Server recompiles helps you strike the perfect balance between performance and correctness.
Mastering this topic puts you firmly in the advanced SQL Server developer category.
