You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Query: optimize queries projecting correlated collections, so that they don't result in N+1 database queries #9282
In #8584 we allowed include pipeline to be reused for queries that project collection navigations as long as they are not composed on. This results in generating much better queries for those cases - no longer producing N+1 queries.
This could be further enhanced to support collection navigations that are composed on (e.g. customers.Select(c => c.Orders.Where(o => o.City == "London")), so that those queries also stop producing N+1 queries.
Reacted by Kim Tranjan, Jon P Smith, Kieren Johnstone, Hicham Bouchikhi, sir-boformer, Michael Sogos, Sam, proff, Shimmy Weitzhandler, Andrew Boklashko and 5 more
I think I'm running into the same problem with the following projection query. The one-to-manys/collections are causing N+1 queries to occur. This is different than the behavior in Entity Framework 6 which creates nested JOINs and no N+1 queries. This is a use case that is important for a reporting application that I have. I have been wanting to switch to Entity Framework Core for everything, but, have run into issues like this. I think this is one of the last remaining issues that I have.
changed the title [-]Query: improve include pipeline so it can be reused for queries projecting composed collection navigations (filters and projections)[/-][+]Query: optimize queries projecting correlated collections, so that they don't result in N+1 database queries[/+]on Jan 10, 2018
@jemiller0 I looked at the query you provided, and the problem is that adding filter after the projection makes it a much complicated query, from EF perspective - we create quite a complicated predicate, which gets client-evaluated and that disables correlated collection optimization and also this is why you don't see a WHERE clause in your query.
You can modify the query slightly (move the filter before the projection):
Thanks @maumar. I'm sorry I didn't get back to you yet. Some things came up a work and I haven't had a chance to test. The main reason I'm putting the filter at the bottom like that is that I'm using the query with a grid control (RadGrid) from a company called Telerik. The grid control allows you to enable filtering controls at the top of the grid and generates Dynamic LINQ filter strings. The output projection field names are what the grid knows. Otherwise, I agree, it would be best to do the filtering at the top.
filed #10811 to track the RadGrid issue, perhaps we can do some QM rewrite to improve the queries, making them take advantage of N+1 optimization (and others)
@Liero@MaklaCof Can you please file new issues including a runnable project/solution or complete code listing so that we can investigate what you are seeing. (This issue is closed, so it is unlikely that posting here will result in any action.)
var maps = ( from map in Set<Map>().Include(m => m.MapPoints) select new { Map = map, TotalCount = map.MapPoints.Count() } ) .OrderBy(m=>m.Map.Id) .Skip(0) .Take(10) .ToList();
I have the code above. The table map has a one to many relation to MapPoints. So in this query I'm trying to list all maps and in the same query count the number of points from the MapPoints table.
We are using EF core 2.2 and have problem with N+1.
@maumar Is this supposed to be fixed in that version or is this specific issue that I have not fixed in 2.2?
@patcor 2.1 and 2.2 fix has a number of limitations, using Skip/Take is one of them. We have addressed them all in 3.0 which now produces a single query for all nested navigation/include scenarios
Activity
sir-boformer commentedon Oct 31, 2017
This feature is quite important for complex DTO/ViewModel generation. Here is a simple example
A common view model for a list of chat items:
A typical query to retrieve a list of chat list view models:
This generates n+1 queries (2 for each list item):
jemiller0 commentedon Nov 6, 2017
I think I'm running into the same problem with the following projection query. The one-to-manys/collections are causing N+1 queries to occur. This is different than the behavior in Entity Framework 6 which creates nested JOINs and no N+1 queries. This is a use case that is important for a reporting application that I have. I have been wanting to switch to Entity Framework Core for everything, but, have run into issues like this. I think this is one of the last remaining issues that I have.
Does anyone know if this issue will be solved sometime soon?
Fix to #9282 - Query: improve include pipeline so it can be reused fo…
Fix to #9282 - Query: improve include pipeline so it can be reused fo…
Fix to #9282 - Query: improve include pipeline so it can be reused fo…
Fix to #9282 - Query: improve include pipeline so it can be reused fo…
[-]Query: improve include pipeline so it can be reused for queries projecting composed collection navigations (filters and projections)[/-][+]Query: optimize queries projecting correlated collections, so that they don't result in N+1 database queries[/+]20 remaining items
maumar commentedon Jan 24, 2018
@jemiller0 I looked at the query you provided, and the problem is that adding filter after the projection makes it a much complicated query, from EF perspective - we create quite a complicated predicate, which gets client-evaluated and that disables correlated collection optimization and also this is why you don't see a WHERE clause in your query.
You can modify the query slightly (move the filter before the projection):
and you will get much better queries. (Disclaimer: I only tested it with a simplified model so there still might be more issues.)
jemiller0 commentedon Jan 24, 2018
Thanks @maumar. I'm sorry I didn't get back to you yet. Some things came up a work and I haven't had a chance to test. The main reason I'm putting the filter at the bottom like that is that I'm using the query with a grid control (RadGrid) from a company called Telerik. The grid control allows you to enable filtering controls at the top of the grid and generates Dynamic LINQ filter strings. The output projection field names are what the grid knows. Otherwise, I agree, it would be best to do the filtering at the top.
maumar commentedon Jan 30, 2018
filed #10811 to track the RadGrid issue, perhaps we can do some QM rewrite to improve the queries, making them take advantage of N+1 optimization (and others)
Liero commentedon Mar 19, 2018
Hi, what about this query, which is still producing N+1 queries in EF2.1 preview:
dbContext.ForumThreads.Include(t => t.Posts)
.Select(t => new
{
t.Id,
t.Title,
PostAuhtors = t.Posts.Select(p => p.AuthorName).Take(5).ToArray()
}).ToArray();
https://stackoverflow.com/questions/49346001/how-to-avoid-n1-queries-in-ef-core-2-1/
MaklaCof commentedon Mar 19, 2018
I did not try this in 2.1, but I would like to mention another scenario (FirstOrDefault):
ajcvickers commentedon Mar 19, 2018
@Liero @MaklaCof Can you please file new issues including a runnable project/solution or complete code listing so that we can investigate what you are seeing. (This issue is closed, so it is unlikely that posting here will result in any action.)
maumar commentedon Mar 19, 2018
@Liero this is a known limitation (there are other limitations that we are aware of, listed here: b95f23f)
@MaklaCof this is already fixed by #10813
patcor commentedon Nov 18, 2019
var maps = ( from map in Set<Map>().Include(m => m.MapPoints) select new { Map = map, TotalCount = map.MapPoints.Count() } ) .OrderBy(m=>m.Map.Id) .Skip(0) .Take(10) .ToList();I have the code above. The table map has a one to many relation to MapPoints. So in this query I'm trying to list all maps and in the same query count the number of points from the MapPoints table.
We are using EF core 2.2 and have problem with N+1.
@maumar Is this supposed to be fixed in that version or is this specific issue that I have not fixed in 2.2?
maumar commentedon Nov 19, 2019
@patcor 2.1 and 2.2 fix has a number of limitations, using Skip/Take is one of them. We have addressed them all in 3.0 which now produces a single query for all nested navigation/include scenarios