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
Improve GroupBy translation for JSON-owned properties to avoid N scalar subqueries #37778
When grouping and aggregating owned JSON properties, EF Core translates each aggregate into a correlated scalar subquery instead of a single GROUP BY aggregation.
This results in multiple scans over the same dataset and significantly degrades performance for large datasets.
SELECT [s].[Key],
(SELECTSUM(JSON_VALUE(...TotalAmount...)) ...) AS TotalAmount,
(SELECTSUM(JSON_VALUE(...TotalDiscount...)) ...) AS TotalDiscount
FROM (...)
GROUP BY [s].[Key];
Each aggregate is translated into a separate correlated scalar subquery, causing multiple scans of the same data.
Impact
This affects reporting and analytical workloads and makes aggregation over JSON-owned properties impractical for large datasets.
It also limits adoption of JSON-owned types in real-world reporting scenarios where server-side aggregation is required.
Describe the solution you'd like
Suggested Behavior
Translate to a single GROUP BY with all JSON_VALUE projections in one pass, e.g.:
SELECT CAST(i.InvoiceDateASdate) AS [Date],
SUM(CAST(JSON_VALUE(i.Summary, '$.TotalAmount') ASbigint)) AS TotalAmount,
SUM(CAST(JSON_VALUE(i.Summary, '$.TotalDiscount') ASbigint)) AS TotalDiscount
FROM Invoices i
GROUP BY CAST(i.InvoiceDateASdate);
The issue is confirmed on EF Core 10.0.3 / SQL Server. When using GroupBy with aggregation over JSON-owned properties, each aggregate is translated as a separate correlated scalar subquery instead of an inline aggregation within the GROUP BY:
Generated SQL (2 subqueries for 2 aggregates):
SELECT [i0].[Key] AS [Date],
(SELECT COALESCE(SUM(CAST(JSON_VALUE([i1].[c], '$.TotalAmount') ASbigint)), ...) FROM (...) WHERE ...) AS [TotalAmount],
(SELECT COALESCE(SUM(CAST(JSON_VALUE([i3].[c], '$.TotalDiscount') ASbigint)), ...) FROM (...) WHERE ...) AS [TotalDiscount]
FROM (...) AS [i0]
GROUP BY [i0].[Key]
Desired SQL (single GROUP BY, inline aggregation):
SELECTCONVERT(date, [i].[CreatedAt]) AS [Date],
SUM(CAST(JSON_VALUE([i].[Summary], '$.TotalAmount') ASbigint)) AS TotalAmount,
SUM(CAST(JSON_VALUE([i].[Summary], '$.TotalDiscount') ASbigint)) AS TotalDiscount
FROM [Invoices] AS [i]
GROUP BYCONVERT(date, [i].[CreatedAt])
Additional finding
This is not specific to JSON-owned types. I also tested with:
Regular scalar properties on the entity → produces efficient single GROUP BY with inline SUM() ✅
JSON-owned types → produces same N-subquery pattern ❌
So the issue affects all owned types when aggregating their properties in a GroupBy.
Related issues
Aggregate function on owned type fails in EF8 #36990 – "Aggregate function on owned type fails in EF8" – a different but related issue where certain aggregates (e.g. Count with predicate) on owned types throw an exception rather than producing suboptimal SQL. Already milestoned for 11.0.0 and assigned to @roji. The fix for Aggregate function on owned type fails in EF8 #36990 may also be the natural place to address this suboptimal translation pattern.
Thanks. We're aware that there are various GroupBy SQL issues, and I hope to do a concerted push on improving the quality of GroupBy translation - I'll definitely take a look at this when that happens.
This traces to the same owned-navigation binding as #36990: the aggregate lambda is remapped onto the grouping element and nothing expands the owned navigation there, so each aggregate falls back to its own correlated subquery. I have an implementation that removes that for JSON-mapped owned types as well.
Root cause and the proposed approach are on #36990. Waiting on query-team feedback there before opening a PR.
Activity
roji commentedon Feb 25, 2026
AI Analysis
Confirmed behavior
The issue is confirmed on EF Core 10.0.3 / SQL Server. When using
GroupBywith aggregation over JSON-owned properties, each aggregate is translated as a separate correlated scalar subquery instead of an inline aggregation within theGROUP BY:Generated SQL (2 subqueries for 2 aggregates):
Desired SQL (single GROUP BY, inline aggregation):
Additional finding
This is not specific to JSON-owned types. I also tested with:
GROUP BYwith inlineSUM()✅OwnsOne) → produces same N-subquery pattern ❌So the issue affects all owned types when aggregating their properties in a
GroupBy.Related issues
Countwith predicate) on owned types throw an exception rather than producing suboptimal SQL. Already milestoned for 11.0.0 and assigned to @roji. The fix for Aggregate function on owned type fails in EF8 #36990 may also be the natural place to address this suboptimal translation pattern.This is filed as a feature request (not a bug), and the behavior is technically correct — it just produces suboptimal SQL with N scans instead of 1.
roji commentedon Feb 25, 2026
Thanks. We're aware that there are various GroupBy SQL issues, and I hope to do a concerted push on improving the quality of GroupBy translation - I'll definitely take a look at this when that happens.
mohsen-mars commentedon Feb 25, 2026
Thanks for the clarification and for tracking this.
Appreciate the work on improving GroupBy translation.
QHarshil commentedon Sep 4, 2026
This traces to the same owned-navigation binding as #36990: the aggregate lambda is remapped onto the grouping element and nothing expands the owned navigation there, so each aggregate falls back to its own correlated subquery. I have an implementation that removes that for JSON-mapped owned types as well.
Root cause and the proposed approach are on #36990. Waiting on query-team feedback there before opening a PR.
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)