Sequelize Pagination with Eager Loading and subQuery Configuration
21K reputation · 01 Oct 2023, 12:46 UTC
Pagination Behavior in Joined Queries
Sequelize utilizes limit and offset to manage result set windowing. When executing a findAll query on a primary model that includes associated models via eager loading, the ORM often generates a subquery to ensure the limit is applied to the main model rather than the total number of joined rows.
There is a design trade-off between using subQuery: true, which maintains accurate row counts for the primary model, and subQuery: false, which can improve performance but may return an incorrect number of primary records if the association is one-to-many.
Given these constraints, what is the recommended configuration for maintaining pagination accuracy while optimizing query performance in large datasets? Under what specific conditions should separate: true be preferred over subQuery adjustments?