[SERVER-55417] Conditionally reorder $sort and $lookup depending on whether the query planner allows for an index-provided sort Created: 22/Mar/21 Updated: 06/Dec/22 |
|
| Status: | Backlog |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Yuhong Zhang | Assignee: | Backlog - Query Optimization |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | optimization | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||
| Assigned Teams: |
Query Optimization
|
||||||||||||||||
| Participants: | |||||||||||||||||
| Description |
|
With the pipeline of [$lookup, $unwind, $sort, $limit], the current implementation will not push $sort before $lookup since $unwind will block $limit from being pushed before $lookup and then combined with $sort. Although letting $sort take advantages of the indexes benefits more than absorbing $limit into $sort to run a top-k sort, there is no way in the current system to learn if there are indexes on the sorting fields. Instead of depending on the presence of $unwind, the optimization should reorder $lookup and $sort depending on if an index-provided sort is allowed. The other condition of not sorting on fields introduced or modified by $lookup and $unwind remains the same. |