Skip to content

[PoC] Push simple filter conditions into $lookup stage. #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

WaVEV
Copy link
Collaborator

@WaVEV WaVEV commented Jul 18, 2025

In this PR we tackle the slow join pushing the simple conditions inside the $lookup. the condition is also fix a bug.

New query:

[{'$lookup': {'as': 'm2m_regress_selfrefer_references',
              'from': 'm2m_regress_selfrefer_references',
              'let': {'parent__field__0': '$_id'},
              'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$$parent__field__0',
                                                                   '$to_selfrefer_id']},
                                                          {'$eq': ['$from_selfrefer_id',
                                                                   ObjectId('687ad601daccf7b757ff46ec')]}]}}}]}},
 {'$unwind': '$m2m_regress_selfrefer_references'},
 {'$match': {'$expr': {'$eq': ['$m2m_regress_selfrefer_references.from_selfrefer_id',
                               ObjectId('687ad601daccf7b757ff46ec')]}}}]

Previous query

[{'$lookup': {'as': 'm2m_regress_selfrefer_references',
              'from': 'm2m_regress_selfrefer_references',
              'let': {'parent__field__0': '$_id'},
              'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$$parent__field__0',
                                                                   '$to_selfrefer_id']}]}}}]}},
 {'$unwind': '$m2m_regress_selfrefer_references'},
 {'$match': {'$expr': {'$eq': ['$m2m_regress_selfrefer_references.from_selfrefer_id',
                               ObjectId('687ad676396a5de149f2ea4d')]}}}]

@WaVEV WaVEV marked this pull request as ready for review July 18, 2025 23:21
@WaVEV WaVEV changed the title Push simple filter conditions into $lookup stage. [PoC] Push simple filter conditions into $lookup stage. Jul 18, 2025
@WaVEV WaVEV requested review from timgraham and Jibola July 19, 2025 18:33
@WaVEV WaVEV force-pushed the INTPYTHON-635-Slow-joins-for-ManyToMany-fields branch from 129893f to 3e685a8 Compare July 21, 2025 19:29
@timgraham
Copy link
Collaborator

Does it address #309? Do we need some additional tests for other cases besides the changed test?

I'll have to take it on faith that this does the right thing, because it would take me a lot of time to understand this code. Some comments explaining variable names like promote_filters, pushed_expressions, and rerooted_replacement as well as what cases this covers (what are "simple cases"?) would be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants