[quoted text, click to view] "Erland Sommarskog" <sommar@algonet.se> wrote in message
news:Xns949CF41F3DA21Yazorman@127.0.0.1...
> Harold (chen1999@hotmail.com) writes:
> > I have a question in SQL Server 2K, I use SQL Profile to trace, and
> > find Stored Procedure was auto recompiled, like this row in the
> > trace:
> > SP:Recompile 15 1680 76 2004-02-27 16:01:11.610
> >
> > How can I stop the auto recompile.
>
> Simon posted a very useful link. But permit me some short notes.
I'm going to jump in with my own real life experience here.
We went through the steps MS recommended.... big first step was making sure
we called all of our stored procs fully qualified with names, etc.
i.e. dbo.stored_proc as opposed to just stored_proc.
This helped. We got about 10% improvement in throughput.
Just last week, we found another issue (which I think is partly related to
the ODBC drivers, but that's beyond the scope here.)
In any case, rather than doing: exec stored_proc @foo=123 @bar='xyz' etc.
we wrapped the stored_proc call in a sp_executesql.... exec
sp_executesql(stored_proc, etc etc.)
What used to take two sql boxes to handle the load for (and even then we
were getting a lot of problems) we now run on ONE box and have processing
power to spare.
One thing that helped us track this down was a recent article in SQL Server
Magazine that provided some stored procs on tracking wait states.
Whereas over a 2 minute period we'd get MILLIONS of LCK_MX_I (I think I got
that right off the top of my head) we now get hundreds to thousands.)
So, in this case, making the change made a major difference for us. (in our
case this stored proc gets called millions of times a day.)
So, in our case, the savings was HUGE.
[quoted text, click to view] >
> Recompiles are a mixed blessing. Sometimes they kill your performance,
> sometimes they save the day. Not at all knowing your situation, it is
> difficult to tell.
>
> The most common reason for recompiles are temp tables that are filled
> with data in the procedure. There are two remedies: use table variables
> instead, or use OPTION (KEEPFIXED PLAN).
>
> Table variables does not have statistics, which is why they cannot cause
> recompile. But that also means that SQL Server has less information about
> them when building a plan. Also, be aware of that insering data into a
> table variable precludes parallellism. This can also be a performance
> killer.
>
> If you litter your code with OPTION (KEEPFIXED PLAN) you can prevent
> recompiles, but forget it in one place, and you are in for it again.
>
> A war story: some time back, I fought with a procedure that took 1½
> minute to run. I figured I try to cut that down, and since there
> were several recompiles, I tried to switching to table variables
> plus a few more tricks. Sure, the recompiles went away - but I lost
> on the roundabouts, so the execution time was still 1½ minute.
>
>
> --
> Erland Sommarskog, SQL Server MVP, sommar@algonet.se
>
> Books Online for SQL Server SP3 at
>
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp