Skip to content

Improve GroupBy translation for JSON-owned properties to avoid N scalar subqueries #37778

Description

@mohsen-mars

What problem are you trying to solve?

Problem

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.


Example Model

public class InvoiceDb
{
    public int Id { get; set; }
    public SummaryDb Summary { get; set; }
}

[NotMapped]
public class SummaryDb
{
    public long TotalAmount { get; set; }
    public long TotalDiscount { get; set; }
}

Mapping (JSON column)

builder.OwnsOne(p => p.Summary, b =>
{
    b.ToJson();
    b.Property(x => x.TotalAmount);
    b.Property(x => x.TotalDiscount);
});

LINQ Query

var result = await dbContext.Invoices
    .GroupBy(x => x.CreatedAt.Date)
    .Select(g => new
    {
        Date = g.Key,
        TotalAmount = g.Sum(x => x.Summary.TotalAmount),
        TotalDiscount = g.Sum(x => x.Summary.TotalDiscount),
    })
    .ToListAsync();

Generated SQL (simplified)

SELECT [s].[Key],
    (SELECT SUM(JSON_VALUE(...TotalAmount...)) ...) AS TotalAmount,
    (SELECT SUM(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.InvoiceDate AS date) AS [Date],
       SUM(CAST(JSON_VALUE(i.Summary, '$.TotalAmount') AS bigint)) AS TotalAmount,
       SUM(CAST(JSON_VALUE(i.Summary, '$.TotalDiscount') AS bigint)) AS TotalDiscount
FROM Invoices i
GROUP BY CAST(i.InvoiceDate AS date);

Environment

  • EF Core version: 10.0.0
  • Database provider: Microsoft.EntityFrameworkCore.SqlServer
  • .NET SDK: .NET 10.0

Activity

  1. roji commented on Feb 25, 2026

    @roji
    Member

    AI Analysis

    Confirmed behavior

    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') AS bigint)), ...) FROM (...) WHERE ...) AS [TotalAmount],
        (SELECT COALESCE(SUM(CAST(JSON_VALUE([i3].[c], '$.TotalDiscount') AS bigint)), ...) FROM (...) WHERE ...) AS [TotalDiscount]
    FROM (...) AS [i0]
    GROUP BY [i0].[Key]

    Desired SQL (single GROUP BY, inline aggregation):

    SELECT CONVERT(date, [i].[CreatedAt]) AS [Date],
           SUM(CAST(JSON_VALUE([i].[Summary], '$.TotalAmount') AS bigint)) AS TotalAmount,
           SUM(CAST(JSON_VALUE([i].[Summary], '$.TotalDiscount') AS bigint)) AS TotalDiscount
    FROM [Invoices] AS [i]
    GROUP BY CONVERT(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() ✅
    • Table-splitting owned types (non-JSON OwnsOne) → produces same N-subquery pattern ❌
    • JSON-owned types → produces same N-subquery pattern ❌

    So the issue affects all owned types when aggregating their properties in a GroupBy.

    Related issues

    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.

  2. added this to the Backlog milestone on Feb 25, 2026
  3. self-assigned this
    on Feb 25, 2026
  4. roji commented on Feb 25, 2026

    @roji
    Member

    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.

  5. mohsen-mars commented on Feb 25, 2026

    @mohsen-mars
    Author

    Thanks for the clarification and for tracking this.
    Appreciate the work on improving GroupBy translation.

  6. QHarshil commented on Sep 4, 2026

    @QHarshil
    Contributor

    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.

  7. added 2 commits that reference this issue on Sep 4, 2026
    cdd7aad
    0a8e61d
  8. modified the milestones: Backlog, 12.0.0 on Sep 10, 2026
  9. added a commit that references this issue on Sep 10, 2026
    49cee0d
  10. added a commit that references this issue on Sep 25, 2026
    f3ff96c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions