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

Optimize a $sort after a $unionWith

    • Query Optimization

      Consider the following pipeline:

      db.example.aggregate([
        {$unionWith: "unionColl"},
        {$match: {example: {$eq: "predicate"}}},
        {$sort: {y: 1}}
      ])
      

      Our optimization pass can recognize the $match stage can be duplicated and applied to both "example" and "unionColl" collections, which means the predicate could be answered using an index.

      Unfortunately we are missing an optimization to duplicate the $sort for each branch of the union and keep only a merge-sort instead of the union. We should implement this optimization.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            charlie.swanson@mongodb.com Charlie Swanson
            Votes:
            2 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: