Optimize a $sort after a $unionWith

XMLWordPrintableJSON

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

      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:
              [DO NOT USE] Backlog - Query Optimization
              Reporter:
              Charlie Swanson
              Votes:
              2 Vote for this issue
              Watchers:
              10 Start watching this issue

                Created:
                Updated: