Welcome to AspAdvice Sign in | Join | Help

Stored Procedure Performance Varies Between ADO.NET and Management Studio

I ran into this very annoying issue earlier this week, that my buddy and SQL guru Gregg Stark was able to track down for me.  I have a fairly intense XtraReport report that gets its data from a stored procedure.  When I run that stored procedure in SQL Management Studio, it returns in less than a second.  When I run it on my web site, it takes over 30 seconds and times out.  WTF?  I confirmed, ad nauseum, that it really was running the same sproc with the same parameters.

So I hit up Stark and he suggests I try messing with ARITHABORT settings in Management Studio.  Lo and behold, when it's ON it's fast - less than one second.  When it's OFF it takes 90 seconds.  Apparently, ADO.NET uses different settings by default than Management Studio, and as a result it's using a really awful horrible bad query plan for this stored procedure.  Now, rather than hardcoding the ARITHABORT setting in the sproc, Stark suggested adding WITH RECOMPILE to the stored procedure.  This will lose the "benefit" of having a cached query plan, but it will help ensure that SQL Server uses a good query plan (instead of a godawful slow one).  Gregg posted about this solution a couple of days ago, so check it out there.

One nice thing about this fix - it took about 2 seconds to implement once the issue was discovered!  If you see a discrepancy between the performance of your stored procedure in Management Studio (or Query Analyzer) and your web site or ADO.NET code, then consider the WITH RECOMPILE option on the stored procedure (right before AS) as a possible fix.

Published Friday, February 15, 2008 4:36 PM by ssmith
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Stored Procedure Performance Varies Between ADO.NET and Management Studio

I also ran into this some time ago, and learnt about ARITHABORT impact by a post by Erland (SQL MVP): http://www.groupsrv.com/computers/about164393.html

Friday, February 15, 2008 4:55 PM by Teemu

# re: Stored Procedure Performance Varies Between ADO.NET and Management Studio

To add to the previous: IMO it is better to have ARITHABORT on for the connection since then indexed views and computed columns can be used...

"However, maybe the most probable cause. is that when the query runs frm

QA, the optimizer makes use of an indexed view or an index on a computed

column. Such indexes can only be used if a number of SET options are

ON and one is OFF. If you are running a normal client API such as

ODCC, ADO, OLE DB or ADO .Net, all these settings are in the right

position, except one: ARITHABORT, which must be ON.

"

ARITHABORT is set to ON when you set ANSI_WARNINGS ON (compat level 90 is SQL 2005 - with compat 80 nit must be set manually) and that can be set with sp_configure/server settings

There's also a note about it at: http://msdn2.microsoft.com/en-us/library/ms188783.aspx

Friday, February 15, 2008 5:13 PM by Teemu

# re: Stored Procedure Performance Varies Between ADO.NET and Management Studio

...You can also just re-compute your stats.

Monday, November 03, 2008 11:45 AM by Hoots

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below