[SERVER-48121] Optimize a $group after a $unionWith Created: 12/May/20  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Aggregation Framework, Performance
Affects Version/s: 4.4.0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Charlie Swanson Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: optimization
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-45532 Implement $group pushdown optimizatio... Closed
Assigned Teams:
Query Optimization
Participants:

 Description   

Consider the following pipeline:

db.example.aggregate([
  {$unionWith: "unionColl"},
  {$match: {example: {$eq: "predicate"}}},
  {$group: {_id: "$groupKey", total: {$sum: "$price"}}}
])

Unfortunately we are missing an optimization to duplicate the $group for each branch of the union and keep only a merge of pre-grouped data after the $unionWith. We should implement this optimization.

This could help in cases where part of either pipeline is happening on another shard in the cluster, or when an index could be used to speed up the group like in SERVER-9507.


Generated at Thu Feb 08 05:16:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.