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

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

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None
    • None
    • Query Optimization

    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

      Attachments

        Activity

          People

            backlog-query-optimization Backlog - Query Optimization
            c.alonso@mongodb.com Carlos Alonso Pérez
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

              Created:
              Updated: