Home All Groups Group Topic Archive Search About

Recompile vs Recreate



Author
17 Apr 2007 10:24 PM
Frank Rizzo
I have an intermittent problem on SQL Server 2005 64bit where the stored
procedure query will get out of sync with the data and it starts taking
a long time to return the data.

I've tried issuing sp_recompile on the stored proc and the performance
did not improve at all.

I then dropped and recreated the sproc and, magically, the performance
improved to its original state.

How is sp_recompile different from dropping and recreating a stored proc
from the standpoint of statistics.

Regards

Author
18 Apr 2007 6:28 AM
Uri Dimant
Frank
Can you post here stored procedure's code?

1) There are new RECOMPILE hints  in SQL Server 2005
(OPTION(RECOMPILE)),OPTION(OPTIMIZE FOR ...)
2) Take a  look at execution plan
3) Add WITH RECOMPILE option when you create a stored procedure



Show quoteHide quote
"Frank Rizzo" <n***@none.com> wrote in message
news:uCrO28TgHHA.1244@TK2MSFTNGP04.phx.gbl...
>I have an intermittent problem on SQL Server 2005 64bit where the stored
>procedure query will get out of sync with the data and it starts taking a
>long time to return the data.
>
> I've tried issuing sp_recompile on the stored proc and the performance did
> not improve at all.
>
> I then dropped and recreated the sproc and, magically, the performance
> improved to its original state.
>
> How is sp_recompile different from dropping and recreating a stored proc
> from the standpoint of statistics.
>
> Regards
Are all your drivers up to date? click for free checkup

Author
18 Apr 2007 11:38 AM
peterh
I would recommend caution about WITH RECOMPILE. For a busy proc, this
can creat a big CPU overhead for SQL Server recompiling the same proc
every time it is used.


Show quoteHide quote
On Apr 18, 7:28 am, "Uri Dimant" <u***@iscar.co.il> wrote:
> Frank
> Can you post here stored procedure's code?
>
> 1) There are new RECOMPILE hints  in SQL Server 2005
> (OPTION(RECOMPILE)),OPTION(OPTIMIZE FOR ...)
> 2) Take a  look at execution plan
> 3) Add WITH RECOMPILE option when you create a stored procedure
>
> "Frank Rizzo" <n***@none.com> wrote in message
>
> news:uCrO28TgHHA.1244@TK2MSFTNGP04.phx.gbl...
>
>
>
> >I have an intermittent problem on SQL Server 2005 64bit where the stored
> >procedure query will get out of sync with the data and it starts taking a
> >long time to return the data.
>
> > I've tried issuing sp_recompile on the stored proc and the performance did
> > not improve at all.
>
> > I then dropped and recreated the sproc and, magically, the performance
> > improved to its original state.
>
> > How is sp_recompile different from dropping and recreating a stored proc
> > from the standpoint of statistics.
>
> > Regards- Hide quoted text -
>
> - Show quoted text -
Author
18 Apr 2007 5:47 PM
Frank Rizzo
peterh wrote:
> I would recommend caution about WITH RECOMPILE. For a busy proc, this
> can creat a big CPU overhead for SQL Server recompiling the same proc
> every time it is used.

You are absolutely correct.  I can't afford a recompile.  This proc is
called at least 5-10 times every 5 seconds.
Author
19 Apr 2007 6:09 AM
Wei Lu [MSFT]
Hello Frank,

According to the BOL, when SQL Server 2005 recompiles stored procedures,
only the statement that caused the recompilation is compiled, rather than
the entire procedure. As a result, SQL Server uses the parameter values as
they exist in the recompiled statement when regenerating a query plan.
These values may differ from those that were originally passed into the
procedure.

So it may be that in your scenario, the parameters used by the stored
procedure when you recompile is different so the query plan is not a
optimized one.

But after you recreate the stored procedure, the parameters used in the
compilation generate a new query plan which is more opimize.

For your reference, please follow this article:

Recompiling Stored Procedures
http://msdn2.microsoft.com/en-us/library/ms190439.aspx

If you have any question, please feel free to let me know.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
25 Apr 2007 8:37 AM
Wei Lu [MSFT]
Hi ,

How is everything going? Please feel free to let me know if you need any
assistance.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
18 Apr 2007 12:00 PM
Andrew J. Kelly
I think it was just a coincidence that it starting working as you wanted
after you dropped it. Both recompiling and recreating will force a new query
plan the first time it is run after that event. The plan is dependent on the
actual parameters passed in at that time. Make sure your statistics are up
to date and accurate. Also think about using the OPTIMIZE FOR hint if the
values of the parameters may be such that you get some parameters with
values that produce an incorrect plan for the othervalues.

--
Andrew J. Kelly SQL MVP

Show quoteHide quote
"Frank Rizzo" <n***@none.com> wrote in message
news:uCrO28TgHHA.1244@TK2MSFTNGP04.phx.gbl...
>I have an intermittent problem on SQL Server 2005 64bit where the stored
>procedure query will get out of sync with the data and it starts taking a
>long time to return the data.
>
> I've tried issuing sp_recompile on the stored proc and the performance did
> not improve at all.
>
> I then dropped and recreated the sproc and, magically, the performance
> improved to its original state.
>
> How is sp_recompile different from dropping and recreating a stored proc
> from the standpoint of statistics.
>
> Regards
Author
18 Apr 2007 5:50 PM
Frank Rizzo
Andrew J. Kelly wrote:
> I think it was just a coincidence that it starting working as you wanted
> after you dropped it. Both recompiling and recreating will force a new query
> plan the first time it is run after that event. The plan is dependent on the
> actual parameters passed in at that time. Make sure your statistics are up
> to date and accurate. Also think about using the OPTIMIZE FOR hint if the
> values of the parameters may be such that you get some parameters with
> values that produce an incorrect plan for the othervalues.

I do not think that it's a coincidence.  It happened once too many times.
Author
19 Apr 2007 2:14 PM
Andrew J. Kelly
The plans are not different for a compile vs. a recompile. I seriously doubt
that is the issue. Have a look at this white paper, especially the part
about parameter sniffing.

http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx



--
Andrew J. Kelly SQL MVP

Show quoteHide quote
"Frank Rizzo" <n***@none.com> wrote in message
news:ugiJFIegHHA.4952@TK2MSFTNGP02.phx.gbl...
> Andrew J. Kelly wrote:
>> I think it was just a coincidence that it starting working as you wanted
>> after you dropped it. Both recompiling and recreating will force a new
>> query plan the first time it is run after that event. The plan is
>> dependent on the actual parameters passed in at that time. Make sure your
>> statistics are up to date and accurate. Also think about using the
>> OPTIMIZE FOR hint if the values of the parameters may be such that you
>> get some parameters with values that produce an incorrect plan for the
>> othervalues.
>
> I do not think that it's a coincidence.  It happened once too many times.

Bookmark and Share