Choose the right tool
At the most basic level, pagination just means retrieving a (potentially big) collection of items in small sequential chunks (pages)... but there's more than one way to crack an egg.
Pagy offers four different techniques, each implementing different types of paginators. Choose the right one to ensure the best performance and workflow.
Pagination Types
The most common pagination technique is counting the collection items (COUNT DB query), using the count to calculate where the specific pages start (i.e., OFFSET) and retrieving only the LIMIT items for that page (OFFSET+LIMIT DB query).
It is straightforward to understand and set up and very versatile for the UI, but it also has a few shortcomings.
- DB Performance
- Counting records might be quite expensive in terms of execution time and DB load. The standard OFFSET pagination causes the DB to count twice per page: one for the total records and another for the records to skip.
- Data Shift
- If records are created or deleted during browsing, the calculated OFFSET may become inconsistent. The user may see previous records again or miss records entirely.
- Notice that querying the precise count for each page DOES NOT fix the data-shift problem.
For better DB performance with OFFSET...
- Use the :countish or :countless paginators to improve the DB performance up-to two times.
- Paginate only MAX records when possible.
The KEYSET pagination technique allows the fastest and lighter DB performance. It does not count the (ordered) collection (which makes it faster), nor calculates any numeric page pointers in advance (which avoids the data-shift during browsing). It just uses the values in the last record of the page to retrieve the next page.
- It knows only the current page and the pointer to the next page.
- Page pointers are encoded strings and the count is not known.
- It supports only APIs and infinite scrolling.
For UI support with KEYSET pagination...
Use the :keynav_js paginator.
This hybrid technique filters by a specific time period (Year, Month, Day, etc.) and applies the offset paginator within that period.
Pagy supports ElasticsearchRails, Meilisearch, Searchkick, and TypesenseRails.
The search paginators get the count, limit and results provided by the search platform. Pagy acts as an interface to these underlying gems, using the