![]() |
Stored procedure recompiles and SET options | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Home About me and Nucleus Partners Links Articles, tips etc. My blog Microsoft articles Forums Course demo files Sync logins SMTP Alerter ![]() |
Overview Using stored procedures is generally considered a good thing. One advantage of stored procedures is that they are precompiled. This means that at execution time, SQL Server will fetch the precompiled procedure plan from cache memory (if exists) and execute it. This is generally faster than optimizing and compiling the code for each execution. However, under some circumstances, a procedure needs to be recompiled during execution. Thanks to SQL Server MVP Frank Kalis, this article is translated to German. More information The execution engine can come to a point in the stored procedure where it finds that it needs to recompile the whole procedure. Note that stored procedures are not compiled statement by statement. If a recompile is needed, say, at the end of the procedure, then the whole procedure will be optimized and compiled. Why does this happen? Here are some reasons:
Be aware that a procedure can be recompiled several times during execution. I recommend that you use Profiler and catch the SP:Recompile event to determine whether your procedure is recompiled at execute time. You will get one event for each re-compilation. Not all SET options causes recompiles. So I conducted a test to find out which does. Below table lists all documented SET options and whether each causes a recompile or not.
SQL Code I used below SQL code to semi-automate above work: USE pubs
|