Repository navigation
Description
EF Core 7 for SQL Server has a SQL query translation regression from EF Core 6 for GroupBy with aggregate functions.
For the query:
var query = context.Items
.GroupBy(x => x.Group)
.Select(x => new
{
Set1Value1 = x.Sum(v => v.Set1.Value1),
Set1Value2 = x.Sum(v => v.Set1.Value2),
Set2Value1 = x.Sum(v => v.Set2.Value1),
Set2Value2 = x.Sum(v => v.Set2.Value2),
});
SQL generated from EF Core 6:
SELECT COALESCE(SUM([i].[Set1_Value1]), 0) AS [Set1Value1], COALESCE(SUM([i].[Set1_Value2]), 0) AS [Set1Value2], COALESCE(SUM([i].[Set2_Value1]), 0) AS [Set2Value1], COALESCE(SUM([i].[Set2_Value2]), 0) AS [Set2Value2]
FROM [Items] AS [i]
GROUP BY [i].[Group]
SQL generated from EF Core 7.0.0
SELECT (
SELECT COALESCE(SUM([i0].[Set1_Value1]), 0)
FROM [Items] AS [i0]
WHERE [i].[Group] = [i0].[Group] OR (([i].[Group] IS NULL) AND ([i0].[Group] IS NULL))) AS [Set1Value1], (
SELECT COALESCE(SUM([i1].[Set1_Value2]), 0)
FROM [Items] AS [i1]
WHERE [i].[Group] = [i1].[Group] OR (([i].[Group] IS NULL) AND ([i1].[Group] IS NULL))) AS [Set1Value2], (
SELECT COALESCE(SUM([i2].[Set2_Value1]), 0)
FROM [Items] AS [i2]
WHERE [i].[Group] = [i2].[Group] OR (([i].[Group] IS NULL) AND ([i2].[Group] IS NULL))) AS [Set2Value1], (
SELECT COALESCE(SUM([i3].[Set2_Value2]), 0)
FROM [Items] AS [i3]
WHERE [i].[Group] = [i3].[Group] OR (([i].[Group] IS NULL) AND ([i3].[Group] IS NULL))) AS [Set2Value2]
FROM [Items] AS [i]
GROUP BY [i].[Group]
EF Core 7 is now adding sub queries to the SELECT for each of the SUM functions. I believe that the EF Core 6 SQL is better and more efficient.
I have created a repo https://github.com/martosource/EfCoreGroupByOwnedTest
Reacted by cyril265, Andrew Schlackman, Julien Jacobs, Dan Ports, rofenix2, Georg Jung, Gary Anderson, Philip Cox, simsek25, Oron and 6 more
Activity
raffaele-cappelli commentedon Nov 17, 2022
I agree, I just noted the same problem. Many of my queries are 10x slower !
maumar commentedon Nov 17, 2022
per conversation with Smit - this is the commit that introduced the change - 21bbb3f, specifically the removal of GroupByAggregateLiftingExpressionVisitor. With the design changes and new scenarios that were enabled, aggregate lifting became too complicated to do reliably
martosource commentedon Nov 17, 2022
@maumar does this means it won't or can't be fixed?
Using sub queries in the SELECT is a real performance killer.
[-]EF Core 7 GroupBy with Owned Entity Regression[/-][+]EF Core 7 GroupBy with Owned Entity perf Regression[/+]maumar commentedon Nov 17, 2022
@martosource realistically the chances of fixing this (soon) are low. The proper fix is very hard and the person who had the most knowledge about the area no longer works in the team, which makes it even harder/riskier. I found a hacky workaround - test it out to see if it's acceptable for your case:
produces:
The idea is to expand and project all the needed navigations before you do group by. EF is smart enough to counter act this, but for that purpose the workaround introduces Distinct - it causes a pushdown and "locks" the shape of projection, so EF engine can't do anything but accept the shape. Distinct can change the results (if there are any duplicates), so to counteract that, we introduce a dummy guid, that always guarantees the uniqueness. You can drop the dummy guid if you are sure the results are unique. But you also need to assess if the distinct slowdown is bad or not.
edit: actually, projecting the owner key instead of generating guid should be better/faster in almost every case
martosource commentedon Nov 18, 2022
@maumar thank you, I will give this a try.
raffaele-cappelli commentedon Nov 18, 2022
Thank you @maumar for your quick reply and workaround. In my case I have too many queries like this to apply the workaround, and I can confirm the performance loss is large (about 10x slower in my application on SqlServer). I'll have to stick with EF6 until this bug is solved.
15 remaining items
Query: expand owned navigations in GroupBy aggregate selectors
Query: expand owned navigations in GroupBy aggregate selectors
Query: expand owned navigations in GroupBy aggregate selectors (dotne…
Query: expand owned navigations in GroupBy aggregate selectors (#38918)