ruby on rails - Reducing the number of queries kaminari makes -
given following code:
# items_controller class itemscontroller < applicationcontroller def show @files = @item.essences.page(params[:files_page]).per(params[:files_per_page]) end end
and
# 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 |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
i'm looking through rack-mini-profiler , seeing sql queries getting generated
executing action: show t+2450.2 ms reader 1.1 ms app/controllers/items_controller.rb:72:in `show' select `essences`.* `essences` `essences`.`item_id` = 729
and
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(*) `essences` `essences`.`item_id` = 729
and
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) (select 1 count_column `essences` `essences`.`item_id` = 729 limit 25 offset 0) subquery_for_count
and
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`.* `essences` `essences`.`item_id` = 729 limit 25 offset 0
looking @ results, there seem redundant sql calls.
after paginate @files, :param_name => :files_page
call made, shouldn't number of pages available calculated, , therefore shouldn't known whether or not @files.empty?
true or not?
is reason it's not calculated performance gained on avoiding sql call @files.empty?
not worthwhile? (i'm new optimizing rails applications, maybe fact query takes 0.6ms means it's not worth worrying about)
i realize itemscontroller#show
, @files.each
both select essences.*
, seems mistake on rails' application coder's part, not upon kaminari's part.
i'm using rails 3.2, , activerecord, , latest version of kaminari.
Comments
Post a Comment