#88 ✓resolved
Georg Ledermann

Pagination: Count optimization

Reported by Georg Ledermann | May 25th, 2009 @ 12:08 PM

It seems that the helpers for pagination are requesting the "count" from the database even if it's not needed.

Think about this: The given collection contains fewer elements than per_page AND there is no LIMIT statement (because :page param is not there). In this situation the count should not be calculated, because it will be same like collection.length.

In addition, regarding the tuturial (http://www.binarylogic.com/2008/9/7/tutorial-pagination-ordering-an...: It would be much better if the count-calculation is made implicit in the Searchlogic internals, the controller should NOT calculate the count (because it's not always needed, see above).

So far as I know, will_paginate implements this optimization.

Comments and changes to this ticket

  • Ben Johnson

    Ben Johnson June 3rd, 2009 @ 04:00 AM

    • State changed from “new” to “open”

    You make some good points. What you are referring to is this line basically:

    options[:last_page] ||= options[:search_obj].page_count

    I know it's out of context.

    But you are proposing to use the collection.size if we are on page 1 and the size is less than the per_page, if it is provided?

  • Georg Ledermann

    Georg Ledermann June 3rd, 2009 @ 07:58 AM

    Yes. It think here is the right place for this change:

    module Searchlogic
      module Search
        module Pagination
          def page_count
            @page_count ||= (per_page.blank? || per_page <= 0) ? 1 : (count / per_page.to_f).ceil

    Replace it with something like this:

          def page_count
            @page_count ||= (per_page.blank? || per_page <= 0 || per_page > collection.length) ? 1 : (count / per_page.to_f).ceil

    (this code does not work, I don't know how to get the collection object from within this module).

  • Georg Ledermann

    Georg Ledermann June 3rd, 2009 @ 08:01 AM

    oops, the condition "&& page == 1" has to be added...

  • Ben Johnson

    Ben Johnson June 5th, 2009 @ 03:05 PM

    Hi George,

    The other day I had a great idea for searchlogic v2. So I figured I would give it a shot and it turned out really nice. I'd like to show it to you because I think it will solve a lot of problems including this one. You've been pretty helpful since I released this library so I'd like to get your opinion. Heres the best part:

    v1: ~3700 lines of code
    v2: ~180 lines of code

    So far I am able to do just about everything I can do in v1. I would say 95% done.

    Anyways, let me know if you'd like to take a look. Just give me your github username and I'll add you to the private repo. Thanks.

  • Georg Ledermann

    Georg Ledermann June 5th, 2009 @ 03:36 PM

    Wow, this sounds great!

    I like Searchlogic very much and the ideas on your weblog regarding v2 are very promising, so if I can help, I will have a look at the code in my spare time. Hopefully I can give some input... my Github username is "ledermann".

  • Ben Johnson

    Ben Johnson June 5th, 2009 @ 03:39 PM

    Good deal, check it out when you get a chance. I added you to the repo.

  • Ben Johnson

    Ben Johnson June 20th, 2009 @ 05:00 AM

    • State changed from “open” to “resolved”

    Alright, I think this is resolved because of v2. Thanks!

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.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Provides common named scopes and object based searching.

People watching this ticket