Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

paginate very slow on large queries with :group

#255

In finder.rb, wp_count() passes off to count() in order to figure out how many total results exist. When :group is specified, count() uses execute_grouped_calculation() in ActiveRecord::Calculations, which calls ActiveSupport::OrderedHash[]= . ActiveSupport::OrderedHash[]= calls assoc() on the its internal Array, which compares the value passed to assoc() to the first value of each array within its internal Array. As a result, for my application, which has a total of 2,895 matching rows, a total of 4,189,065 == comparisons are made in the course of determining the number of rows.

This is obviously very slow. Is there be a faster way to calculate the number of rows for queries with :group?

Reported by Michael Ragalie · December 10th, 2008 @ 03:37 AM

State: resolved
Milestone: none
Assigned to: Mislav Mislav

Activity

  1. Mislav
    Mislav
    • State changed from new to open

    That's rather unfortunate.

    Does this help?

    
    Model.paginate :page => 1, :per_page => 10,
      :group => 'foo', :count => { :group => nil }
    

    That will cause the count to be performed like the :group option wasn't even there.

    December 11th, 2008 @ 11:45 PM

  2. Michael Ragalie
    Michael Ragalie

    That worked like a charm. All I did was use

    
    Model.paginate :page => 1, :per_page => 10, :group => 'foo.id', :count => {:group => nil, :select => "DISTINCT foo.id"}
    

    and got the expected behavior.

    Thanks for your help!

    December 12th, 2008 @ 12:57 AM

  3. Mislav
    Mislav
    • State changed from open to resolved

    No prob ;)

    December 12th, 2008 @ 02:22 AM

  4. Sebastian
    Sebastian

    I*m sorry. But I think this is absolutly wrong, and will return wrong results.

    When you group something by a specific value, and then count without the group option as described above you'll get the count of all ungrouped results.

    I don't know about a better workaround to speed things up.

    February 6th, 2009 @ 12:12 AM

  5. Mislav
    Mislav

    You may be right.

    But still, little can be done from will_paginate. Maybe you guys should open a Rails ticket? On the other hand, the internal implementation of OrderedHash was rewritten and now the problem may be gone (better check to be sure)

    February 6th, 2009 @ 01:32 AM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.