-
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
Simple query generates many unnecessary LEFT JOINS #12722
Comments
Duplicate of #12022 |
@smitpatel It is similar but I think it is not the same issue. In my issue Owned Entities are LEFT JOINed with the parent entity which is my opinion not necessary because in the Owned Entity properties are at the same table. You see it in the query.
If you have any questions please message me. For us it is important that the bug is fixed and if it is expected behavior to understand why this LEFT JOIN is needed and to have a long-term solution. |
@altmann - Removing Joins with owned entity (due to them being same table) is task of Join Elimination. It is not really accurate at this point. (e.g. #11817). We have a task to refactor whole thing slightly differently so that joins are not created in first place. As for the duplication, when generating collection include query (which has 2 queries), we copy over first query to do inner join in second query. The joins from first query is also copied over. But if you look at the projection inside the inner join subquery then doing left join is not going to change the result. Further we generate Inner join only when it is safe to do (no change of results of outer). Hence those joins are unnecessary and removing them makes shorter & faster SQL. While there are 2 different ways to solve this one, the latter approach is better. |
Looks like Deja Vu for me. Offtop |
@smitpatel Ok thanks for your answer. Luckily we found a workaround (see above) but I hope this is only temporary in our project. What do think, when or with which version we get a more optimized query? |
I'm seeing this issue of useless |
@matteocontrini - Your issue is certainly not same as this one. Please file a new issue with detailed repro. |
Thanks. For reference: #18299 |
In our project we observe that a simple query of an entity causes many unnecessary LEFT JOINs.
The following setup causes the problem:
SingleAsync()
(querying withToListAsync()
is no problem)Include(x => x.SimpleEntities)
When I execute
var person = appContext.Persons.Include(x => x.SimpleEntities).SingleAsync(x => x.Id == 1);
then the following query is generated:The problematic part of the query is the LEFT JOIN which is completely unnecessary. Person Id is compared with "x.SimpleOwnedEntity0"."Id" which is also the Person Id.
The bad thing is that we use in our data model many Owned Entities and the query generate for each Owned Entity one LEFT JOIN. Because of that our query in our real project contains many (more then 60) unnecessary LEFT JOINS.
We use in our project Sqlite which has an limitation: Maximal 64 tables in a join query. This limit is not configureable and because EF Core produces many unnecessary LEFT JOINs in a query we reach the limit of 64 tables in a join query.
We also tried to use the InMemoryDatabase from Ef Core but here we faced very poor performance caused by the many LEFT JOINs. So this is not a practical solution for us.
Our temporary workaround: We don't query the database with
SingleAsync()
because this contains the problem. Instead we query the database with.Where(x => x.Id == 1).ToListAsync()
and then make a.Single()
.Steps to reproduce
Here is a complete solution with a very small setup which produce the LEFT JOINs.
EfCoreSample.zip
Further technical details
EF Core version: v2.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Sqlite
Operating system: Windows 10
IDE: Visual Studio 2017 15.7.5
The text was updated successfully, but these errors were encountered: