如何调试在rails中渲染这么长的部分?

时间:2015-06-02 08:55:25

标签: ruby-on-rails

  Rendered cars/_handover_instruction_fields.html.slim (5.5ms)
  Rendered cars/_handover_instruction_fields.html.slim (0.6ms)
  Rendered cars/_handover_instruction_fields.html.slim (0.5ms)
  Rendered cars/_handover_instruction_fields.html.slim (0.5ms)
  Rendered cars/_handover_instruction_fields.html.slim (0.5ms)
  Rendered cars/_handover_instruction_fields.html.slim (0.5ms)
  Rendered cars/_edit_handover.html.slim (30.7ms)
  Rendered cars/_edit_vrd.html.slim (5.2ms)
  Rendered cars/_edit_features.html.slim (3.5ms)
  Rendered cars/_edit_description.html.slim (5.2ms)
  CarPhoto Load (0.5ms)  SELECT "car_photos".* FROM "car_photos" WHERE "car_photos"."car_id" = $1  [["car_id", 19]]
  Rendered cars/_photo_fields.html.slim (217.0ms)
  Rendered cars/_photo_fields.html.slim (1.5ms)
  Rendered cars/_photo_fields.html.slim (1.1ms)
  Rendered cars/_photo_fields.html.slim (1.1ms)
  Rendered cars/_photo_fields.html.slim (1.1ms)
  Rendered cars/_photo_fields.html.slim (1.0ms)
  Rendered cars/_photo_fields.html.slim (1.1ms)
  Rendered cars/_photo_fields.html.slim (1.0ms)
  Rendered cars/_edit_photos.html.slim (254.6ms)
  Rendered cars/_edit_js.html.erb (0.5ms)
  Rendered cars/edit.html.slim within layouts/application (356.7ms)
  Rendered shared/_flash.html.slim (5.5ms)
  Rendered shared/_slideout_nav.html.slim (16.9ms)
  Rendered svgs/_carshare_typeface_blue_small.html (0.8ms)
  Rendered shared/_header_nav.html.slim (20.1ms)

当我在webapp中渲染其中一个页面时,上面是Rails.logger的摘录。我意识到有些部分需要花费很长时间才能呈现。

例如

  Rendered cars/_photo_fields.html.slim (217.0ms)
  Rendered cars/_photo_fields.html.slim (1.5ms)

我想找出是什么让渲染如此之长以及如何改进它。

2 个答案:

答案 0 :(得分:0)

<% benchmark("Showing projects partial") do %>
  <%= render @projects %>
<% end %>

看起来很有趣。

一般来说,如果您有许多@items,请不要每次都致电render。这可以节省您的时间。

@items.each do |item|
  render 'one_item', :item => item
end

例如,使用集合渲染一次。

答案 1 :(得分:0)

检查日志,它将显示正在生成的数据库查询。这通常是第一个查看的地方,它将突出显示您可以通过优化查询来提高性能的位置。显而易见的是检查您是否可以使用包含N + 1查询。

您还可以采取其他步骤,例如添加索引,使用pluck,如果您只需要获取单个属性,使用LIMIT并避免排序。