You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix pagination_total index option to prevent SELECT count(*) queries
The pagination_total option is supposed to prevent expensive SELECT
count(*) queries from being performed when viewing a resource's index
page.
However, while this option was hiding the display of the total number of
pages, it did not actually prevent the query from being performed.
There were two reasons:
In `#build_pagination`, the line:
`text_node paginate collection, options`
would trigger Kaminari to call `#total_pages` on the collection, which would
trigger the query:
```
def paginate(scope, options = {}, &block)
options[:total_pages] ||= options[:num_pages] || scope.total_pages
```
By passing in the `:total_pages` option, we short circuit this assignment
and avoid the query.
The downside is we do so by hardcoding in an essentially random total pages
count of 100. I chose the number 100, because I wanted to ensure ellipses
would appear in Kaminari's "Page 1, 2, 3, ..., Next, Last" links, no matter
how many links are configured to be displayed. However, if the resource only
had 2 pages worth of items, then clicking the "3" would result in an empty
page. That said, without querying the resource, there's no way to know how
many pages there should be.
That fix was not sufficient. In `#page_entries_info`, the call
`collection.num_pages` also triggered a SELECT count(*). I re-arranged
the conditionals to avoid that call if `@display_total` is false, while
trying to keep the behavior the same.
0 commit comments