Skip to content

Option to add row count lazily#410

Open
MieskeB wants to merge 4 commits into
omines:masterfrom
MieskeB:master
Open

Option to add row count lazily#410
MieskeB wants to merge 4 commits into
omines:masterfrom
MieskeB:master

Conversation

@MieskeB
Copy link
Copy Markdown

@MieskeB MieskeB commented Apr 10, 2026

Hi everyone! For our project, we have a huge table displayed using datatables-bundle. Because of the forced 'count' queries, the request becomes extremely slow. Therefore, I have implemented the option to lazily count the amount of rows displayed.

Current flow in ORMAdapter.php:

  1. Build query
  2. Count ALL results
  3. Apply filters
  4. Count filtered results
  5. Fetch 50 rows
  6. Return everything

Mostly step 2, but also step 4 make the request much longer when big tables are being used. Therefore, I added an option to use the following flow (default remains the top flow).

  1. Build query
  2. Fetch 50 rows
  3. Return rows immediately
  4. Count results later in a separate request

To the ORMAdapter, the following properties can be added:

Property Type Default Description
lazy_total_count bool false adds lazy loading to the total count
lazy_filtered_count bool false adds lazy loading to the filtered count

This is controller code is used in my test project:

    #[Route('/', name: 'app_index')]
    public function index(Request $request, DataTableFactory $factory): Response
    {
        $table = $factory->create()
            ->add('email', TextColumn::class, ['field' => 'u.email'])
            ->add('iban', TextColumn::class, ['field' => 'u.iban'])
            ->add('name', TextColumn::class, ['field' => 'u.name'])
            ->createAdapter(ORMAdapter::class, [
                'entity' => User::class,
                'query' => function (QueryBuilder $qb) {
                    $qb->select('u')->from(User::class, 'u');
                },
                'lazy_total_count' => true,        // <-- This line adds lazy counting to the total count
                'lazy_filtered_count' => true,     // <-- This line adds lazy counting to the filtered count
            ]);

        $table->handleRequest($request);

        if ($table->isCallback()) {
            return $table->getResponse();
        }

        return $this->render('index/index.html.twig', ['datatable' => $table]);
    }

Tests and documentation have also been included

@MieskeB MieskeB changed the title Option to add count lazily (do it in a later request) Option to add row count lazily Apr 10, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.21%. Comparing base (f29e574) to head (f07c810).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #410   +/-   ##
=======================================
  Coverage   95.21%   95.21%           
=======================================
  Files          40       40           
  Lines        1087     1087           
=======================================
  Hits         1035     1035           
  Misses         52       52           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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