Skip to content
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

Unneeded subquery and projection when using ordering without limit/offset in set operations #30684

Closed
roji opened this issue Apr 13, 2023 · 0 comments · Fixed by #30685
Closed
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@roji
Copy link
Member

roji commented Apr 13, 2023

LINQ:

_ = ctx.Blogs
    .Select(b => b.Id)
    .Union(ctx.Blogs.OrderBy(b => b.Name).Select(b => b.Id))
    .ToList();

SQL:

SELECT [b].[Id]
FROM [Blogs] AS [b]
UNION
SELECT [t0].[Id]
FROM (
  SELECT [b0].[Id], [b0].[Name]
  FROM [Blogs] AS [b0]
) AS [t0]

This happens because when applying a set operation, we pushdown to a subquery when there are orderings, but those orderings are subsequently removed in the subquery because subquery ordering isn't preserved outside.

Note that set operations generally don't maintain ordering - except for Concat. However, even for Concat, databases don't generally allow applying ordering to set operation operands (which is very different from applying ordering to the result of the set operation). We may need to research more into this if we decide it's important.

Also, when we do push down (e.g. because there's limit/offset), we currently project the ordering column(s) out of the subquery, since usually the the outer query orders by it (ordering lifting). But in the case of set operations the outer ordering is immediately cleared - again since set operations don't preserve ordering - and we're just left with the useless projection.

@roji roji self-assigned this Apr 13, 2023
roji added a commit to roji/efcore that referenced this issue Apr 13, 2023
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 13, 2023
roji added a commit to roji/efcore that referenced this issue Apr 14, 2023
roji added a commit to roji/efcore that referenced this issue Apr 14, 2023
roji added a commit to roji/efcore that referenced this issue Apr 14, 2023
@ajcvickers ajcvickers added this to the 8.0.0-preview4 milestone Apr 20, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0-preview4, 8.0.0 Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants