-
Type: Improvement
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
-
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.
{$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.
- causes
-
SERVER-92146 Rewrite to group topN/bottomN sorts generates incorrect projection
- Closed
- is related to
-
SERVER-87589 Rewrite adjacent $sort + $group with $first/$last to $group with $top/$bottom for timeseries
- Closed
- related to
-
SERVER-88613 Introduce a new syntax to $group to define the common sort key for multiple $top & $bottom accumulators
- Backlog
- split from
-
SERVER-85213 Rewrite $sort+$group with $first/$last to use $top/$bottom
- Backlog