0

I have a basic show action and a data set with less than 500 objects.

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

I haven't done any performance testing or optimization yet, but I can't understand why my show action is so slow with such a small data set.

BTW, I'm using the app locally.

Edit: 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)

View

<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>
4

1 回答 1

0
Report.find(params[:id])

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

另外,注释掉下面的语句,看看它是否引起了它,然后研究如何构造查询。

@keywords = @report.keywords.all(:order => "total_value DESC", :limit => 5)
于 2013-05-16T15:10:00.587 回答