Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 371507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:07:15+00:00 2026-05-12T14:07:15+00:00

I have a current implementation of will_paginate that uses the paginate_by_sql method to build

  • 0

I have a current implementation of will_paginate that uses the paginate_by_sql method to build the collection to be paginated. We have a custom query for total_entries that’s very complicated and puts a large load on our DB. Therefore we would like to cut total_entries from the pagination altogether.

In other words, instead of the typical pagination display of ‘previous 1 [2] 3 4 5 next’, we would simply like a ‘next – previous’ button only. But we need to know a few things.

  1. Do we display the previous link? This would only occur of course if records existing prior to the ones displayed in the current selection
  2. Do we display the next link? This would not be displayed if the last record in the collection is being displayed

From the docs

A query for counting rows will
automatically be generated if you
don’t supply :total_entries. If you
experience problems with this
generated SQL, you might want to
perform the count manually in your
application.

So ultimately the ideal situation is the following.

  • Remove the total_entries count because it’s causing too much load on the database
  • Display 50 records at a time with semi-pagination using only next/previous buttons to navigate and not needing to display all page numbers available
  • Only display the next button and previous button accordingly

Has anyone worked with a similar issue or have thoughts on a resolution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:07:16+00:00Added an answer on May 12, 2026 at 2:07 pm

    There are many occasions where will_paginate does a really awful job of calculating the number of entries, especially if there are joins involved that confuse the count SQL generator.

    If all you need is a simple prev/next method, then all you need to do is attempt to retrieve N+1 entries from the database, and if you only get N or less than you’re on the last page.

    For example:

    per_page = 10
    page = 2
    
    @entries = Thing.with_some_scope.find(:all, :limit => per_page + 1, :offset => (page - 1) * per_page)
    
    @next_page = @entries.slice!(per_page, 1)
    @prev_page = page > 1
    

    You can easily encapsulate this in some module that can be included in the various models that require it, or make a controller extension.

    I’ve found that this works significantly better than the default will_paginate method.

    The only performance issue is a limitation of MySQL that may be a problem depending on the size of your tables.

    For whatever reason, the amount of time it takes to perform a query with a small LIMIT in MySQL is proportional to the OFFSET. In effect, the database engine reads through all rows leading up to the particular offset value, then returns the next LIMIT number rows, not skipping ahead as you’d expect.

    For large data-sets, where you’re having OFFSET values in the 100,000 plus range, you may find performance degrades significantly. How this will manifest is that loading page 1 is very fast, page 1000 is somewhat slow, but page 2000 is extremely slow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Paramiko's SFTPClient apparently does not have an exists method. This is my current implementation:
I have been reviewing the setup of a current Amazon Web Store implementation to
I have three current thoughts on how to do this: re-implement AuthenticationService, which uses
Background I have a custom authentication back end for our django applications that refers
I have an old memcached client implementation that is based of the danga client,
I have a running program that uses glVertexPointer, glNormalPointer etc to draw a large
I have build a simple application that opens a ServerSocket, and on connection, it
I have current a developed app which I am going to submit in just
We have this current database which we need to replace some tables by anther
I have the current bundle reference working locally: Bundles.Reference(/Content/global.css); On the development site, this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.