xing / rails_cursor_pagination

Add cursor pagination to your ActiveRecord backed application
MIT License
112 stars 17 forks source link

It still fetches the count of all records even if I omit the with_total #132

Open amrelhewy09 opened 1 year ago

amrelhewy09 commented 1 year ago

The pagination works but does the extra select COUNT(*) query for no reason.

chat_applications = RailsCursorPagination::Paginator.new(ChatApplication.all, first: 2, after: after).fetch

Why does this query do 2 extra queries?

chat_app                 |    (0.4ms)  SELECT COUNT(*) FROM `chat_applications` WHERE (`chat_applications`.`id` > 2)
chat_app                 |   ↳ app/controllers/api/v1/chat_applications_controller.rb:20:in `index'
chat_app                 |    (0.2ms)  SELECT COUNT(*) FROM `chat_applications`
tomhughes commented 1 year ago

This is because, depending on the direction, either previous_page? or next_page? will wind up querying the total even though it has been disable in the results.