[SERVER-83585] Avoid projecting fields that are only used for a subsequent group stage Created: 27/Nov/23  Updated: 07/Dec/23

Status: Backlog
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Carlos Alonso Pérez Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Assigned Teams:
Query Optimization
Participants:

 Description   

Extracting fields into variables just to use them later in a group stage is unnecessary and should be optimized away.

Query optimizer should move as many entries from the project stage as possible into the group stage provided such entries are only used in such group stage. See the following example:

...aggregate([
    { $project: {
        "a": "$path.to.a",
        "b": "$path.to.b",
        ...
    }},
    { $group: {
            _id: {
                "a": "$a",
                "b": "$b",
                ...
           }
     }}
])

results in a group < -- projection_default < -- collscan plan

While the following

...aggregate([ 
    { $group: { 
       _id: { 
           "a": "$path.to.a", 
           "b": "$path.to.b",
           ... 
        } 
    }} 
]) 

results in a simpler group <- collscan plan

And produces same output


Generated at Thu Feb 08 06:52:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.