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

Push $sort before $project and $addFields

    • Query Optimization

      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:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            david.storch@mongodb.com David Storch
            Votes:
            9 Vote for this issue
            Watchers:
            39 Start watching this issue

              Created:
              Updated: