Rails-请求的完成时间明显高于View和DB时间 [英] Rails - Completed time for a request significantly higher than View and DB times

查看:45
本文介绍了Rails-请求的完成时间明显高于View和DB时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,它从一个相当复杂的关系查询中返回一个JSON或XML,同时还包含一些控制器逻辑.

I have a controller that returns JSON or XML from a fairly complex relational query with some controller logic as well.

我通过优化查询并确保我的索引对我的查询是正确的,从而在数据库方面进行了调整.

I've tuned on the DB side by refining my query and making sure my indexes are correct for my query.

在我的日志中,我看到这样的项目:

In my log I see items like this:

Completed in 740ms (View: 1, DB: 50)

因此,如果我理解正确,则意味着视图需要1秒钟的时间来渲染,而数据库查询为50ms.所有剩余时间都在控制器中吗?我试图绕过我的控制器逻辑,只是将我的to_json和to_xml留在那儿,它同样慢.作为参考,我平均返回的JSON结果集为168k.

So if I understand correctly this means the view took 1 second to render and the DB query was 50ms. Is all the remaining time in the controller? I've tried bypassing my controller logic and just leaving my to_json and to_xml in there and it is just as slow. As a point of reference my average returned JSON result set is 168k.

还有其他步骤可以及时完成吗?它包括到网络传输的最后一个字节为止的时间吗?

Are there other steps that go into the Completed in time? Does it include time until last byte for the network transfer?

更新:我将控制器的各个部分包装在基准测试块中:

Update: I wrapped various parts of my controller in benchmarking blocks:

self.class.benchmark("Active Record Find") do
  #my query here
end

我发现的是,即使日志行显示DB:50,我的活动记录发现也几乎占用了所有剩余时间.因此,现在我对于DB号的含义以及基准线为何会说〜600ms而DB:时间将为〜50感到困惑.

What I found was that even though the log line says DB: 50 my active record find is taking almost all of the remaining time. So now I'm confused as to what that DB number means and why the benchmark line will say ~ 600ms but the DB: time will be ~50.

谢谢

推荐答案

您的数据库编号是实际上在数据库中花费的时间,但未加载ActiveRecord对象.

Your DB number is time actually spent in the database, but not loading ActiveRecord objects.

因此,如果您要加载168,000个ruby active_record对象以进行渲染,然后以JSON形式呈现,那么这将解释您的550毫秒(或更长时间!)

So if you're loading 168,000 ruby active_record objects to render then as JSON, this would explain your 550 ms (or more!)

这篇关于Rails-请求的完成时间明显高于View和DB时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆