简体   繁体   English

减少到服务器/数据库的往返

[英]Reducing roundtrips to the server/database

When writing ASP.NET pages, what signs do you look for that your page is making too many roundtrips to a database or server? 在编写ASP.NET页面时,您有什么迹象表明您的页面正在向数据库或服务器进行过多的往返?

(This is a general question but I say ASP.NET as the majority of my coding is on the web side of things). (这是一个普遍的问题,但我说ASP.NET,因为我的大部分编码都是在网络方面)。

How much is too much? 多少是太多了? The €1M question! 100万欧元的问题! Profile. 轮廓。 Then profile. 然后简介。 If your app is spending most of its time doing data access, you have a problem (and should look at a sql trace). 如果您的应用程序花费大部分时间进行数据访问,则会出现问题(并且应该查看sql跟踪)。 If it is spending most of its time drawing the UI, then (assuming your view isn't doing data access) you should probably look elsewhere first... 如果它花费大部分时间来绘制UI,那么(假设你的视图没有进行数据访问)你应该先看看其他地方......

Round trips are more relevant to latency than the total quantity of data being moved, so it really does make sense to optimize for them. 往返行程与延迟相关,而不是移动的数据总量,因此优化它们确实有意义。 The usual way is to use stored procedures that do multiple steps, perhaps even returning multiple result sets. 通常的方法是使用执行多个步骤的存储过程,甚至可能返回多个结果集。

What I do is I look at the ASP performance counters and SQL performance counters. 我所做的是查看ASP性能计数器和SQL性能计数器。 To get an accurate measurement you must ensure that there is no random noise activity on the SQL Server (ie. import batches running unrelated to the web site). 要获得准确的度量,您必须确保SQL Server上没有随机噪声活动(即,与网站无关的导入批次)。

The relevant counters I look at are: 我看的相关柜台是:

  • SQL Statistics/Batch requests/sec : This indicates exactly how many Transact-SQL batches the server receives. SQL Statistics / Batch requests / sec :这表明服务器接收的Transact-SQL批次的确切数量。 It can be, in most cases, equated 1:1 with the number of round trips from the web site to SQL. 在大多数情况下,它可以与从网站到SQL的往返次数等于1:1。
  • Databases/Transaction/sec : this counter is instanced per database, so I can quickly see in which database there is 'activity'. Databases / Transaction / sec :这个计数器是按数据库实例化的,所以我可以快速查看哪个数据库有“活动”。 This way I can correlate the web site data roundtrips (ie. my app logic requests, goes to app database) and the ASP session state and user stuff (goes to Asp session db or tempdb) 这样我可以关联网站数据往返(即我的app逻辑请求,转到app数据库)和ASP会话状态和用户资料(转到Asp会话db或tempdb)
  • Databases/Write Transaction/sec : This I correlate with the counters above (transaction per second) so I can get a feel of the read-to-write ratio the site is doing. 数据库/写入事务/秒 :这与上面的计数器(每秒事务)相关联,因此我可以了解站点正在执行的读写比率。
  • ASP.NET Applications/Requests/sec : With this counter I can get the number of requests/sec the site is seeing. ASP.NET Applications / Requests / sec :使用此计数器,我可以获得站点看到的请求数/秒。 Correlated with the number of SQL Batch Requests/sec it gives a good indication of the average number of round-trips per request. 与SQL Batch Requests / sec的数量相关联,它可以很好地指示每个请求的平均往返次数。

The next thing to measure is usually trying to get a feel for where is the time spent in the request. 接下来要测量的是通常试图了解在请求中花费的时间。 On my own project, I use abundantly performance counters I publish myself so is really easy to measure. 在我自己的项目中,我使用了很多我自己发布的性能计数器,所以很容易测量。 But I'm not always so lucky as to clean up only my own mess... Profiling is usually not an option for me because I most times troubleshoot live production systems I cannot instrument. 但是我并不总是那么幸运,只能清理我自己的烂摊子......对我来说,性能分析通常不是一个选择,因为我大多数情况下对我无法检测的现场制作系统进行故障排除。

My approach is to try to sort out the SQL side of things first, since it's easy to find the relevant statistics for execution times in SQL: SQL keeps a nice aggregated statistic ready to look at in sys.dm_exec_query_stats . 我的方法是首先尝试解决SQL方面的事情,因为很容易在SQL中找到执行时间的相关统计信息:SQL保留了一个很好的聚合统计信息,可以在sys.dm_exec_query_stats中查看。 I can also use Profiler to measure execution duration in real time. 我也可以使用Profiler实时测量执行持续时间。 With some analysis of these numbers collected, knowing the normal request pattern of the most visited pages, you can give a pretty good estimate of the total time spent in SQL per web request. 通过对收集到的这些数字的一些分析,了解访问量最大的页面的正常请求模式,您可以非常好地估计每个Web请求在SQL中花费的总时间。 If this times adds up to nearly all the time it takes a request to serve the page, then you have your answer. 如果此次加起来几乎所有时间都需要提供服务页面,那么您就得到了答案。

And to answer the original question title: to reduce the number of round-trips, you make fewer requests. 并回答原始问题标题:减少往返次数,减少请求。 Seriously. 认真。 First, caching what is appropriate to cache I guess is obvious. 首先,缓存我认为适合缓存的内容是显而易见的。 Second you reduce the complexity: don't display unnecessary data on each page, you cache and display stale data when you can get away with it, you hide details on secondary navigation panels. 其次,您可以降低复杂性:不要在每个页面上显示不必要的数据,当您可以使用它时缓存和显示陈旧数据,您可以隐藏辅助导航面板上的详细信息。

If you feel that the problem is the number of round-trips per se as opposed to the number of requests (ie. you would benefit tremendously from batching multiple requests in one round-trip), then you should somehow measure that the round-trip overhead is what's killing you. 如果您认为问题是往返次数本身而不是请求数量(即,您将通过在一次往返中批量处理多个请求而获益极大),那么您应该以某种方式衡量往返行程开销正在扼杀你。 With connection pooling on a normal network connection this is usually not the most important factor. 通过正常网络连接上的连接池,这通常不是最重要的因素。

And finally you should look if everything that can be done in sets is done in sets. 最后你应该看看是否可以在集合中完成所有可以完成的事情。 If you have some half-brained ORM that retrieves objects one at a time from an ID keyset, get rid of it. 如果你有一些半脑的ORM,它可以从ID键集中一次检索一个对象,那就去除它。

I know that this may sound reiterative, but client server round trips depends of how many program logic is located at any side of the connection. 我知道这可能听起来很反复,但客户端服务器往返取决于连接任何一侧有多少程序逻辑。

First thing to check is validation: You have to validate and sanitize your input at server side always, but it does not means that you cannot do it too at client side too reducing a round trips that are been used only too check input. 要检查的第一件事是验证:您必须始终在服务器端验证和清理您的输入,但这并不意味着您也不能在客户端执行此操作,这也减少了仅用于检查输入的往返。

At second: What can you do at client side to reduce server side overload? 第二:在客户端你能做些什么来减少服务器端过载? There are calculations that you can check or make at client side. 您可以在客户端检查或制作计算。 There is also AJAX that can be used to load only a percentage of the page that is changing. 还有一些AJAX可用于仅加载正在更改的页面的一定百分比。

At third: Can you delegate work to another server? 第三:你可以将工作委托给另一台服务器吗? If your server is too loaded, why not to use web services or simply delegate some side of the logic to another server? 如果您的服务器负载过重,为什么不使用Web服务或只是将逻辑的某一方委托给另一台服务器?

As Mark wrote: ¿How is too much? 马克写道:¿怎么样太多了? It is is up to you and your budget. 这取决于您和您的预算。

When writing ASP.NET pages, what signs do you look for that your page is making too many roundtrips to a database or server? 在编写ASP.NET页面时,您有什么迹象表明您的页面正在向数据库或服务器进行过多的往返?

Of course it all depends and you have to profile. 当然这一切都取决于你必须描述。 However, here are some indicators, they do not mean there is a problem, but often will indicate 但是,这里有一些指标,它们并不意味着存在问题,但往往会表明

  • Page is taking a very long time to render locally. Page需要很长时间才能在本地呈现。

  • To render the page you need more than 30 round trips. 要渲染页面,您需要超过30次往返。 I pulled that number out of my hat, but assuming a round trip is taking about 3.5ms then 30 round trips will kick you over the 100ms guideline (before any other kind of processing). 我把这个数字从我的帽子中取出来,但假设往返行程需要大约3.5毫秒,那么30次往返将使你超过100毫秒的准则(在任何其他类型的处理之前)。

  • All the queries involved in rendering the page are heavily optimized and do not take longer than a millisecond or two to execute. 渲染页面所涉及的所有查询都经过了大量优化,执行时间不会超过一毫秒或两秒。 There are no operations that require lots of CPU cycles that execute every time you render the page. 每次渲染页面时都不需要执行大量CPU周期的操作。

  • Data access is abstracted away and not cached in any kind of way. 数据访问被抽象出来,而不是以任何方式缓存。 If, for example, GetCustomer will call the DAL which in turn issues a query and your page is asking for 100 Customer objects which are not retrieved in a batch, you are probably in trouble. 例如,如果GetCustomer将调用DAL,而DAL又发出查询,并且您的页面要求批量检索的100个Customer对象,则可能遇到麻烦。

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

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