sql runs fast in ssms slow in asp.net

浪尽此生 提交于 2019-11-26 14:38:40

Chances are that you are hitting on a problem with parameter sniffing.

I suggest reading Slow in the Application, Fast in SSMS? by Erland Sommarskog to get a full understanding of the issue (long article but very good).

Aaron Bertrand

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:

Stored procedure slow when called from web, fast from Management Studio

Procedure times out from ADO.NET but not in SSMS

Query times out when executed from web, but super-fast when executed from SSMS

ADO .NET vs. SQL Server Management Studio - ADO performs worse

I had same issue, in my case it was related to MARS, so I removed MultipleActiveResultSets=True; from connection string and now running time are almost same (0.2s difference comparing to 4.5s)

Note: MARS = Multiple Active Result Sets. If you set this property on a connection string, you can run multiple queries on the same connection in an interleaved fashion. It's mainly intended to permit you to submit UPDATE statements as you are iterating through a result set.

For what its worth, very occasionally we run into the same problem; may be once a year. You can spend a good week reading and digesting all of those wonderful resources mentioned in the other answers, or you can do what we do; stop and start SQL Server.

It works a treat.

We have noticed that this problem generally occurs after various schema/sp/view mods that may not be directly related to the problem at hand.

Sharath

Are you using any orm? If you are using nhibernate, you can enable db tracing in nhibernate and see what could be the issue. Following are some of the scenarios that I observed in such scenarios:

  1. implicit conversion which leads to bad plan choice (nvarchar being used instead of varchar). You can observe the nhibernate parameter mapping in its logs.
  2. Lack of index

Nhibernate uses log4net and you just need to add an appender as mentioned here: https://devio.wordpress.com/2010/04/14/logging-sql-statements-generated-by-nhibernate/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!