The query is a single select containing a lot of grouping levels and aggragate operations. With SET ARITHABORT ON is takes less than a second, otherwise it takes several minutes. We have seen this behavior on SQL Server 2000 and 2008.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
migrated from stackoverflow.com Dec 28 '11 at 12:26
This question came from our site for professional and enthusiast programmers.
A little dated, but for anyone ending up here with a similar problem...
I had the same problem. For me it turned out to be parameter sniffing, which at first I didn't understand enough to care about. I added a 'set arithabort on' which fixed the problem but then it came back. Then I read:
It cleared -everything- up. Because I was using Linq to SQL and had limited options to fix the issue, I ended up using a query plan guide (see end of link) to force the query plan I wanted.
.NET applications connect with the option disabled by default, but it's enabled by default in Management Studio. The result is that the server actually caches 2 separate execution plans for most/all procedures. This affects how the server performs numerical calculations and as such you can get wildly different results depending on the procedure. This is really only one of 2 common ways a proc can get fed a terrible execution plan, the other being parameter sniffing.
Take a look at http://sqladvice.com/blogs/gstark/archive/2008/02/12/Arithabort-Option-Effects-Stored-Procedure-Performance.aspx for a little more discussion on it.
I would argue that this was almost certainly parameter sniffing.
It is often stated that
In this case (for SQL2005+ and unless your database is in SQL2000 compatibility mode). If you have both
The claim in Ben's answer that "the way the server performs numerical calculations" can add minutes to a result that would otherwise take less than a second just doesn't seem credible to me. I think what tends to happen is that upon investigating a performance performance problem Profiler is used to identify the offending query. This is pasted into management studio and run and returns results instantly. The only apparent difference between connections is the
A quick test in a management studio window shows that when
However that ignores the fact that with that option set you can end up getting the exact same bad plan from the cache.
This plan reuse can happen even if you are logged in as a different user than the application connection uses.
I tested this by executing a test query first from a web application then from management studio with
In order for this sharing pf plans to actually occur all plan cache keys must be the same. As well as
I know I'm late to this party, but for future visitors, Martin is exactly correct. We ran into this same issue--an SP was running very slowly for .NET clients, while it was blazing fast for SSMS. In exploring and resolving the issue, we did the systematic testing that Kenny Evitt asks about in his comment to Martin's question.
Using a variant of Martin's query, I looked for the SP in the procedure cache and found two of them. Looking at the plans, it was in fact the case that one had ARITHABORT ON and one had ARITHABORT OFF. The ARITHABORT OFF version had an index seek while the ARITHABORT ON version used an index scan for that same output. Given the parameters involved, the index seek would have required a lookup on tens of millions of records for the output.
I cleared the two procedures from the cache and had the .NET client run the SP again, using the same parameters (which featured a wide date range for a customer with lots of activity). The SP returned instantly. The cached plan used the same index scan that was previously featured in the ARITHABORT ON plan--but this time the plan was for ARITHABORT OFF. We ran the SP with the same parameters in SSMS, and again got results instantly. Now we saw that a second plan was cached, for ARITHABORT ON, with the index scan.
We then cleared the cache, ran the SP in SSMS with a narrow date range and got an instant result. We found that the resulting cached plan had an index seek, for the same output was previously handled with a scan (which was also a seek in the original plan with ARITHABORT OFF). Again from SSMS, we ran the SP, this time with the same wide date range, and saw the same terrible performance we had in the original .NET request.
In short, the disparity had nothing to do with the actual value of ARITHABORT--with it on or off, from either client, we could get acceptable or terrible performance: All that mattered was the parameter values used in compiling and caching the plan.
While MSDN indicates that ARITHABORT OFF itself can have a negative impact on query optimization, our testing confirms that Martin is correct--the cause was parameter sniffing and the resulting plan not being optimal for all ranges of parameters.