SQLcommitted

Committed with SQL

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.

image

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.

image

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

About these ads

January 13, 2012 - Posted by | Performance Tips and Tricks | , , ,

3 Comments »

  1. 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?

    Comment by Raghavendra | September 9, 2012 | Reply

    • 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.

      Comment by SQLcommitted | September 12, 2012 | Reply

  2. Nice one

    Comment by Raghavendra | January 14, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 61 other followers

%d bloggers like this: