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
I have some big queries with lots of includes which caused performance issues so we enabled "QuerySplittingBehavior.SplitQuery".
But for most cases it's not necessary to do the splitting, even better, not all parts of the query need to be split:
var users = _context
.Users
.Include(u => u.SuperVisor) // 1 to many relation, can use a regular join
.Include(u => u.Subordinates) // many to many relation, should do a separate query
.ToList();
Would it be an idea to add an option to UseQuerySplittingBehavior to only split the parts that are a N..N join?
@rogerfar Query splitting is useful whenever a collection is included, regardless of whether that collection is from a many-to-many relationship or a one-to-many relationship.
No it doesn't have many managers, I'm specifically talking about 1 user has 1 manager (1..N correct?), not 1 user has many managers (N..M right?).
Many managers have many users yes, so then it would be much better to have 2 separate queries, but if many users have 1 manager, it's better to have a join query correct?
Maybe my terminology is incorrect, it's been a while :-)
So if you have this condition: 1 user has 1 manager, but 1 user has many subordinates. Now it will split the query in multiple queries, but we can optimize this by splitting only the many to many join.
I'm specifically talking about 1 user has 1 manager (1..N correct?)
Maybe it's 1..N viewed from the manager perspective, but from the user's perspective there's only one manager. That's called a "reference navigation" in EF Core, and these indeed don't generate split queries in EF Core. You should be seeing a simple LEFT JOIN for the Manager.
See the complete code sample below, which shows the EF Core behavior.
In this sample a single Blog has many Posts, each of which has many Comments. The query selects all Posts, including both Blogs and Comments - using AsSplitQuery. The resulting two queries are:
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (22ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [p].[Id], [p].[BlogId], [p].[Title], [b].[Id], [b].[Name]
FROM [Posts] AS [p]
LEFT JOIN [Blogs] AS [b] ON [p].[BlogId] = [b].[Id]
ORDER BY [p].[Id], [b].[Id]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [c].[Id], [c].[PostId], [c].[Text], [p].[Id], [b].[Id]
FROM [Posts] AS [p]
LEFT JOIN [Blogs] AS [b] ON [p].[BlogId] = [b].[Id]
INNER JOIN [Comment] AS [c] ON [p].[Id] = [c].[PostId]
The first query fetches all Posts, including their Blogs - that's precisely the reference navigation we discussed above (since each Post has just one Blog). However, since Comments are a collection navigation on each Post (one-to-many), it is split to a separate query which fetches only the Comment information.
If you're seeing something different, please post a runnable code sample similar to the one below (you can try tweaking that to try to produce the behavior you want).
Interesting, thanks for the detailed response, I will look deeper into my queries, if it already does this then consider this feature request as a dud.
Activity
ajcvickers commentedon Mar 23, 2021
@rogerfar Query splitting is useful whenever a collection is included, regardless of whether that collection is from a many-to-many relationship or a one-to-many relationship.
rogerfar commentedon Mar 23, 2021
@ajcvickers it's not useful when it's a 1 to many at all.
Or are you saying that this
is more effective than this
roji commentedon Mar 23, 2021
It most definitely is, if the LEFT JOIN duplicates a huge amount of Users fields because each User has many Managers.
rogerfar commentedon Mar 23, 2021
No it doesn't have many managers, I'm specifically talking about 1 user has 1 manager (1..N correct?), not 1 user has many managers (N..M right?).
Many managers have many users yes, so then it would be much better to have 2 separate queries, but if many users have 1 manager, it's better to have a join query correct?
Maybe my terminology is incorrect, it's been a while :-)
So if you have this condition: 1 user has 1 manager, but 1 user has many subordinates. Now it will split the query in multiple queries, but we can optimize this by splitting only the many to many join.
roji commentedon Mar 23, 2021
Maybe it's 1..N viewed from the manager perspective, but from the user's perspective there's only one manager. That's called a "reference navigation" in EF Core, and these indeed don't generate split queries in EF Core. You should be seeing a simple LEFT JOIN for the Manager.
rogerfar commentedon Mar 23, 2021
Also when
QuerySplittingBehavior.SplitQueryis set? Because it looks like it's also splitting those.roji commentedon Mar 23, 2021
See the complete code sample below, which shows the EF Core behavior.
In this sample a single Blog has many Posts, each of which has many Comments. The query selects all Posts, including both Blogs and Comments - using AsSplitQuery. The resulting two queries are:
The first query fetches all Posts, including their Blogs - that's precisely the reference navigation we discussed above (since each Post has just one Blog). However, since Comments are a collection navigation on each Post (one-to-many), it is split to a separate query which fetches only the Comment information.
If you're seeing something different, please post a runnable code sample similar to the one below (you can try tweaking that to try to produce the behavior you want).
Code sample
rogerfar commentedon Mar 23, 2021
Interesting, thanks for the detailed response, I will look deeper into my queries, if it already does this then consider this feature request as a dud.