Why do we need to recompile stored procedures?
Category:
technology and computing
databases
Another reason to force a stored procedure to recompile is to counteract, when necessary, the "parameter sniffing" behavior of stored procedure compilation. When SQL Server executes stored procedures, any parameter values used by the procedure when it compiles are included as part of generating the query plan.
Besides, what causes stored procedure to recompile?
Factors in Recompilation
- Inserting or deleting lots of data (data density in indexes & tables often controls query plans)
- Rebuilding indexes (a change to underlying objects)
- Creating/dropping temp tables (again, underlying DML changes).
- query plan ages out (think not used recently and sql want's to clean up memory use)
Hereof, what is the use of recompile in SQL Server?
RECOMPILE – specifies that after the query is executed, its query execution plan stored in cache is removed from cache. When the same query is executed again, there will be no existing plan in cache, so the query will have to be recompiled.
sp_recompile looks for an object in the current database only. The queries used by stored procedures, or triggers, and user-defined functions are optimized only when they are compiled. SQL Server automatically recompiles stored procedures, triggers, and user-defined functions when it is advantageous to do this.