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

Rewrite many $topNs/$bottomNs that have the same sort pattern so that it only creates one sort key

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.1.0-rc0, 8.0.0-rc3
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • Query Integration
    • Fully Compatible
    • v8.0
    • QI 2024-04-01, QI 2024-04-15
    • 33

      In SERVER-87589, we limited the number of rewritten $first/$last accumulators only to 2 as overhead of generating the same sort key multiple times for multiple rewritten $top/$bottom could be big if there are too many $first/$last.

      Suggested idea:

      {$sort: {sortField: 1}},  
      {$group: {_id: ..., firstX: {$first: "$x"}, firstY: {$first: "$y"},  
                                    lastX: {$last: "$x"}, lastY: {$last: "$y"}}}  
      

      --> becomes -->

      {$group: {_id: ...,
        tops: {$top: {sortBy: {sortField:1}}, outputs: {x: "$x", y: "$y"}},  
        bottoms: {$bottom: {sortyBy: {sortField:1}}, outputs: {x: "$x", y:"$y"}}},
      {$project: {
        firstX: "$tops.x", firstY: "$tops.y", lastX: "$bottoms.x", lastY: "$bottoms.y"}}  
      

      Update #1:
      anna.wawrzyniak@mongodb.com suggested to implement a generalized optimization like this where, if there are several topNs/bottomNs on the same patterns, we use a single sorter instead of having a different sorter for each accumulator.

      Update #2:
      As part of this improvement, we should be able to increase internalQueryMaxFirstLastRewrites's default value to a much higher one.

            Assignee:
            yoonsoo.kim@mongodb.com Yoon Soo Kim
            Reporter:
            yoonsoo.kim@mongodb.com Yoon Soo Kim
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: