-
Type: New Feature
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Query Optimization
In a pipeline like this:
{$sort: {x: 1}}
... // order-preserving stages
{$sort: {x: 1, y: 1}}
we could optimize the second $sort, to take advantage of the fact that the input is already sorted on {x: 1}. Any two documents with the same {x: 1} key are already next to each other, so whenever we see 'x' increase, we can start returning results before asking for more input. Depending on how big these runs of equal 'x' are, this could save a lot of space, and maybe avoid spilling.
Also, we only need to compare values of 'y', since we're only breaking ties within each run of equal 'x' values. Maybe that would be useful if 'x' is large, or the overall document is small.
This could be useful for cases like:
- {$densify ...} {$sort: {partitionField: 1, someOtherField: 1}}, because the output of $densify will be sorted by
{partitionField: 1}
.
- {$unpackBucket ...} {$sort: {meta: 1, temperature: 1}}, because we could push down the sort on
{meta: 1}
.
- {$unwind ...} {$sort ...} similar to $unpackBucket.
- is related to
-
SERVER-66707 Add ability to nest $group operations
- Backlog
-
SERVER-65159 Consider {a: 1, b: 1} index to satisfy sort on {a: 1, b: -1}
- Backlog
- related to
-
SERVER-69027 [CQF] Support for Recursive Index Navigation
- Closed