views:

78

answers:

5

I have a stored procedure that is called by a website to display data. Today the web page has started timing out so I got profiler going and saw the query that was taking too long. I then ran the same query in management studio, under the same user login, and it takes less than a second to return.

Is there anything obvious that could be causing this? I can't think of a reason why when ASP calls the stored proc it takes 30 secs but when I call it it's fine.

Thanks

A: 

I guess, there might be two reasons:

  1. Network problem
  2. Parameter sniffing
Cagdas
A: 

Sounds like a dead lock.

Chris Bednarski
No it doesn't! That would cause one of the participating transactions to be rolled back and you'd get an error message telling you that a deadlock had occurred.
Martin Smith
+1  A: 

This is usually because some of the SET-tings differ between the Management Studio connection and the ASP connection, such as SET ARITHABORT. This wouldn't explain why it's only started being problematic today from the website call, but there's a fair chance it's related.

Rob
There's also some good answers in http://stackoverflow.com/questions/2736638/sql-query-slow-in-net-application-but-instantaneous-in-sql-server-management-stu/ that may well be related
Rob
I'm not sure that it is the Settings per se that would explain the speed difference? More usually if you change one of these settings you will get a new or different execution plan so it looks like changing the setting fixed it but actually the culprit was parameter sniffing.
Martin Smith
I experienced this, too, with ARITHABORT. The issue was to query an index on a computed column, which performs better when ARITHABORT is set.
devio
A: 

It seemed to be parameter sniffing... I've stopped the sniffing by assigning the passed in parameters to local variables and it seems to be fine at the moment (i.e. it's running under a second from the website again). It'll be interesting to see if it stays like this or will degrade again.

I had assumed running with the option RECOMPILE would have temporarily 'fixed' the parameter sniffing problem for the query in question but it didn't.

Ah well. Thank you everyone for answering. I'll see what happens

Blootac
If this ends up being the answer, please accept it once you're sure. I mention it since you're new here, and welcome!
rwmnau
A: 

We had a similar issue with our IVR - when I ran a query through SSMS, it returned instantly, but when it was run through a webservice accessed by our IVR, it would time out about 20% of the time - really odd.

I ended up running SQL Profiler to see the queries being submitted and then added some additional indexes per the recommendations of the Index Tuning wizard, which sped up the IVR query to under a second every time. I suspect the problem was also something to do with parameters, and while I didn't compare the execution plan between the two different venues, I suspect they were quite different. SQL Profiler will help you sort this out, though, since you can see the query actually submitted to the engine, as well as the execution plan it uses to fetch the data.

rwmnau