预加载数据以加快查看加载时间 - rails

时间:2017-04-19 01:27:11

标签: ruby-on-rails eager-loading

我正在研究使用rails的技术以加快加载时间。我现在正在使用一个页面,它在渲染时渲染大量数据。我试图逐步完成每个负载,看看我是否可以优化时间。

我当前遇到的问题是这个;

 (5.4ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23022]]
   (4.4ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23023]]
   (4.2ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23024]]
   (6.0ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23029]]
   (7.1ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23121]]
   (6.0ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23180]]
   (4.2ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23229]]
   (4.2ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23250]]
   (6.6ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23254]]
   (4.4ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23255]]
   (4.0ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23259]]
   (6.5ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23154]]
   (6.6ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23177]]
   (5.1ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23201]]
   (4.2ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23221]]
   (4.1ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23232]]
   (7.9ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23235]]
   (4.3ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23237]]
   (4.1ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23238]]
   (5.9ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23248]]
   (7.8ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23295]]
   (6.8ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23119]]
   (4.2ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23123]]
   (4.1ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23178]]
   (7.9ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23231]]
   (4.4ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23239]]
   (4.3ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23240]]
   (6.4ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23241]]
   (5.1ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23243]]
   (6.7ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23245]]
   (4.3ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23247]]
   (4.2ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23249]]
   (6.9ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23251]]
   (4.4ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23256]]
   (4.2ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23257]]
   (6.5ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23275]]
   (6.4ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23350]]
   (5.0ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23272]]
   (4.2ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23296]]
   (4.0ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23401]]
   (8.8ms)  SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1  [["rsvp_id", 23347]]

我从这段代码中得到了这个:

def number_attending
  rsvp.attendees.count
end 

所以RSVPS有很多与会者。我在这里称这种方法。

<% rsvps.each do |rsvp| %>
 <span class="event-response__indicator event-response__indicator--guests" title="<%= rsvp.number_attending %> Guests">
<% end %>

所以问题是。我不想在页面加载时遍历每个rsvp和参与者。有没有办法预加载这些数据。像Eagerloading这样的东西?如果是这样,我怎么能用给出的信息来实现呢?

2 个答案:

答案 0 :(得分:0)

结帐this link, 这专门用于避免n + 1次计数查询。

这解释了执行连接查询或为同一个

创建视图

答案 1 :(得分:-1)

嗯,您没有显示如何查询rsvps,因此我将其视为rsvps = Rsvp.all。您只需添加includes方法,就像这样rsvps = Rsvp.includes(:attendees).all

此外,我不确定在运行此行rsvp.attendees.count时是否会避免查询。我遇到了count方法触发查询但某个size方法没有触发查询的情况,因此您可能需要将此行替换为rsvp.attendees.size