-
Type: Improvement
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
-
Query Integration
-
Fully Compatible
-
QI 2024-03-18, QI 2024-04-01
-
158
Details can be found at SERVER-85213.
This ticket includes any necessary performance investigation and adding additional perf workloads.
Rough idea:
- In DocumentSourceInternalUnpackBucket::doOptimizeAt(), if it can find $sort followed by $group, then try to optimize
- If so, then rewrite the $first and/or $last into $top and/or $bottom with the same sort key(s) as the $sort stage. We can use GroupProcessorBase's getMutableAccumulationStatements() to manipulate AccumulationStatement
- After rewriting, remove the previous $sort stage
This optimization should not be applied when $sort has a limit.
Update:
- To limit the scope of this ticket and perf impact further, we will apply this optimization only to cases where $_internalUnpackBucket followed by $sort followed by $group.
- This optimization isn't applied to lastpoint optimizable queries to not regress perf of them.
- Limit the number of $first and $last only to 2 as a safe limit 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.
- is related to
-
SERVER-90017 Parse firstN/lastN/minN/maxN/topN/bottomN with n = 1 into a corresponding first/last/min/max/top/bottom
- Closed
- related to
-
SERVER-88087 Rewrite many $topNs/$bottomNs that have the same sort pattern so that it only creates one sort key
- Closed
- split from
-
SERVER-85213 Rewrite $sort+$group with $first/$last to use $top/$bottom
- Backlog