Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
How to avoid SELECT COUNT ? #447
Hey guys, we the following code for pagination, which is pretty much trivial.
User.paginate(page: page, per_page: limit).order(id: :desc)
The problem is that it executes a second query to count the number of records in the table, and that's a very expensive operation, especially in a very big table (which is the case), is there any option to avoid doing that extra count? It's not necessary for us to have it.
Curious about this as well!
Postgres counting is slow:
Would be great if will_paginate supported a mode where instead of using LIMIT/OFFSET it just let did
referenced this issue
Jan 19, 2016
will_paginate needs to perform the COUNT to know how many pages in total there are so it can generate pagination links. That's the sole purpose why the library was created.
Now, I understand that some COUNTs are slow. Yours, for instance, could maybe be replaced with a simple
Kaminari has a mode without_count for those that just need simple "next page", "previous page" buttons: https://stackoverflow.com/questions/13284860/kaminari-without-count (Note: only for ActiveRecord, no implementation for Mongoid unfortunately)