views:

345

answers:

3

I have a query from a web site that takes 15-30 seconds while the same query runs in .5 seconds from SQL Server Management studio. I cannot see any locking issues using SQL Profiler, nor can I reproduce the delay manually from SSMS. A week ago, I detached and reattached the database which seemed to miraculously fix the problem. Today when the problem reared its ugly head again, I tried merely rebuilding the indexes. This also fixed the problem. However, I don't think it's necessarily an index problem since the indexes wouldn't be automatically rebuilt on a simple detach/attach, to my knowledge.

Any idea what could be causing the delay? My first thought was that perhaps some parameter sniffing on the stored procedure being called (said stored proc runs a CTE, if that matters) was causing a bad query plan, which would explain the intermittent nature of the problem. Since both detaching / reattaching and an index rebuild should theoretically invalidate the cached query plan, this makes sense, but I'm unsure how to verify this. Additionally, why wouldn't the same query (copied directly from SQL Profiler with the exact same parameters) exhibit the same delay when run manually through SSMS?

Any thoughts?

+4  A: 

If a bad plan is cached then the same bad plan should be used from SSMS too, if you run the very same query with identical arguments.

There cannot be better solution that finding the root cause. Trying to peek and poke various settings in the hope it fixes the problem will never give you the confidence it is actually fixed. Besides, next time the system may have a different problem and you'll believe this same problem re-surfaced and apply a bad solution.

The best thing to try is to capture the bad execution plan. Showplan XML Event Class Profiler event is your friend, you can get the plan of the ADO.Net call. This is a very heavy event, so you should attach profiler and capture it only when the problem manifests itself, in a short session.

Query IO statistics can also be of help. RPC:Completed and SQL: Batch Completed events both include Reads and Writes so you can compare the amount of logical IO performed by ADO.Net invocation vs. SSMS one. Large difference (for exactly the same query and params) indicate different plans.sys.dm_exec_query_stats is another avenue of investigation. You can find your query plan(s) in there and inspect the execution stats.

All these should help establish with certitude if the problem is a bad plan or something else, to start with.

Remus Rusanu
The number of reads for the "bad" query was significantly higher than the number of reads for the query from SSMS. I too thought that SSMS should use the same query plan if the exact same query was entered, but is there a scenario in which this may not be the case?
Chris
If any @parameter *types* are different or if the connection settings are different then the plan cannot be reused and SSMS will compile its own plan. Does your query has any @variables in it?
Remus Rusanu
I bet it was the connection settings. I was logging in to SSMS using windows auth while ASP.NET uses the sql user. That clears that up. Now, on to catching the bad execution plan in action.. :(
Chris
Nowdays SQL Server too has the ability to lock plans with the `USE PLAN` hint, see http://msdn.microsoft.com/en-us/library/ms186954.aspx
Remus Rusanu
A: 

Is it possible that your ADO.NET query is running after the system has been busy doing other things, so that the data it needs is no longer in RAM? And when you test on SSMS, it is?

You can check for that by running the following two commands from SSMS before you run the query:

CHECKPOINT
DBCC DROPCLEANBUFFERS

If that causes the SSMS query to run slowly, then there are some tricks you can play on the ADO.NET side to help it run faster.

RickNZ
+2  A: 

I have been having the same problem. The only way i can fix this is setting ARITHABORT ON. but unfortunatley when it occurs again i Have to set ARITHABORT OFF.

I have no clue what ARITHABORT has to do with this but it works, I have been having this problem for over 2 years now with still no solution. The databses i am working with are over 300GB so maybe it is a size issue...

The closest i got to resolving this problem was from an earlier post Google Groups post

Let me know if you have managed to completely solve this problem as it is very frustrating!

Eli Perpinyal