AsSplitQuery: non-collection-Includes are repeated in all SQL statements
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 28/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100