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

Query: Include for both navigations of One-To-Many relationship should use split query #12775

Closed
smitpatel opened this issue Jul 24, 2018 · 2 comments
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed.

Comments

@smitpatel
Copy link
Contributor

Currently when we include a collection navigation, we send 2 queries to the server.
e.g.
db.Blogs.Include(b => b.Posts).ToList();
Generates

      SELECT [b].[Id]
      FROM [Blogs] AS [b]
      ORDER BY [b].[Id]

      SELECT [b.Posts].[Id], [b.Posts].[BlogId]
      FROM [Post] AS [b.Posts]
      INNER JOIN (
          SELECT [b0].[Id]
          FROM [Blogs] AS [b0]
      ) AS [t] ON [b.Posts].[BlogId] = [t].[Id]
      ORDER BY [t].[Id]

We do above because of the cardinality. i.e. When each blog has 10 related post, doing single query would cause each blog to be repeated 10 times in the join causing unnecessary data fetch from the server. It also causes "dreaded cartesian product" when you are doing multiple includes since each navigation would add additional join and more duplicated data. So we chose this design in EF Core.

But when you do include on other way round on same relationship.
e.g.
db.Set<Post>().Include(p => p.Blog).ToList();
Generates

  SELECT [p].[Id], [p].[BlogId], [p.Blog].[Id]
  FROM [Post] AS [p]
  LEFT JOIN [Blogs] AS [p.Blog] ON [p].[BlogId] = [p.Blog].[Id]

Here we are duplicating the Blogs for each post which has same parent. While we do not materialize each blog due to identity resolution, we still fetch the data from server and create value buffer.
We could actually use split query like first one and get related Blog (parent) in separate query without causing duplication.

Same is applicable for correlated subquery.

@ajcvickers
Copy link
Member

Added to #12795

@smitpatel
Copy link
Contributor Author

Closing since we decided to do #12098 and this issue is opposite of it.

@smitpatel smitpatel removed this from the 3.0.0 milestone Jan 25, 2019
@smitpatel smitpatel removed their assignment Jan 25, 2019
@ajcvickers ajcvickers added the closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. label Mar 10, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed.
Projects
None yet
Development

No branches or pull requests

2 participants