简体   繁体   English

在ASP.NET中查询比在SSMS中查询快10倍

[英]Query 10 x faster in ASP.NET than in SSMS

I am currently analyzing the search function of a web application written in ASP.NET / VB. 我目前正在分析用ASP.NET/VB编写的Web应用程序的搜索功能。 The search function on the home page requests the results from a web service that in turn executes an SQL query and sends back the results. 主页上的搜索功能从Web服务请求结果,该服务依次执行SQL查询并发回结果。

For analyzing the performance, I debugged the application locally and copied the query to SSMS for running it with SET STATISTICS TIME ON. 为了分析性能,我在本地调试了该应用程序,并将查询复制到SSMS以便在SET STATISTICS TIME ON下运行它。

When I run the application and check the response time for the requests to my service in the browser's developer tools, they tend to be around 300ms. 当我运行该应用程序并在浏览器的开发人员工具中检查对我的服务的请求的响应时间时,它们通常约为300毫秒。 When I copy the exact same query to SSMS, set all the parameters manually (eg "DECLARE @SearchTerm VARCHAR = '%foobar%'"), the query runs for anywhere between 1500ms and 2000ms. 当我将完全相同的查询复制到SSMS时,手动设置所有参数(例如“ DECLARE @SearchTerm VARCHAR ='%foobar%'”),查询将在1500ms到2000ms之间的任何时间运行。

I have found countless mentions of queries running slower from an application, but nothing as to why the opposite might happen. 我发现无数提及应用程序运行速度较慢的查询,但是却没有提到相反的原因的可能。

Has anyone had similar experiences in the past or found a possible explanation for this behavior? 有没有人曾经有过类似的经历或找到过这种行为的可能解释?

I have to guess because you have not included the query and its associated query plan. 我不得不猜测,因为您尚未包括查询及其关联的查询计划。 I would guess you are using different values for @SearchTerm (and others) for the query in SSMS versus the ASP page. 我猜想您在SSMS和ASP页面中对查询使用@SearchTerm(和其他)不同的值。 The query had a query plan optimized for the first time you used it based on the parameters passed in and when you run it from SSMS it uses an inappropriate query plan (for the different paramaters being passed in). 该查询的查询计划是针对您首次基于传入的参数进行优化的查询计划,当您从SSMS运行该查询计划时,该查询计划使用了不合适的查询计划(对于要传入的不同参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM