-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Avoid duplicated subquery when translating LINQ intermediate projections #27285
Comments
Note that if the same subquery ends up getting referenced twice in SQL (e.g. multiple joins to the same subquery), a WITH expression can be used to extract that out and avoid duplication. |
Duplicate of #7776 |
@smitpatel does this get unblocked (or even implemented) as part of the pending selector work you have planned? |
It will automatically work after pending selector work. I plan to add tests for various scenarios from linked issues on #7776 |
That's great news, thanks! |
I would have needed this a few times in my projects. @smitpatel Is there any estimation when this might be implemented? As a workaround one could use the |
@InspiringCode we're looking into improving this, but it's quite a bit architectural change that can't be done lightly. Re AsSubquery(), that's generally not a direction we want to go in - LINQ is about expressing your queries via .NET code, and not via SQL concepts (like "subquery"). At the end of the day is this an internal problem in how EF translates LINQ queries, so it should be fixed inside EF rather than forcing the user to work around it via additional operators. |
@roji Is there any workaround to achieve this behavior at the time? |
@InspiringCode whatever AsSubquery is doing should be possible for the PG provider as well - both are EF relational providers. Beyond that, I'm not aware of a workaround at this point. |
@roji It implements some kind of visitor and calls PushdownToSubquery (I don't remember the exact name) if it encounters a CallExpression with the AsSubQuery method (which is inserted in the expression tree by the AsSubQuery method). But requires quite some classes and infrastructure and is not a trivial thing. Is there any easier way to plug into the expression tree processing or call PushdownToSubquery in another way? |
@InspiringCode unfortunately not, there's no magic solution here. But I do think what AsSubquery is doing should pretty much work as-is. |
Given the following query:
... we currently generate the following SQL:
Note that the LINQ Select gets duplicated twice as a subquery in the SQL. Rather than duplicating, the LINQ intermediate projection can be applied as a subquery:
... allowing SQL Server to evaluate the subquery just once. The SQL Server plan TotalSubtreeCost for the former query is 1.63, whereas the WITH-based alternative is 1.00, which is very significant.
Full plan details
Current query with duplicated subquery
Proposed single subquery variant
/cc @smitpatel maybe good to keep this in mind for your upcoming pending select work.
Note: exposing WITH as a user-facing API is tracked by #26486; this is different (and seems more important).
The text was updated successfully, but these errors were encountered: