Stored procedures are timing out intermittently!

29,075

Solution 1

Ok - this is how I fixed it in the end.

A clustered index on a table with 45 million records was killing my SQL server - every insert from code was resulting in the nasty timeouts described in the answer. Increasing the timeout tolerance wasn't gonna solve my scalability issues so I played around with indexes and making the clustered index on the primary key nonclustered unlocked the situation.

I'd appreciate comments on this to better understand how this fixed the problem.

Solution 2

Try to recompile these procedures. I've such problems few times and didn't find the cause of problem, but recompiling always helps.

EDIT:

To recompile proc, you go to management studio, open procedure to modify and hit F5 or execute: EXEC sp_recompile 'proc_name'

Solution 3

Management studio sets an infinite timeout on queries/commands it runs. Your database connection from code will have a default timeout which you can change on the command object.

Solution 4

This can often relate to:

  • bad query plans due to over-eager plan-reuse (parameter sniffing)
  • different SET options - in particular ANSI_NULLS and CONCAT_NULL_YIELDS_NULL
  • locking (you might have a higher isolation level)
  • indexing needs to be rebuilt / stats updated / etc

The SET options can lead to certain index types not being usable (indexes on persisted calculated columns, for example - including "promoted" xml/udf queries)

Solution 5

Is you command timeout set? Has something in your db recently changed that is causing this proc to take longer?

If you are have to diagnose locking issues, you will need to use something like sp_lock.

Can you share the source of one of your procs?

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx

Share:
29,075
JohnIdol
Author by

JohnIdol

The world will be mine but then I'll be killed by my own AI. Or by a giant simulated C. elegans out of the OpenWorm project. You can follow me on twitter.

Updated on June 21, 2020

Comments

  • JohnIdol
    JohnIdol almost 4 years

    I have a number of stored procedures I call from code with ExecuteNonQuery.

    It was all good but 2 of my stored procedures started timing out intermittently today with:

    Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated.

    If I execute the sp manually from management studio it's still all good.

    Nothing recently changed in my db - my command timeout is the default one.

    Any clue?

    EDIT

    the table against the SPs are running it's huge --> 15 Gigs. Rebooted the box - same issue but this time can't get the sp to run from Management Studio either.

    Thanks!

  • JohnIdol
    JohnIdol over 15 years
    I am doing pretty extensively transactions - it was all good till a few hours ago - not too sure setting a higher timeout will help.
  • Sam Saffron
    Sam Saffron over 15 years
    You can set it to 0 meaning it will not timeout
  • Russ Cam
    Russ Cam over 15 years
    You can inspect the connection properties by setting up a trace in SQL Server Profiler. From here, you'll be able to see the default timeout setting being used, if a value is not explicitly set within your code
  • JohnIdol
    JohnIdol over 15 years
    It happens pretty fast on management studio though (less than 1 sec) and on my code it is timing out after 30 secs or so.
  • Sam Saffron
    Sam Saffron over 15 years
    Also, if its fast in studio, maybe a transaction is blocking it. you need to diagnose with sp_lock
  • Sam Saffron
    Sam Saffron over 15 years
    Connection timeout and command timeout should not be confused, connection timeout is the max time it would take to connect to the db
  • Sam Meldrum
    Sam Meldrum over 15 years
    @JohnIdol You may want to take a look at query profiler to see what is going on. It may give you some extra clues. May give you some commands you can run through query analyzer to see if the query plan is the same as the one being run through management studio.
  • Michal Dymel
    Michal Dymel over 15 years
    the best is to EXEC sp_recompile 'proc_name'
  • Russ Cam
    Russ Cam over 15 years
    Good call on the Parameter sniffing - particularly pertinent to SQL Server 2000. I understand that parmeter sniffing is not a problem in SQL Server 2005 onwards. Good article on how to combat parameter sniffing here blogs.msdn.com/khen1234/archive/2005/06/02/424228.aspx
  • dburges
    dburges over 15 years
    Changing the timeout is the worst thing you can do. You need to diagnose and find the problem not just let it take more time.
  • Till F.
    Till F. over 15 years
    Changing the timeout is not the worst thing you can do. If a query takes > 30 sec there's nothing you CAN do except change the timeout. Try querying 20-40 million rows and see how long that takes...
  • JohnIdol
    JohnIdol over 15 years
    it started taking indefinitely long times to execute on management studio as well now ---- haven't a clue!
  • klhurley
    klhurley about 15 years
    I've seen this happen to views too where they need to be recreated after the source table(s) changed. Definitely worth a try.
  • klhurley
    klhurley about 15 years
    Was the PK integer based? If it was, no sense in clustering sequential data (right?). I've had situations in SQL2000 where a table had to be fully recreated before an index would be 'repaired', that even removing/reindexing couldn't fix.
  • JohnIdol
    JohnIdol about 15 years
    Yes it was an integer - it was clustered by default as far as I know (I didn't make it clustered explicitly)
  • Rob
    Rob almost 12 years
    An anonymous user tried editing this answer, which was an inappropriate edit and should have been a comment, so I include his comment here: "We too encounter this problem and used the solution described above while struggling to find the cause, but the cause ended up being SQL Server parameter sniffing. The issue and resolution are described here dannykendrick.blogspot.co.nz/2012/08/…"
  • Jon
    Jon almost 9 years
    Parameter sniffing is most definitely still a problem with SQL Server (up to and including 2012). I've been able to replicate this easily on all versions of SQL. Declaring a local variable for each stored proc parameter and setting Localparam1 = Param1 fixes the timeouts in every instance. It's been bothering me for years.