AsSplitQuery: non-collection-Includes are repeated in all SQL statements

Open
#24,552 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
28/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp, sql
Domain
backend, databases

Research direction

Start at the AsSplitQuery entry point and reproduce the two SQL statements shown in the issue, including the PriceType ThenInclude case. Trace how split-query statements select included navigations; done means non-collection navigations appear only in the statement that needs them while collection data and its nested navigation still load correctly.

Written by the indexing model from the issue text.

Description

area-perf area-query customer-reported

When loading one or more collections along with non-collection navigational properties then non-collections are repeated in every SQL statement.

Example:

var products = _ctx.Products
                   .AsSplitQuery()
                   .Include(p => p.ProductGroup) // non-collection
                   .Include(p => p.Prices) // collection
                   .ToList();

The LINQ query leads to 2 SQL statements.

SELECT [p].[Id], [p].[Name], [p].[ProductGroupId], [p0].[Id]
FROM [Products] AS [p]
INNER JOIN [ProductGroups] AS [p0] ON [p].[ProductGroupId] = [p0].[Id]
ORDER BY [p].[Id], [p0].[Id]

SELECT [p1].[Id], [p1].[ProductId], [p1].[Value], [p].[Id], [p0].[Id]
FROM [Products] AS [p]
INNER JOIN [ProductGroups] AS [p0] ON [p].[ProductGroupId] = [p0].[Id]
INNER JOIN [Prices] AS [p1] ON [p].[Id] = [p1].[ProductId]
ORDER BY [p].[Id], [p0].[Id]

As we can see the non-collection navigational property ProductGroup is present in both SQL statements, although it is not necessary in the 2nd statement.

Is it possible to optimize the current behavior so the non-collections are present only once?

Entity types

public class Product
{
   public int Id { get; set; }
   public string Name { get; set; }

   public int ProductGroupId { get; set; }
   public virtual ProductGroup ProductGroup { get; set; }

   public virtual List<Price> Prices { get; set; }
}

public class ProductGroup
{
   public int Id { get; set; }

   public virtual List<Product> Products { get; set; }
}

public class Price
{
   public int Id { get; set; }
   public int ProductId { get; set; }
   public decimal Value { get; set; }
}

DbContext incl. mappings

public class DemoDbContext : DbContext
{
   public DbSet<Product> Products { get; set; }
   public DbSet<ProductGroup> ProductGroups { get; set; }
   public DbSet<Price> Prices { get; set; }

   public DemoDbContext(DbContextOptions<DemoDbContext> options)
      : base(options)
   {
   }

   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
      base.OnModelCreating(modelBuilder);

      modelBuilder.Entity<ProductGroup>(builder => builder.Property(g => g.Id).ValueGeneratedNever());
      modelBuilder.Entity<Studio>(builder => builder.Property(s => s.Id).ValueGeneratedNever());
      modelBuilder.Entity<Product>(builder => builder.Property(p => p.Id).ValueGeneratedNever());
      modelBuilder.Entity<Price>(builder => builder.Property(p => p.Id).ValueGeneratedNever());
   }
}

In the example above the ProductGroup should be loaded with the first query only.


Another use case is, when the Prices would have a non-collection navi PriceType as well, something like:

var products = _ctx.Products
                   .AsSplitQuery()
                   .Include(p => p.ProductGroup) // non-collection
                   .Include(p => p.Prices) // collection
                          .ThenInclude(price => price.PriceType) // new, non-collection
                   .ToList();

In this case, I expect the PriceType to be loaded with the 2nd SQL statement, i.e. along with the Prices.

Expected SQL statements would be:
(I replaced column names with * for brevity)

-- 1st statement loads Products and ProductGroups only, i.e. current behavior works well as is
SELECT [p].*, [p0].*
FROM [Products] AS [p]
INNER JOIN [ProductGroups] AS [p0] ON [p].[ProductGroupId] = [p0].[Id]
ORDER BY [p].[Id], [p0].[Id]

-- 2nd statement loads Prices and PriceTypes only
SELECT [p1].*, [pt].*, [p].[Id]
FROM [Products] AS [p]
INNER JOIN [Prices] AS [p1] ON [p].[Id] = [p1].[ProductId]
INNER JOIN [PriceTypes] AS [pt] ON [p1].[PriceTypeId] = [pt].[Id]
ORDER BY [p].[Id]

For the 2nd use case we need another entity PriceType which is being referenced by a Price.

Entity types

public class Price
{
   public int Id { get; set; }
   public int ProductId { get; set; }
   public decimal Value { get; set; }

   public Guid PriceTypeId { get; set; }    // new
   public PriceType PriceType { get; set; } // new
}

public class PriceType
{
   public int Id { get; set; }
}
Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.