We provide a UI for certain mongo records in a multitenant application. Each list of records is scoped to a user’s account number and whatever filter they enter into the UI. The initial page load is all records (for the account) sorted by time. The first 20 records come back fast, but we also count the total records returned using:
Sorting by time makes me think that you might be able to use the Computed Pattern on older data. Also look at
For example, in accounting it is pointless to recompute every time account totals for months or years that are closed because those totals won’t change anymore. In January 2025 you would not be adding transactions for December 2024 and for the whole 2024 year. So you compute and store the totals at month’s and year’s closing. You probably already do those computations to produce an account statement, the extra steps to store them have less impact that computing anew every time you have to go back.
The performance issue is that most likely that the old transactions documents are not part of the current working set anymore so you have to read them for storage.
Our app is a high volume integration platform, and this particular count is all the events processed in the last 2 months (rolling). Saving a count periodically would give a good approximation, but it could not be recalculated for every new event nor could we capture only net new events as they come across, as far as I can tell. New data comes in and old data drops off the TTL too quickly.