[SERVER-48120] Optimize a $sort after a $unionWith Created: 12/May/20  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Aggregation Framework, Performance
Affects Version/s: 4.4.0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Charlie Swanson Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 1
Labels: optimization
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-45534 Implement $sort pushdown optimization... Closed
Related
related to SERVER-60800 Allow $search in $lookup/$unionWith Closed
is related to SERVER-55612 Improve handling of $geonear+$or when... Closed
Assigned Teams:
Query Optimization
Participants:

 Description   

Consider the following pipeline:

db.example.aggregate([
  {$unionWith: "unionColl"},
  {$match: {example: {$eq: "predicate"}}},
  {$sort: {y: 1}}
])

Our optimization pass can recognize the $match stage can be duplicated and applied to both "example" and "unionColl" collections, which means the predicate could be answered using an index.

Unfortunately we are missing an optimization to duplicate the $sort for each branch of the union and keep only a merge-sort instead of the union. We should implement this optimization.


Generated at Thu Feb 08 05:16:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.