Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kaminari without COUNT

Can Kaminari work without hitting the DB with a COUNT(*) query?

My app's database is huge and counting the items takes much much longer than getting the items itself, leading to performance issues.

Suggestions for other pagination solutions with large datasets are also welcome.

like image 418
Jonathan Lin Avatar asked Oct 06 '22 01:10

Jonathan Lin


1 Answers

Paginating Without Issuing SELECT COUNT Query

Generally the paginator needs to know the total number of records to display the links, but sometimes we don't need the total number of records and just need the "previous page" and "next page" links. For such use case, Kaminari provides without_count mode that creates a paginatable collection without counting the number of all records. This may be helpful when you're dealing with a very large dataset because counting on a big table tends to become slow on RDBMS.

Just add .without_count to your paginated object:

User.page(3).without_count

In your view file, you can only use simple helpers like the following instead of the full-featured paginate helper:

<%= link_to_prev_page @users, 'Previous Page' %>
<%= link_to_next_page @users, 'Next Page' %>

Source: github.com/kaminari

like image 155
Kattia Avatar answered Oct 10 '22 01:10

Kattia