Stored procedures are timing out intermittently!
Asked Answered
Z

9

8

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!

Zillah answered 5/2, 2009 at 11:17 Comment(3)
Is it running much longer than expected? Or is it simply that it runs for ages but completes in query analyzer?Backflow
it runs very fast in management studio that's why I don't think setting the timeout will helpZillah
Well, you need to figure out if its blocked, or if you have a bad execution plan ...Utley
Z
2

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.

Zillah answered 24/2, 2009 at 21:57 Comment(2)
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.Radiology
Yes it was an integer - it was clustered by default as far as I know (I didn't make it clustered explicitly)Zillah
G
10

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'

Gagliano answered 5/2, 2009 at 11:26 Comment(4)
worth a shot! how do I do that?Zillah
the best is to EXEC sp_recompile 'proc_name'Gagliano
I've seen this happen to views too where they need to be recreated after the source table(s) changed. Definitely worth a try.Radiology
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/…"Teodora
C
7

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.

Clearing answered 5/2, 2009 at 11:19 Comment(8)
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 codeTwedy
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.Zillah
Connection timeout and command timeout should not be confused, connection timeout is the max time it would take to connect to the dbUtley
@Zillah 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.Clearing
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.Manger
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...Gifted
@Manger - Yes, but this would explain the difference between running in management studio and running in the code which was the question. The question was not about how to optimise his SP/query.Clearing
it started taking indefinitely long times to execute on management studio as well now ---- haven't a clue!Zillah
B
6

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)

Backflow answered 5/2, 2009 at 11:22 Comment(2)
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.aspxTwedy
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.Dulse
U
3

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

Utley answered 5/2, 2009 at 11:19 Comment(3)
I am doing pretty extensively transactions - it was all good till a few hours ago - not too sure setting a higher timeout will help.Zillah
You can set it to 0 meaning it will not timeoutUtley
Also, if its fast in studio, maybe a transaction is blocking it. you need to diagnose with sp_lockUtley
Z
2

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.

Zillah answered 24/2, 2009 at 21:57 Comment(2)
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.Radiology
Yes it was an integer - it was clustered by default as far as I know (I didn't make it clustered explicitly)Zillah
M
1

You might need to update statistics on the database. Also has indexing on the table changed recently?

Check the execution plan of the sp to see if you can find the bottleneck. Even if it ran ok before, it can probably be tuned to run more efficiently.

Also how much data are you returning? We have had issues with poorly designed SQL in the past that didn't show up until the cumulative report starting having more data in the result set. Not knowing wht your sps do, it is hard to say if this is a possibilty, but it is worth mentioning for you to investigate.

Manger answered 5/2, 2009 at 15:53 Comment(0)
C
1

SQL Server will wait indefinitely before returning to the user. More than likely there was a client side timeout property set. For example you can set a timeout property for the ADO command object.

Choreodrama answered 6/2, 2009 at 11:51 Comment(0)
B
0

Get the SQL profiler on it, compare results between running it in Management studio and via your app.

Brauer answered 19/8, 2010 at 15:58 Comment(0)
P
0

In my case I just reorganized my cluster index of the operation table, the timeout problem resolved. Also the select * from table query time reduced to 2 sec, where before reorganize index was almost 30 sec +

Pinnace answered 2/5, 2020 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.