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

leading $facet with each facet beginning with $match should add $match before $facet

    XMLWordPrintableJSON

Details

    • Query Optimization

    Description

      Imagine you have an aggregation pipeline that looks something like:

      db.collection.aggregate([
        {$facet: {
          xs: [
            {$match: {x: "SOMETHING"}},
            {$group: {...}}
          ],
          ys: [
            {$match: {y: "SOMETHING ELSE"}},
            {$unwind: "$y"},
            ...
          ]
        }}
      ])
      

      Such a pipeline would not provide any predicates for the query system to use to begin the pipeline. This pipeline might benefit from being optimized to something more like:

      db.collection.aggregate([
        {$match: {$or: [{x: "SOMETHING"}, {y: "SOMETHING ELSE"}]}},
        {$facet: {
          xs: [
            {$match: {x: "SOMETHING"}},
            {$group: {...}}
          ],
          ys: [
            {$match: {y: "SOMETHING ELSE"}},
            {$unwind: "$y"},
            ...
          ]
        }}
      ])
      

      Original Description

      $facet is not smart enough to use an index. if the only stage in a pipeline is $facet, the planning system will simply default to a COLLSCAN.

      Attachments

        Issue Links

          Activity

            People

              backlog-query-optimization Backlog - Query Optimization
              vick.mena Vick Mena (Inactive)
              Votes:
              9 Vote for this issue
              Watchers:
              28 Start watching this issue

              Dates

                Created:
                Updated: