Query runs fast in Query Analyzer but slow in C# application

Fred picture Fred · Sep 18, 2012 · Viewed 9.9k times · Source

Possible Duplicate:
sql runs fast in ssms slow in asp.net

Using SQL Server 2005, there is a sql query on a few big tables and it times out in the program. However, when I run it in Query Analyzer, it finishes in about 30 seconds.

Why the difference?

Answer

Aaron Bertrand picture Aaron Bertrand · Sep 18, 2012

Usually these are SET setting differences, leading to different plans. In Management Studio the procedure is probably running with the same settings as were in place when it was created.

Take a look at sys.dm_exec_sessions for your ASP.Net application and for your SSMS session. I will hazard a guess that at least one of your SET settings is different. This can contribute to different plans (ultimately this gets attributed to parameter sniffing) and the app side usually ends up worse off.

See these other questions for a lot more details:

And also read Erland Sommarskog's article, Slow in the Application, Fast in SSMS?