Query times out from web app but runs fine from management studio

前端 未结 8 1481
盖世英雄少女心
盖世英雄少女心 2020-12-01 05:13

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.

The problem is that you have one

相关标签:
8条回答
  • 2020-12-01 05:52

    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.

    You might look into the execution times between the web app functions as well through the trace to help track things down.

    0 讨论(0)
  • 2020-12-01 05:52

    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.

    0 讨论(0)
  • 2020-12-01 05:55

    We had the same issue and here's what we found out.

    our database log size was being kept at the default (814 MB) and auto growth was 10%. On the server, maximum server memory was kept at the default setting as well (2147483647 MB).

    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.

    0 讨论(0)
  • 2020-12-01 06:02

    Same problem I had with SQL reporting services. Try to check type of variables, I was sending different type of variable to SQL like sending varchar in place where it should be integer, or something like that. After I synchronized the types of variables in Reporting Service and in stored procedure on SQL, I solved the problem.

    0 讨论(0)
  • 2020-12-01 06:08

    This is what I've learned so far from my research.

    .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:

    -- network protocol: TCP/IP  
    set quoted_identifier off  
    set arithabort off  
    set numeric_roundabort off  
    set ansi_warnings on  
    set ansi_padding on  
    set ansi_nulls off  
    set concat_null_yields_null on  
    set cursor_close_on_commit off  
    set implicit_transactions off  
    set language us_english  
    set dateformat mdy  
    set datefirst 7  
    set transaction isolation level read committed  
    

    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.

    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.

    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.

    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.

    Is there something else about the arithabort setting that might cause a query to run slowly in some cases?

    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'.

    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).

    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.

    0 讨论(0)
  • 2020-12-01 06:11

    test this out on a staging box first, change it on a server level for sql server

    declare @option int

    set @option = @@options | 64

    exec sp_configure 'user options', @option

    RECONFIGURE

    0 讨论(0)
提交回复
热议问题