DISTINCT_SCAN with $top and $bottom is only used when grouping field is included as sort prefix

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: 8.0.0
    • Component/s: None
    • Query Optimization
    • None
    • 3
    • TBD
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Based on SERVER-84347 which was supposed to make DISTINCT_SCAN optimization available to $top and $bottom accumulators, it seems to rely on this form of group:

      {$group: {
          _id: "$a", 
          accum: {$top: {
                output: "$b", 
                sortBy: {a: 1, b: 1}}}
      }}
      

      But no one would know to write that - took me a few minutes to figure it out:

      {$group: {
          _id: "$a", 
          accum: {$top: {
                output: "$b", 
                sortBy: {b: 1}}}
      }}
      

      is the normal and expected syntax that we should expect to optimize.  Of course it would rely on index on {a: 1, b: 1} which is its own little side issue ... 

              Assignee:
              Sopho Kevlishvili (Inactive)
              Reporter:
              Asya Kamsky
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated: