[Draft] Use $sum instead of $push to optimise Mongo group queries#311
Draft
suddendust wants to merge 2 commits into
Draft
[Draft] Use $sum instead of $push to optimise Mongo group queries#311suddendust wants to merge 2 commits into
suddendust wants to merge 2 commits into
Conversation
lrathod
approved these changes
Jun 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optimize Mongo
COUNTaggregation:$suminstead of$push+$sizeSummary
The document-store library compiled
COUNT(<expr>)for MongoDB into a$pushaccumulatorthat collected every value into an in-memory array, then took its length with
$sizein afollow-up
$project:This materializes one array element per matching document just to count them. This change makes
COUNTemit a$sumaccumulator instead — O(1) memory per group, no array, no spill — whilepreserving the existing semantics:
Performance Evidence
Both plans below are the same query, same filter, same index (
tenantId_isLearnt_index),on
default_db.application_asset_entitiesfor a tenant with ~459K matching documents(
explain("executionStats"), MongoDB 8.0.20). The only difference is theCOUNTcompilation.The "after" plan was captured with the
$sum: 1(COUNT(*)) form.$push+$size)$sum)$groupaccumulatoraddToArrayCapped(entityId, 104857600)count()$groupstage time (est.)Result: ~48% faster (13.75s -> 7.13s) on this dataset. The dominant saving is eliminating the
per-group array build (
$groupstage time drops from ~13.5s to ~80ms over its input); the$condpresence-guard form forCOUNT(<field>)removes the same array work, so it gets the sameclass of improvement.
Out of scope (follow-up)
Note on full index coverage: the most efficient form (
$sum: 1, reading no document fields) isproduced for
COUNT(<constant>). ForCOUNT(<field>)the presence guard reads the field, so itcannot be fully index-covered. The production asset-count query currently uses
COUNT(fieldName);if the intent is "count documents" (entityId is always present), the caller should issue
COUNT(*)/COUNT(1)to enable a fully index-covered, sub-second plan together with thecovering index.