Forked from hammerstonedev/fast-paginate
Fast Paginate provides faster limit/offset pagination macros for Hyperf model builders and
relations. It is most useful for queries with large offsets, but performance depends on the data and
indexes, so benchmark it against the standard paginator for your application.
The component uses an approach similar to a deferred join. It first paginates a query that selects the model's primary key and any selected aliases required for ordering. It then fetches the complete rows for the keys on that page with a second query. Conceptually, the two data queries look like:
selectcontacts.idfrom contacts limit15 offset 150000;
select*from contacts wherecontacts.idin (...);The component executes separate queries rather than putting the limited query inside a where in
subquery. fastPaginate() also runs the normal count query needed for a length-aware paginator;
simpleFastPaginate() does not run that count query.
composer require friendsofhyperf/fast-paginateThe component depends on Hyperf 3.2 packages. No configuration is required: Hyperf discovers the
component's ConfigProvider, which registers the pagination macros when the application boots.
Model builders and relations provide both macros:
User::query()->fastPaginate();
User::query()->simpleFastPaginate();
User::first()->posts()->fastPaginate();
User::first()->posts()->simpleFastPaginate();Their signatures match the corresponding Hyperf model-builder pagination methods:
fastPaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);
simpleFastPaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);$perPage: items per page;nulluses the model's configured per-page value.$columns: columns fetched for the complete rows.$pageName: query-string parameter used to resolve the current page.$page: explicit page number;nullresolves it from the current request.
fastPaginate() returns a length-aware paginator with a total count. simpleFastPaginate() returns
a simple paginator that only determines whether more pages exist. BelongsToMany relations keep
their hydrated pivot data, and HasManyThrough relations are also supported.
When hyperf/scout is installed, the component also registers fastPaginate() on
Hyperf\Scout\Builder:
User::search('Hyperf')->fastPaginate();Its signature is fastPaginate($perPage = null, $pageName = 'page', $page = null). This Scout macro
delegates directly to Scout's normal paginate() method; it does not use the two-query database
optimization. hyperf/scout is not required by this component.
When the optimized query shape is incompatible, the component automatically calls the corresponding
standard paginate() or simplePaginate() method. This happens when:
- the query contains
having,group by, orunionclauses; $perPageis-1;- a selected expression required for ordering contains a
?binding placeholder.
These fallbacks preserve pagination behavior, but they do not provide the fast-pagination optimization.