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 7488189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:46:54+00:00 2026-05-29T14:46:54+00:00

The code: Channel.all.paginate(:page => 3, :per_page => 25) Say I have a table with

  • 0

The code:

Channel.all.paginate(:page => 3, :per_page => 25)

Say I have a table with 400,000 records, does the above code select all 400,000 records then get the current 25 I need or does it only query for the 25 I need.

If it queries all 400,000 records is there a better optimized way to paginate large datasets using rails?

  • 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-29T14:46:56+00:00Added an answer on May 29, 2026 at 2:46 pm

    Mongo Mapper (which I assume your using because of the syntax of your query) is implementing this using the limit and skip expressions.

    Basically it would run a query where it skips over a number of Channels and then retrieves the amount specified by the limit (the number you are getting per page).

    For example: If you were on page 3 and have 25 per page, the query that mongo mapper runs looks like this:

    db.channels.find().skip((page - 1) * per_page).limit(per_page)
    

    Which translates to:

    db.channels.find().skip(2 * 25).limit(25)
    

    To return results, mongo has to skip over (page - 1) * per_page number of results which can be costly if the page number is high. Lets say that expression evaluates to 1000, then it would have to run the query, skip over 1000 documents and get the next 25 documents (the limit). MongoDB would essentially be doing a table scan over those documents.

    To avoid that you can do range based paging which provides better use of indexes but does not allow you to easily jump to a specific page.

    If the Channel model has a date field for example, range based paging would, instead of using skip, use $gte and limit. You would take the date of last document on x page and get the next page’s results by querying for documents with date $gte of previous page’s final document. If you do that you could get dupes though, so it might make sense to use a different criteria.

    In practice, don’t worry about it unless you have a really high number of pages.

    Cheers and good luck!

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

Sidebar

Related Questions

I have the following problem I have 3 table (all of the are used
I have the following code foreach (var rssItem in rss.Channel.Items) { // ... }
I have the following code: page = Net::HTTP.get_response(URI.parse('http://www.enhancetv.com.au/tvguide/rss/melbournerss.php')).body rescue nil show_info = Hash.from_xml(page).to_json puts
gcc 4.4.4 c89 I have the following code in my channel.h file typedef struct
gcc 4.4.2 I have the following code: char channels[] = NumberOfChannel = [2]; sscanf(channels,
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code I have: cell_val = CStr(Nz(fld.value, )) Dim iter As Long For iter =
(code examples are python) Lets assume we have a list of percentages that add
I've been fighting this all day. Inside my styles.xml file I have color information
I have a pretty simple client-server ASP.NET app; communication is via WCF service. All

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.