-
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
GroupBy Children - N+1 DB trips #10472
Comments
Multiple queries is the expected behavior currently and this specific case won't be improved in 2.1 release. We can translate subqueries that result in just one element (e.g. p.Children.Count). If subquery produces a collection we often can translate it into two queries (see #9282), however optimization has a number of limitations, including GroupBy result operator. In the future release we might improve the translation. You can try performing GroupBy operation on the client, which might be faster than N+1, depending on the data. ctx.Parents
.Select(p => new
{
Groups = p.Children.Select(c => c.Group).ToList(),
ChildrenCount = p.Children.Count,
ParentId = p.Id,
})
.ToList()
.Select(s => new FooVm
{
Bars = s.Groups
.GroupBy(g => g)
.Select(g => new BarVm
{
Group = g.Key,
}),
}).ToList(); produces the following two queries that are later grouped on the client: SELECT [p].[Id] AS [ParentId], (
SELECT COUNT(*)
FROM [Children] AS [c]
WHERE [p].[Id] = [c].[ParentId]
) AS [ChildrenCount]
FROM [Parents] AS [p]
ORDER BY [p].[Id]
SELECT [t].[Id], [p.Children].[Group], [p.Children].[ParentId]
FROM [Children] AS [p.Children]
INNER JOIN (
SELECT [p0].[Id]
FROM [Parents] AS [p0]
) AS [t] ON [p.Children].[ParentId] = [t].[Id]
ORDER BY [t].[Id] |
this scenario is not supported now due to #15873- we need key of the child to know how to properly bucket the results in the correlated collection scenario. Due to Distinct/GroupBy semantics we can't add these necessary columns to the projection, so the scenario is blocked. If the user manually provides those necessary columns (by grouping by them) we still hit the problem tracked by #21965 (we try to add parent side of the correlation predicate into the inner projection, which causes an error because now we project a column that is not part of the grouping key or aggregate) |
closing as duplicate |
I couldn't find such existing issues or information anywhere
Further technical details
EF Core version: 2.0.0
Database Provider: SQLite (same with Npgsql.EntityFrameworkCore.PostgreSQL)
Operating system: MacOS High Sierra 10.13.1
IDE: Visual Studio Code 1.18.1
Steps to reproduce
A complete solution with the test scenario https://github.com/mudrz/TestsMaxGroupByEF
If we query children and group - they get pulled in memory and grouped - expected
If we query parents and group children - we get N+1 trips to DB
Models
Views
Query the children
This one works as expected
Group
gets pulled in memory and locally it is grouped.Query the parents
This one though
Double selecting still produced the same result
Questions
FromSql
is not a solution, since the real-world scenario pulls various different expressions, merges them dynamically and produces the query)The text was updated successfully, but these errors were encountered: