I have a stored proc in an SQL Azure database. If I run this stored proc via SSMS it always takes around 1 second to run. If I run from a web role application running on Azure with an SqlCommand object it often (not always) runs slowly and eventually ti开发者_开发知识库mes out. What would be the different from running via SSMS or a SqlCommand?
SQL Server will choose and cache an execution plan for the stored procedure. This plan may be optimized for a given set of parameters, and be a poor choice for others.
You should review the execution plan it is choosing and assess whether it's adequate for your most common or most heavy queries.
Possible solutions include creating (or dropping) indexes as necessary, or using optimization hints.
Query optimization is more complex and subtle that it would seem at first. Some helpful references:
- Parameter Sniffing (or Spoofing) in SQL Server
- Slow in the Application, Fast in SSMS? Understanding Performance Mysteries
- I Smell a Parameter!
- SQL Server 2008 Query Performance Tuning Distilled
精彩评论