views:

104

answers:

2

I have a webpage that takes 10 minutes to run one query against a database, but the same query returns in less than a second when run from SQL Server Management Studio.

The webpage is just firing SQL at the database that is executing a stored procedure, which in turn is performing a pretty simple select over four tables. Again the code is basic ADO, setting the CommandText on an SqlCommand and then performing an ExecuteReader to get the data.

The webpage normally works quickly, but when it slows down the only way to get it speeded up is to defragment the indexes on the tables being queried (different ones different times), which doesn't seem to make sense when the same query executes so quickly manually.

I have had a look at this question but it doesn't apply as the webpage is literally just firing text at the database.

Does anyone have any good ideas why this is going slow one way and not the other? Thanks

+2  A: 

I would suspect parameter sniffing.

The cached execution plan used for your application's connection probably won't be usable by your SSMS connection due to different set options so it will generate a new different plan.

You can retrieve the cached plans for the stored procedure by using the query below. Then compare to see if they are different (e.g. is the slow one doing index seeks and bookmark lookups at a place where the other one does a scan?)

Use YourDatabase;

SELECT *
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
CROSS APPLY sys.dm_exec_query_plan(plan_handle) 
cross APPLY sys.dm_exec_plan_attributes(plan_handle) AS epa
where sys.dm_exec_sql_text.OBJECTID=object_id('YourProcName') 
         and attribute='set_options'
Martin Smith
The 2 execution plans seem very similar, but would that be because I have done the index defrag already?
Iain Hoult
That would cause the plans to be recompiled. Presumably there is no significant difference in timings between the two at the moment either?
Martin Smith
It's always difficult to spot what's wrong when it's working fine again ;) No real differences in timings, thanks
Iain Hoult
Assuming this is the problem, what do you think needs doing to stop it happening every couple of weeks?
Iain Hoult
@Iain - You can use the `OPTIMIZE FOR` hint so that when the procedure is compiled you specify the parameter values it is compiled with rather than leaving it to chance.
Martin Smith
+1  A: 

Is there any difference between the command text of the query in the app and the query you are executing manually? Since you said that reindexing helps performance (which also updates statistics), it sounds like it may be getting stuck on a bad execution plan.

You might want to run a sql trace and capture the showplanxml event to see what the execution plan looks like, and also capture sql statement complete (though this can slow the server down if a lot of statements are coming through the system so be careful) to be sure the statement sent to SQL server is the same one you are running manually.

Roger Harvest
The sql is identical. I shall try out profiling the execution plans next time it goes slow, thanks
Iain Hoult