Read more: http://www.blogsmonetize.com/2010/10/how-to-use-syntax-highlighter-3083-in.html#ixzz1DHzvEgBA

Thursday, December 16, 2010

IF statement resulting in Recompile

There is a wrong perception amoung someDBAs that the if statement in procedures would cause a recompile of the procedures at the execution time. However, it is been proved that if a procedure contains if statements, all codes are getting compiled at the first compilation stored in the cache,ie, the cache plan contains all the statements and never been recompiled due to the "if statement".
--Look at the plan created
From the above figure, it is clear that even you execute the procedure which succeeds for a condition, SQL server would create plan for both cases and utilize the same for the next execution. The issue here is for a procedure with more if and else condition, the plan created may not be suitable and may lead to an unexpected execution plans.


Reference:
http://www.sqlservercentral.com/blogs/sqlinthewild/archive/2010/12/14/do-if-statements-cause-recompiles_3F00_.aspx
http://scarydba.com/2010/05/11/recompiles-and-constant-learning/

No comments: