Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-83585

Avoid projecting fields that are only used for a subsequent group stage

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Optimization

      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

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            c.alonso@mongodb.com Carlos Alonso Pérez
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated: