减少kaminari的查询次数

时间:2016-03-07 07:02:11

标签: ruby-on-rails performance kaminari

给出以下代码:

# items_controller
class ItemsController < ApplicationController
  def show
    @files = @item.essences.page(params[:files_page]).per(params[:files_per_page])
  end
end

# items/_essences.html.haml

= paginate @files, :param_name => :files_page

%table.table
  %thead
    %tr
      = sortable :foo
      = sortable :bar
      = sortable :baz
      = sortable :aaa
      %th Blah
  %tbody
    - if @files.empty?
      %tr
        %td no files available
        %td
        %td
        %td
        %td
    - else
      - @files.each do |file|
        %tr
          %td= file.foo
          %td= file.bar
          %td= file.baz
          %td= file.aaa
          %td= Blah
      %tr
        %td
          %b== #{@files.count} files
        %td== --
        %td
          %b= number_to_human_size @files.sum(&:size)
        %td== --
        %td== --

= paginate @files, :param_name => :files_page

%p
  %button.per_page{:data => {:per => 10, :param_name => :files_per_page}} Show 10
  %button.per_page{:data => {:per => 50, :param_name => :files_per_page}} Show 50
  %button.per_page{:data => {:per => @num_files, :param_name => :files_per_page}} Show all

我正在查看rack-mini-profiler并查看正在生成的SQL查询

Executing action: show
T+2450.2 ms
Reader
1.1 ms
app/controllers/items_controller.rb:72:in `show'
SELECT `essences`.* FROM `essences`  WHERE `essences`.`item_id` = 729

Rendering: items/_essences
T+2991.6 ms
Reader
0.4 ms
app/views/items/_essences.html.haml:2:in `_app_views_items__essences_html_haml__2548715375674979383_70349081205780'
app/views/items/show.html.haml:157:in `_app_views_items_show_html_haml__802676714698420534_70349066386680'
app/controllers/items_controller.rb:81:in `show'
SELECT COUNT(*) FROM `essences`  WHERE `essences`.`item_id` = 729   

Rendering: items/_essences
T+3045.4 ms
Reader
0.6 ms
app/views/items/_essences.html.haml:13:in `_app_views_items__essences_html_haml__2548715375674979383_70349081205780'
app/views/items/show.html.haml:157:in `_app_views_items_show_html_haml__802676714698420534_70349066386680'
app/controllers/items_controller.rb:81:in `show'
SELECT COUNT(count_column) FROM (SELECT  1 AS count_column FROM `essences`  WHERE `essences`.`item_id` = 729 LIMIT 25 OFFSET 0) subquery_for_count

Rendering: items/_essences
T+3049.4 ms
Reader
1.3 ms
app/views/items/_essences.html.haml:21:in `_app_views_items__essences_html_haml__2548715375674979383_70349081205780'
app/views/items/show.html.haml:157:in `_app_views_items_show_html_haml__802676714698420534_70349066386680'
app/controllers/items_controller.rb:81:in `show'
SELECT  `essences`.* FROM `essences`  WHERE `essences`.`item_id` = 729 LIMIT 25 OFFSET 0   

查看结果,似乎有一些冗余的SQL调用。

paginate @files, :param_name => :files_page来电之后,不应该计算可用的页数,因此不应该知道@files.empty?是否为真?

没有计算的原因是,为避免@files.empty?的SQL调用而获得的性能是否值得? (我是优化Rails应用程序的新手,所以也许查询只花了0.6毫秒意味着它不值得担心)

我意识到ItemsController#show@files.each都选择了essences.*,但这似乎是Rails应用程序编码器的错误,而不是Kaminari的错误。

我正在使用Rails 3.2,ActiveRecord和最新版本的Kaminari。

0 个答案:

没有答案