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:
db.event.aggregate(
[
{ "$match": {"orchData.accountNumber":12345} },
{ "$group": { "_id": "count", "total": { "$sum": 1 } } }
]
)
The group/sort for larger accounts (20mil records) takes ~30 seconds. Is there any way to optimize this?
We have an index on accountNumber, we’ve tried sorting by account and time prior to matching.