-
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
Query: SQL for queries which are projecting out single result from collection in projection #10001
Comments
Can we get an ETA on this fix? Right now, we have several lateral joins (cross/outer apply) and it's resulting in N + 1 queries. If there are a 100 rows, it results in 101 queries instead of 1 query. Sometimes, we have several lateral joins....this can results in 100s of queries just for 1 query. It increases the CPU usage and increases the latency. In Entity Framework 6, this would be 1 query (for 100 rows) and it would correctly translate the LINQ to Cross/Outer Apply.....in Entity Frame Core 2.1 Final, it's 101 queries (for 100 rows). Example:
LINQ:
Rewriting the "LINQ" for all these queries "just to make it work" is costly and not a good solution. To make it use CROSS APPLY, we have to let ld = from db.Details.Where().OrderBy().FirstOrDefault() and then left join again to db.Details. |
It's too bad this got postponed. But thanks for the update. |
Hi, Any possibility to have this issue fixed as soon as possible, in the next minor/major release ? Thank you |
@zulander1 It's currently in the 3.0 milestone, which realistically is the best we can hope to do at this point. |
While people on the OLD EF can write queries...I have to use the OLD EF to generate the best query...then copy the resulting working SQL query and use FromSQL...and hack away to make this usable. On the other hand I've refreshed my TSQL skills due to this bug. So I guess that's a "plus" |
It also seems to break |
Hi, I'm having the same issue. As many to many is not already supported, creating a request to retrieve information from this kind of relationship like Role and User dbset, it will make N+1 queries. Steps to reproduceContext = new MainContext();
var request = Context.Users.Select(data => new UserDTO
{
Id = data.Id,
Roles = data.UserRoles.Select(x => x.Role.Name),
})
.ToList(); // We also tried without it
// Foreach to demonstrate request executing in console
foreach (var item in request)
{
var roles = item.Roles;
}
Console.ReadLine(); Further technical detailsEF Core version: 2.1.4 |
@Rubis61 for your scenario, you can take advantage of correlated collection optimization we added recently (see: b95f23f for some detailed information). Just need to append var request = Context.Users.Select(data => new UserDTO
{
Id = data.Id,
Roles = data.UserRoles.Select(x => x.Role.Name).ToList(),
})
.ToList(); Correlated collection optimization has several limitations, e.g. doesn't work if you want to return just one element of the inner collection, that's what the issue is tracking. |
@ajcvickers are will still on target for this ? |
Triage: we need to make this work for Sqlite also--so try window function or SQlite specific re-write. |
Does this work with includes and sub where now? |
Generates following SQL
It is N+1 queries. Since there is
FirstOrDefault
operator (hence only 1 related row max), instead of going through 2 queries form like include, we can just do cross apply (which include tries to avoid with 2 queries) since there is no duplicated data.Expected SQL:
The text was updated successfully, but these errors were encountered: