New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pagination without having to SELECT COUNT(*). #681
Conversation
This build is failing because ActiveRecord edge is Rails 5 which requires Ruby > 2.2.1 |
I support pushing this along... we have to do this on basically all of my projects when they reach a certain size. It would be great to have a built-in solution to this problem. Perhaps a |
👍 |
@amatsuda anything holding this up? thoughts? |
@bryanrite I came up with a fix for this: https://github.com/amatsuda/kaminari/issues/545#issuecomment-112695739 |
@bryanrite As you mentioned above, if the count is exactly divisible by the per page limit, it'll show an erroneous next link on the last page. We can't accept this as I'm pretty sure someone will complain later on. I personally don't like it either. We know there's a problem, but we don't accept fragile solutions. |
@dogweather Thanks, thats a great solution, but you still need to know the page count. The situation I'm building this for is when data is huge and changing too constantly for too many conditionals. Here is a contrived example: Think of a user's activity feed on a site that has millions of users. I could keep a cache of the number of activities, which is fine for the main page, but then I want to show them only certain types of activities on another page. Now I have to cache that count as well, etc. etc. The actual count of the activities isn't important. Allowing them to just "See More" is basically what I'm getting at, and Kaminari doesn't allow you to do that without a @yuki24 I know its an issue, but if you're using this method, your dataset is obviously quite big and reaching the end of such a huge dataset and being exactly divisible is likely uncommon. For something like an ajax infinitely scrolling list its a non-issue and/or easily handled in the UI (I show a "You've Reached the End" type of entry). If you think that is serious enough not to include, i'll just gemify this. |
Thanks for the feed back 😄 |
@bryanrite go ahead and gemify it. |
39d4bc7
to
609b118
Compare
609b118
to
c4803cd
Compare
What if the |
@bryanrite I like the idea of using |
@bricker Supporting multiple ORMs might be hard, but let's not worry about it for now and focus only on AR. |
@bryanrite Think you can incorporate the |
@contentfree Yup, I'm full up until the end of this month, but I'll take a look over the holidays and hopefully get this pushed. |
Hi @bryanrite I added it:
And now it works as expected. |
@bryanrite Were you able to update the PR with the |
Bah, I completely forgot about this. Let me take a look again. |
@bryanrite Please do! I really like the |
@bryanrite : ping! Could you update the PR and incorporate @shir's fix? |
@bryanrite : I am very interested in this PR, can we review and merge? |
Pagination without count is now available in the Gemfile: gem 'kaminari', github: 'amatsuda/kaminari', ref: 'pagination-without-count' In a controller: @users = User.page(params[:page]).without_count In a view: <%= paginate_without_count @users %> |
@yuki24 |
@PikachuEXE Well, It's a lot different from
|
It's going to be awesome to have this feature! I wonder if the naming could be better. Saying "without count" is an implementation detail, even if it is an important one is some cases, but it isn't always important and it is one lost on novice developers. Maybe it would be clearer and lead developers to using the better option by default in more cases if we instead had |
@cgriego Totally! Actually, you don't have to use |
based on @bryanrite's idea on #681 and @yuki24's experimental implementation
based on @bryanrite's idea on #681 and @yuki24's experimental implementation
Is it possible to use the regular paginate widget and pass in count? I have a counter cache on the model so the query does not need to be executed on every page load. Is there an option that works to pass in count, or is it only this? |
@schneems I know this is not the best interface, but you could use the paginate @issues, total_pages: (total_count / per_page).ceil It will skip calling the |
This PR is unfinished (no specs), I just wanted to test the waters as to whether you would want it.
This is a tweak I've had to use on several projects so that Kaminari doesn't do a COUNT due to slow Postgres COUNT issues, as also described: #240, #545. I cannot use indexes due to where conditions, or any other fancy ways to get the count as the tables are just too big, so this PR adds the ability to use
paginate_without_count
to paginate usingnext
andprevious
links that never call COUNT.Instead of COUNTing the dataset, we assume that if the current paged set is equal to the limit value, there is another page. We only expose
next
andprevious
links, since we have no way of knowing the number of pages. We override the Pagination class simply so we can change the partial class. Making the partial class configurable internally would alleviate that override if that is cleaner.The only issue is if your count is exactly divisible by the per page limit, we'll show an erroneous next link on the last page... but to me this is a small price to pay for being able to page without timing out due to PG's sequential COUNT scan.
Thoughts?