Contao Model::findBy() count query overhead when paginating large datasets
0 reputation · 10 Feb 2026, 12:03 UTC
Pagination count query behavior
Contao's Model::findBy() method accepts limit and offset options to bound result sets. When a limit is provided, the framework automatically executes a separate COUNT(*) query using the same WHERE conditions to determine the total row count for pagination metadata.
Performance constraint
On tables with millions of rows and complex WHERE clauses, this count query adds measurable latency. The OFFSET-based pagination also degrades as the offset grows because the database must scan and discard preceding rows. For infinite-scroll or "load more" UIs where only the next page's existence matters, the full count is unnecessary.
Missing configuration
No native API exists to suppress the count query, provide an estimated count, or switch to keyset/cursor-based pagination (WHERE id > ? ORDER BY id LIMIT N) which avoids OFFSET scan costs. Developers must either override Model::findBy() or use raw Database::prepare() calls, losing automatic hydration and relation loading.
Does Contao 4.13 LTS or 5.x expose a configuration flag or event listener to disable the automatic count query when only a limited result set is needed? Can the Pagination class be extended to use a lightweight existence check instead of a full count? Is there a supported pattern for implementing keyset pagination while retaining Model hydration?