这是indexloc提供的服务,不要输入任何密码
Skip to content

use concise correlated subquery syntax for relations with single column mapping #65

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

Merged
merged 3 commits into from
May 1, 2024

Conversation

hallettj
Copy link
Collaborator

@hallettj hallettj commented Apr 29, 2024

Describe your changes

This generates a simpler query plan when there is only one column mapping. Previously a $lookup looked like this:

{
    "$lookup": {
        "from": "students",
        "let": {
            "v__id": "$_id"
        },
        "pipeline": [
            {
                "$match": { "$expr": {
                    "$eq": ["$$v__id", "$classId"]
                } }
            },
            {
                "$replaceWith": {
                    "student_name": { "$ifNull": ["$name", null] },
                },
            }
        ],
        "as": "students",
    },
},

After the change it looks like this:

{
    "$lookup": {
        "from": "students",
        "localField": "_id",
        "foreignField": "classId",
        "pipeline": [
            {
                "$replaceWith": {
                    "student_name": { "$ifNull": ["$name", null] },
                },
            }
        ],
        "as": "students",
    },
},

I think this will give better query performance due to avoiding the use of a variable in an expression. I need to do some more research to confirm that.

Cases with multiple column mappings still use the first form. I'm going to check to see if we can avoid a variable in those cases too.

Issue ticket number and link

MDB-125

@hallettj hallettj requested a review from dmoverton April 29, 2024 17:24
@hallettj hallettj self-assigned this Apr 29, 2024
Copy link
Contributor

@dmoverton dmoverton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@hallettj hallettj merged commit af3cd8b into main May 1, 2024
@hallettj hallettj deleted the jesse/relation-optimization branch May 1, 2024 00:32
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