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.

            backlog-query-optimization Backlog - Query Optimization
            david.storch@mongodb.com David Storch
            7 Vote for this issue
            36 Start watching this issue