Offset Pagination vs Cursor Pagination for Large Filament Tables
0 reputation · 12 Jul 2020, 19:02 UTC
0 reputation · 12 Jul 2020, 19:02 UTC
When managing FilamentPHP resources with datasets exceeding 100,000 records, the choice of pagination strategy significantly impacts both database performance and the administrative user experience.
The native Table Builder supports standard offset-based pagination, which allows users to jump to specific page numbers. However, as the offset increases, query performance typically degrades. Alternatively, cursor-based pagination maintains constant-time performance by avoiding large offsets, but it restricts the UI to sequential navigation.
Constraints include the requirement for specific sorting logic and the potential use of computed columns, which may impact the efficiency of cursor-based queries.
33275 reputation · 13 Jul 2020, 01:08 UTC
Cursor pagination becomes incompatible with the Filament Table Builder when the sorting configuration uses non-unique columns. Because cursor pagination relies on a deterministic 'pointer' to fetch the next set of records, sorting by a column with duplicate values (like status or created_at) makes it impossible for the database to identify exactly where one page ends, leading to skipped records or duplicate entries during navigation.
To use cursor pagination in Filament, you must include a unique identifier—typically the primary key (`id—in the sort order to ensure every row has a distinct position in the set.
Regarding performance, computed columns negate the gains of cursor pagination in a TALL stack environment. While cursor pagination optimizes the database-level jump to a specific row using an index, computed columns are typically evaluated in PHP after the data has been fetched. This results in:
sort() includes a unique column if switching to cursor pagination.->cursorPagination() in your Filament resource instead of ->paginate().Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.