Push $sort before $project and $addFields

XMLWordPrintableJSON

    • Query Optimization
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      In some cases, a $sort aggregation stage can swap with a preceding $project. Consider the following example:

      db.c.createIndex({a: 1});
      db.c.aggregate([{$project: {a: 1, b: 1}}, {$sort: {a: 1}}]);
      

      An explain of this aggregation shows that a COLLSCAN will feed the $project => $sort pipeline. If the agg optimization phase were to swap the $sort with the $project, however, it could push down the $sort and obtain the desired ordering via an index scan. This would remove the sort stage from the query plan entirely, which could result in a substantial performance improvement.

      We would probably want to apply this optimization only if the query planner's sort analysis shows that the sort can be obtained via an appropriate index scan.

              Assignee:
              [DO NOT USE] Backlog - Query Optimization
              Reporter:
              David Storch
              Votes:
              9 Vote for this issue
              Watchers:
              41 Start watching this issue

                Created:
                Updated: