-
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
EF Core 2.0.2 generates multiple query for a subquery #11677
Comments
I have also changed the query to: let LastStatus = task.TaskStatus.Where(x => x.Deleted == false).OrderByDescending(x => x.CreatedDate).Select(x => x.Status.Name).First() same results Tested with EF core 2.1.0-preview2-final still the same issue |
Try using FirstOrDefault instead, EFCore is unable to mimic throwing behavior of First if it gets fully translated to sql, so it’s evaluated on the client. There are some issues with using “let” keyword, so you can also try without it - this should produce a single efficient query. |
It's working with the FirstOrDefault() what do you mean by “let” keyword, so you can also try without it |
Defining “let LastStatus = ...” so you can reuse it later in the query. There are known problem when the sub query defined using let appears more than once later |
Using the model from Many-to-many sample and 2.1.0-preview2-final, the following query still generates N + 1 queries though:
|
@powermetal63 this is a known issue tracked by #10001 |
Well, it just looks the same as the one from original poster and they claim that their issue is fixed by removing the Anyway, as I understand now, it's not supposed to be addressed in the initial 2.1. Good to know in case similar questions arrive at StackOverflow. Thanks. |
@powermetal63 this one is different because the subquery returns more than one column. In SQL you can inline a subquery that returns a single scalar (like it was in the first example), but if the subquery returns an entity (i.e. multiple columns) it can't be done. For those cases we split it into two queries and patch the results on the client, but the optimization currently doesn't apply if there are any result operators in the subquery. |
Makes sense. But...
still does N + 1 queries even if I select a single value. Just FYI :) |
@powermetal63 - We are tracking it at #11186 |
I am not sure if it's a bug not but I am getting some major performance issue. EF Core is generating multiple query for a simple statement.
This is the query:
Expected resuts:
However EF is generating:
https://i.stack.imgur.com/22H8w.png
https://stackoverflow.com/questions/49834550/ef-core-2-0-2-generates-multiple-query-for-a-subquery
The text was updated successfully, but these errors were encountered: