Wednesday, March 16, 2011

Any downside to "WITH RECOMPILE" for monthly SQL Server stored proc processes?

I think the question says it all. I have several monthly processes in stored procedures which take anywhere from a minute to an hour. If I declare them WITH RECOMPILE, an execution plan will be generated each time.

If the underlying indexes or statistics or views are changed by the DBA, I don't want anyone to have to go in and force a recompile the SPs with an ALTER or whatever.

Is there any downside to this?

From stackoverflow
  • Under the circumstances, it would be completely harmless, and probably a good idea.

  • As I understand it, an SP should be re-compiled if needed automatically. So your concern about underlying changes doesn't really matter.

    However, the server tries to cache compiled SP plans. Using WITH RECOMPILE will free the memory that would have been used to cache the compiled procedures (at least until the next time the cache is cleared). Since they're only run monthly this seems like a good idea.

    Also, you might want to look at this article for other reason to use that directive:
    http://articles.techrepublic.com.com/5100-10878_11-5662581.html

    Cade Roux : What if there are newer, better indexes it could use, or if there were new statistics - will the old plans be automatically discarded?
    Jim McLeod : No, the only time a plan with be invalidated is when indexes or statistics used by the plan are modified. See the topic "Execution Plan Caching and Reuse" (look up "procedure cache" in the index), section "Recompiling Execution Plans" in Books Online for more details about what invalidates plans.
  • If each stored procedure is only run once per month, it is highly unlikely that the compiled procedure will still be in the procedure cache. Effectively it will be recompiling anyway.

    Even if you run the same stored procedure 100 times on your reporting day, it will only take 0-2 seconds to compile each time (depending on the complexity of the stored procedure), so it's not a massive overhead. I'd feel comfortable setting WITH RECOMPILE on those stored procedures.

    Cade Roux : Right - I'm thinking the recompile time is always neglible compared to the run time - and the run time limits me from running these processes so frequently that it could even be a factor.

0 comments:

Post a Comment