基本的ActiveRecord查询似乎很慢

时间:2013-05-16 14:57:26

标签: ruby-on-rails performance

我有一个基本的show动作和一个少于500个对象的数据集。

def show
  @report = Report.find(params[:id])
  @keywords = @report.keywords.order("total_value DESC")
end

我还没有进行任何性能测试或优化,但是我无法理解为什么我的show动作如此缓慢,只有这么小的数据集。

BTW,我在本地使用该应用程序。

编辑: development.log

Started GET "/reports/2" for 127.0.0.1 at 2013-05-16 11:26:33 -0400
Processing by ReportsController#show as HTML
  Parameters: {"id"=>"2"}
  [1m[36mReport Load (0.2ms)[0m  [1mSELECT "reports".* FROM "reports" WHERE "reports"."id" = ? LIMIT 1[0m  [["id", "2"]]
  [1m[35mKeyword Load (1.3ms)[0m  SELECT "keywords".* FROM "keywords" WHERE "keywords"."report_id" = 2 ORDER BY total_value DESC
  Rendered reports/show.html.erb within layouts/application (19552.2ms)
  Rendered layouts/_header.html.erb (0.8ms)
Completed 200 OK in 19777ms (Views: 19773.0ms | ActiveRecord: 1.5ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /application.css - 304 Not Modified (39ms)


Started GET "/assets/pages.css?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /pages.css - 304 Not Modified (0ms)


Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /scaffolds.css - 304 Not Modified (0ms)


Started GET "/assets/styles.css?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /styles.css - 304 Not Modified (2ms)


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /jquery.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-transition.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-transition.js - 304 Not Modified (0ms)


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /jquery_ujs.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-affix.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-affix.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-alert.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-alert.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-button.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-button.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-carousel.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-collapse.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-dropdown.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-modal.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-modal.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-scrollspy.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-tab.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-tab.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-tooltip.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-popover.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-popover.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap-typeahead.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /bootstrap.js - 304 Not Modified (0ms)


Started GET "/assets/keywords.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /keywords.js - 304 Not Modified (0ms)


Started GET "/assets/pages.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /pages.js - 304 Not Modified (0ms)


Started GET "/assets/reports.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /reports.js - 304 Not Modified (0ms)


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-05-16 11:26:52 -0400
Served asset /application.js - 304 Not Modified (1ms)


Started GET "/assets/logo.png" for 127.0.0.1 at 2013-05-16 11:26:53 -0400
Served asset /logo.png - 304 Not Modified (0ms)

查看

<table class ="table table-striped">
  <tr>
    <th>Keyword</th>
    <th>Avg position</th>
    <th>Total revenue</th>
  </tr>

  <% @keywords.each do |keyword| %>
      <tr>
        <td><%= keyword.description %></td>
        <td><%= keyword.avg_position %></td>
        <td><%= keyword.total_value %></td>
      </tr>
  <% end %>
</table>

1 个答案:

答案 0 :(得分:0)

Report.find(params[:id])

以上查询使用数据库/模型的primvary键。 这通常应该是最快的查询,如果它的速度慢,首先检查你的模型,看看你是否有“ID”/主键字段。 2,我会检查“tail -n 100 log / production.log或development.log

另外,请注释掉以下语句,看看它是否有用,然后研究如何构造查询。

@keywords = @report.keywords.all(:order => "total_value DESC", :limit => 5)