Using the “WITH RECOMPILE” option in a SQL Server stored procedure can cause several problems:
Increased Compilation Time: The stored procedure will need to be recompiled every time it is executed, which can lead to increased execution times and a potential impact on performance.
Increased Resource Utilization: Recompiling a stored procedure requires additional resources such as CPU and memory, which can lead to increased resource utilization on the server.
Inconsistent Execution Plans: Because the stored procedure is recompiled every time it is executed, it may generate different execution plans, leading to inconsistent performance.
Reduced Plan Reuse: Stored procedures that are recompiled frequently will not benefit from plan reuse, which can negatively impact performance.
Increased Risk of Plan Regression: Stored procedures with recompile option are more likely to experience plan regression, where a new plan is generated that performs worse than the previous plan.
Lack of Plan Stability: The recompile option can make it difficult to troubleshoot performance issues because the execution plan will change frequently.
Increased Risk of Incorrect Results: Recompiling a stored procedure can lead to incorrect results if the data distribution or statistics have changed significantly since the last compilation.
Lacks query optimization: Recompiling a stored procedure can lose any optimization that the query optimizer might have done during the initial compilation.
In general, the “WITH RECOMPILE” option should be used sparingly and only when absolutely necessary, such as when dealing with dynamic SQL or temporary tables with varying schema. It is recommended to investigate the cause of the poor performance, and fix it using other means like creating indexes, or rewriting the query.