Remove redundant join for table splitting
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 38/100
Research direction
Start with the table-splitting model and mapping in the reproduction, then compare the SQL generated for the queries with and without Include. Trace the table-splitting include query path and determine whether the self-join can be omitted without losing the cancellation fields. Done means the include query loads the mapped columns without the redundant self-join and preserves the expected results.
Written by the indexing model from the issue text.
Description
When using table splitting, EF Core is joining the table with itself to get the second entity field. It seems that was not the case in EF6 (see Select scenario, test C).
The objective is not to load all the columns of a table each time, but group them by functionality and load them when they are going to be used.
Am I doing anything wrong? Is there anything I can do better?
Steps to reproduce
Model
public class PurchaseOrder
{
public int Id { get; set; }
public string Status { get; set; }
public PurchaseOrderCancellation Cancellation { get; set; }
}
public class PurchaseOrderCancellation
{
public string CancelReason { get; set; }
public DateTime? CancelDate { get; set; }
}
Mapping
class PurchaseOrderConfig : IEntityTypeConfiguration<PurchaseOrder>
{
public void Configure(EntityTypeBuilder<PurchaseOrder> builder)
{
builder.ToTable("purchase_order");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).HasColumnName("id").ForSqlServerUseSequenceHiLo();
builder.Property(x => x.Status).HasColumnName("status");
builder.HasOne(x => x.Cancellation).WithOne().HasForeignKey<PurchaseOrder>(x => x.Id);
}
}
class PurchaseOrderCancellationConfig : IEntityTypeConfiguration<PurchaseOrderCancellation>
{
public void Configure(EntityTypeBuilder<PurchaseOrderCancellation> builder)
{
builder.ToTable("purchase_order");
builder.Property(x => x.CancelReason).HasColumnName("cancel_reason").HasMaxLength(500);
builder.Property(x => x.CancelDate).HasColumnName("cancel_date").HasColumnType("datetime2");
builder.Property<int>("Id").HasColumnName("id").ForSqlServerUseSequenceHiLo();
builder.HasKey("Id");
}
}
Queries:
Without include
var query1 = ctx.Set<PurchaseOrder>()
.Where(x => x.Id == 1)
.ToArray();
SELECT [x].[id], [x].[status]
FROM [purchase_order] AS [x]
WHERE [x].[id] = 1
With include
var query2 = ctx.Set<PurchaseOrder>()
.Include(x => x.Cancellation)
.Where(x => x.Id == 1);
SELECT [x].[id], [x].[status], [x.Cancellation].[id], [x.Cancellation].[cancel_date], [x.Cancellation].[cancel_reason]
FROM [purchase_order] AS [x]
INNER JOIN [purchase_order] AS [x.Cancellation] ON [x].[id] = [x.Cancellation].[id]
WHERE [x].[id] = 1
Further technical details
EF Core version: 2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: VS2017 15.3
- 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