For example, $topN: {n: 1, ...} would have the exactly same semantics as $top: {...}. The single element accumulators would not need a mechanism to maintain multiple values. So, it would slightly perform better to process $top than $topN with n=1.
On top of this, thanks to SERVER-87589/SERVER-84347, $sort + $group with $first/$last can be optimized into $group with $top/$bottom and $group with $top/$bottom can leverage DISTINCT_SCAN when there's a proper index but $group with $firstN, $lastN, $topN, and/or $bottomN can't.
This ticket will extend the applicability of SERVER-87589/SERVER-84347 optimizations.
I think that this would be better to be done in the parsing phase to avoid rewriting overhead than in the optimization phase since rewriting may also incur overhead and therefore it may affect very short-running pipeline performance.
- related to
-
SERVER-92146 Rewrite to group topN/bottomN sorts generates incorrect projection
- Closed
-
SERVER-84347 Use DISTINCT_SCAN with $top/$bottom accumulators
- Closed
-
SERVER-87589 Rewrite adjacent $sort + $group with $first/$last to $group with $top/$bottom for timeseries
- Closed