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

Optimize a $sort after a $unionWith

    XMLWordPrintableJSON

Details

    • Query Optimization

    Description

      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.

      Attachments

        Activity

          People

            backlog-query-optimization Backlog - Query Optimization
            charlie.swanson@mongodb.com Charlie Swanson
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated: