Skip to content

Conversation

@veewee
Copy link
Contributor

@veewee veewee commented Aug 1, 2025

Q A
Type feature
BC Break yes ❗
Fixed issues

Summary

Pagerfanta always executes double queries, just to count the amount of records:

  • When the list query is rather heavy, this might slow down the application by a lot.
  • In our code, we never do a second pagination within the same request, making this duplicate query execution obsolete.
  • Counting is not always valid (left joins with aggregated group-by's can become quite hard)

Postgres already provides us with window partitions, making it possible to select the total count directly from the main query:
https://www.postgresql.org/docs/current/tutorial-window.html

This allows us to enhance the query like this:

SELECT 
    users.*,
   COUNT(1) OVER () AS total_results
FROM users
LIMIT 3 OFFSET 3

(which will work for any query: joined, aggregated, .... given it operates on the full query window)

Example implementation:

use Phpro\DbalTools\Pager\MappingPager;
use Phpro\DbalTools\Pager\Pager;
use Phpro\DbalTools\Pager\Pagination;
use Phpro\DbalTools\Pager\WindowCountPager;

$query = $connection->createQueryBuilder()->select('*')->from('users');
$userMapper = new UsersMapper()->convertFromDb(...);

$usersPager = new MappingPager(
    WindowCountPager::create(
        new Pagination(page: $page limit: $limit),
        $query,
    ),
    $userMapper,
);

This implementation makes it possible to run the same pagination queries on both dbal query builder and our custom composite queries.

@veewee veewee marked this pull request as draft August 1, 2025 13:08
@veewee veewee changed the title Use custom window-count based pager over pagerfanta [WIP] Use custom window-count based pager over pagerfanta Aug 1, 2025
@veewee veewee force-pushed the custom-pager branch 3 times, most recently from 2f51e66 to 63e42e3 Compare August 1, 2025 13:54
@veewee veewee changed the title [WIP] Use custom window-count based pager over pagerfanta Use custom window-count based pager over pagerfanta Sep 1, 2025
@veewee veewee marked this pull request as ready for review September 1, 2025 08:35
@veewee veewee merged commit fc3db4d into phpro:main Sep 2, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant