<p>This is a question I asked on another forum which received some decent answers, but I wanted to see if anyone here has more insight.</p>

<p>The problem is that you have one of your pages in a web application timing out when it gets to a stored procedure call, so you use Sql Profiler, or your application trace logs, to find the query and you paste it into management studio to figure our why it's running slow.  But you run it from there and it just blazes along, returning in less than a second each time.</p>

<p>My particular case was using ASP.NET 2.0 and Sql Server 2005, but I think the problem could apply to any RDBMS system.</p>

[{'answer_id': 9982, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': -1, 'selected': False, 'text': '<p>Try changing the SelectCommand timeout value:</p>\n\n<pre><code>DataAdapter.SelectCommand.CommandTimeout = 120;\n</code></pre>\n'}, {'answer_id': 9996, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 6, 'selected': True, 'text': "<p>This is what I've learned so far from my research.</p>\n\n<p>.NET sends in connection settings that are not the same as what you get when you log in to management studio.  Here is what you see if you sniff the connection with Sql Profiler:</p>\n\n<pre><code>-- network protocol: TCP/IP  \nset quoted_identifier off  \nset arithabort off  \nset numeric_roundabort off  \nset ansi_warnings on  \nset ansi_padding on  \nset ansi_nulls off  \nset concat_null_yields_null on  \nset cursor_close_on_commit off  \nset implicit_transactions off  \nset language us_english  \nset dateformat mdy  \nset datefirst 7  \nset transaction isolation level read committed  \n</code></pre>\n\n<p>I am now pasting those setting in above every query that I run when logged in to sql server, to make sure the settings are the same.</p>\n\n<p>For this case, I tried each setting individually, after disconnecting and reconnecting, and found that changing arithabort from off to on reduced the problem query from 90 seconds to 1 second.</p>\n\n<p>The most probable explanation is related to parameter sniffing, which is a technique Sql Server uses to pick what it thinks is the most effective query plan.  When you change one of the connection settings, the query optimizer might choose a different plan, and in this case, it apparently chose a bad one.</p>\n\n<p>But I'm not totally convinced of this.  I have tried comparing the actual query plans after changing this setting and I have yet to see the diff show any changes.</p>\n\n<p>Is there something else about the arithabort setting that might cause a query to run slowly in some cases?</p>\n\n<p>The solution seemed simple: Just put set arithabort on into the top of the stored procedure.  But this could lead to the opposite problem: change the query parameters and suddenly it runs faster with 'off' than 'on'.  </p>\n\n<p>For the time being I am running the procedure 'with recompile' to make sure the plan gets regenerated each time.  It's Ok for this particular report, since it takes maybe a second to recompile, and this isn't too noticeable on a report that takes 1-10 seconds to return (it's a monster).</p>\n\n<p>But it's not an option for other queries that run much more frequently and need to return as quickly as possible, in just a few milliseconds.</p>\n"}, {'answer_id': 10028, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 1, 'selected': False, 'text': "<p>test this out on a staging box first, change it on a server level for sql server</p>\n\n<blockquote>\n  <p>declare @option int</p>\n  \n  <p>set @option = @@options | 64</p>\n  \n  <p>exec sp_configure 'user options',\n  @option</p>\n  \n  <p>RECONFIGURE</p>\n</blockquote>\n"}, {'answer_id': 10163, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 2, 'selected': False, 'text': "<p>Have you turned on ASP.NET tracing yet? I've had an instance where it wasn't the SQL stored procedure itself that was the problem, it was the fact that the procedure returned 5000 rows and the app was attempting to create databound ListItems with those 5000 items that was causing the problem. </p>\n\n<p>You might look into the execution times between the web app functions as well through the trace to help track things down.</p>\n"}, {'answer_id': 51082, 'author': 'tbreffni', 'author_id': 637, 'author_profile': 'https://Stackoverflow.com/users/637', 'pm_score': 0, 'selected': False, 'text': "<p>You could try using the sp_who2 command to see what process in question is doing.  This will show you if it's blocked by another process, or using up an excessive amount of cpu and/or io time.</p>\n"}, {'answer_id': 73111, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Same problem I had with SQL reporting services.\nTry to check type of variables, \nI was sending different type of variable to SQL like sending varchar in place where it should\nbe integer, or something like that.\nAfter I synchronized the types of variables in Reporting Service and in stored procedure on SQL, I solved the problem.</p>\n'}, {'answer_id': 255869, 'author': 'Brian Adams', 'author_id': 32992, 'author_profile': 'https://Stackoverflow.com/users/32992', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ve had similar problems. Try setting the with "WITH RECOMPILE" option on the sproc create to force the system to recompute the execution plan each time it is called.  Sometimes the Query processor gets confused in complex stored procedures with lots of branching or case statements and just pulls a really sub-optimal execution plan.  If that seems to "fix" the problem, you will probably need to verify statistics are up to date and/or break down the sproc.</p>\n\n<p>You can also confirm this by profiling the sproc.  When you execute it from SQL Managment Studio, how does the IO compare to when you profile it from the ASP.NET application.  If they very a lot, it just re-enforces that its pulling a bad execution plan.</p>\n'}, {'answer_id': 20200058, 'author': 'Lac Ho', 'author_id': 1030454, 'author_profile': 'https://Stackoverflow.com/users/1030454', 'pm_score': 0, 'selected': False, 'text': "<p>We had the same issue and here's what we found out.</p>\n\n<p>our database log size was being kept at the default (814 MB) and auto growth was 10%. \nOn the server, maximum server memory was kept at the default setting as well (2147483647 MB).</p>\n\n<p>When our log got full and needed to grow, it used all the memory from the server and there's nothing left for code to be run so it timed out. What we ended up doing was set database log file initial size to 1 MB and maximum server memory to 2048 MB. This instantly fixed our problem. Of course, you can change these two properties to fit your need but this is an idea for someone running into the timing out issue when executing a stored procedure via code but it runs super fast in SSMS and the solutions above do not help.</p>\n"}]
['https://Stackoverflow.com/questions/9974', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1219/']