Set Option causes Stored Procedure Recompilation
I was working on a database performance issue and found that SET options can cause stored procedure recompilation. After knowing this fact a question raised in my mind that, If SET options causes recompilation then why we often use SET options in stored procedure.
Then I tried all applicable SET options in stored procedure and run the SQL profiler by checking the SP:Recompile option, to know whether the SET option is causing recompilation or not.
If Profiler catch the SP:Recompile event it means the stored procedure is recompiled.
Lets Try it,
Step 1: Create a Stored procedure
Create Procedure TestRecompilation
AS
BEGIN
SET ARITHABORT OFF
SELECT ‘SQLCOMMITTED’
END
GO
Step 2: Run the profiler and check the SP: Recompile event. To see the SP:Recompile event check the Show all events check box.
Step 3: Execute the Stored Procedure
exec TestRecompilation
go
Step 4: Check the profiler log. If the SP:Recompile event logged against the Stored Procedure that means the stored procedure recompiled.
Note: Procedure will recompile based on SET option and the value. Say for example if I use SET ARITHABORT ON, it won’t recompile the procedure but SET ARITHABORT OFF will cause procedure recompilation.
Refer link http://msdn.microsoft.com/en-us/library/ms190707.aspx to know more on SET option.
For more detail on procedure recompilation refer http://support.microsoft.com/kb/243586
3 Comments »
Leave a Reply
-
Archives
- March 2013 (1)
- January 2013 (3)
- December 2012 (2)
- November 2012 (1)
- August 2012 (4)
- July 2012 (2)
- June 2012 (2)
- May 2012 (1)
- April 2012 (5)
- March 2012 (1)
- February 2012 (1)
- January 2012 (5)
-
Categories
-
RSS
Entries RSS
Comments RSS


Sandip,
You have mentioned a particular example of ARITHABORT for re-compilation. On what basis re-compilation happens? Is it when we are deviating from default value? If yes , then re-compilation kills advantages of using SET in few cases? Can you please elaborate?
ANSI_NULL_DFLT_OFF, ANSI_NULL_DFLT_ON, ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, DATEFIRST, DATEFORMAT, FORCEPLAN, LANGUAGE, NO_BROWSETABLE, NUMERIC_ROUNDABORT, QUOTED_IDENTIFIER.
Above set options causes recompilation.
SQL Server perform evaluation of expression at compile time to enable some optimization and above set options causes recompilation because it affects the result of those expression.
SET option related recompilation can be avoided by setting required set option at conection time.
Nice one