Kaminari (or any pagination) is slow on Joined query
Asked Answered
C

0

7

I'm paginating the following query with Kaminari (though I get similar results with will_paginate):

Person.joins([:locations=>:location_hour], :friends, :current_images).where(sql_conditions_string).page(params[:page]).per(10)

Everything works as expected but pagination adds a database call

SELECT COUNT(*) FROM 'people' INNER JOIN 'location_histories'...

it's calling count(*) on the whole query. This takes 1.3 seconds whereas the original query (including the exact same joins and .where conditions) takes only ~.5 seconds.

Is there a way for pagination to just use the count from the original query? That last database call is a big performance problem.

I'm on Rails 3.0.7.

Thanks!

Conall answered 12/5, 2011 at 18:5 Comment(1)
Yeah i've just spotted this same issue, using Kaminari, in my own performance tuning. I can see the main query actually being called 4 times in various formats, with and without counts etc adding an additional ~2s to each page load. Digging further to see what i can find. Did you come to any conclusions with this?Kerguelen

© 2022 - 2024 — McMap. All rights reserved.