Skip to content

Query: Left Join (GroupJoin) always materializes elements resulting in unnecessary data pulling #6647

Closed

Description

@qmicron

Steps to reproduce

public class Budget
{
    public Guid Id { get; set; }
    public Guid? DescriptionTextId { get; set; }
    public decimal PostCost { get; set; }
    public string PostNr { get; set; }
    public decimal PostPrice { get; set; }
    public decimal Quantity { get; set; }
    public decimal UnitCost { get; set; }
    public string UnitName { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal VatAmount { get; set; }
    public decimal VatBasis { get; set; }
    public decimal VatFreeAmount { get; set; }
    public decimal VatFreePercent { get; set; }
    public decimal VatIncluded { get; set; }
    public decimal VatRate { get; set; }

    public Text DescriptionText { get; set; }
}

public class Text
{
    public Guid Id { get; set; }
    public string Plain { get; set; }
    public string Rtf { get; set; }

    public ICollection<Budget> BudgetDescriptionText { get; set; } = new List<Budget>();
}

public class DataContext : DbContext
{
    public DataContext(DbContextOptions options)
        : base(options)
    {
    }

    public DbSet<Budget> Budget => Set<Budget>();
    public DbSet<Text> Text => Set<Text>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Budget>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Id).ValueGeneratedNever();
            entity.Property(e => e.DescriptionTextId);
            entity.Property(e => e.PostCost);
            entity.Property(e => e.PostNr).IsRequired().HasMaxLength(1000);
            entity.Property(e => e.PostPrice);
            entity.Property(e => e.Quantity);
            entity.Property(e => e.UnitCost);
            entity.Property(e => e.UnitName).HasMaxLength(100);
            entity.Property(e => e.UnitPrice);
            entity.Property(e => e.VatAmount);
            entity.Property(e => e.VatBasis);
            entity.Property(e => e.VatFreeAmount);
            entity.Property(e => e.VatFreePercent);
            entity.Property(e => e.VatIncluded);
            entity.Property(e => e.VatRate);
            entity.HasOne(e => e.DescriptionText)
                .WithMany(e => e.BudgetDescriptionText).HasForeignKey(e => e.DescriptionTextId);
        });

        modelBuilder.Entity<Text>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Id).ValueGeneratedNever();
            entity.Property(e => e.Plain);
            entity.Property(e => e.Rtf);
        });
    }
}

The issue

We expect that the following query returns values for two columns

var result = context.Budget
    .Select(p => new
    {
        p.PostCost,
        p.DescriptionText.Plain
    })
    .ToArray();

but in reality, EF generates SQL query that returns all columns.

SELECT 
    [p].[Id], 
    [p].[DescriptionTextId], 
    [p].[PostCost], 
    [p].[PostNr], 
    [p].[PostPrice], 
    [p].[Quantity], 
    [p].[UnitCost], 
    [p].[UnitName], 
    [p].[UnitPrice], 
    [p].[VatAmount], 
    [p].[VatBasis], 
    [p].[VatFreeAmount], 
    [p].[VatFreePercent], 
    [p].[VatIncluded], 
    [p].[VatRate], 
    [p.DescriptionText].[Id], 
    [p.DescriptionText].[Plain], 
    [p.DescriptionText].[Rtf]
FROM 
    [Budget] AS [p]
    LEFT JOIN [Text] AS [p.DescriptionText] ON [p].[DescriptionTextId] = [p.DescriptionText].[Id]
ORDER BY 
    [p].[DescriptionTextId]

Further technical details

EF Core version: 1.0.1
Operating system: Windows 10
Visual Studio version: VS 2015

Activity

  1. smitpatel commented on Sep 30, 2016

    @smitpatel
    Contributor

    This is due to requiring materialization of both the sides in group join.
    DescriptionText is optional navigation hence it will generate a group join query. In group join query changes in the outer elements need to be recorded (bug #6360). This was fixed in #6387 by pulling all columns to figure out the changes in outer element.

  2. added this to the 1.1.0 milestone on Oct 3, 2016
  3. rowanmiller commented on Oct 3, 2016

    @rowanmiller
    Contributor

    @maumar to check if we already have work items covering this

  4. maumar commented on Oct 5, 2016

    @maumar
    Contributor

    Another commit related to this issue: e514b49 (fix to #4858)

  5. changed the title [-]Left Join causes unnecessary data pulling[/-] [+]Left Join (GroupJoin) always materializes elements resulting in unnecessary data pulling[/+] on Oct 5, 2016
  6. removed their assignment
    on Oct 5, 2016
  7. modified the milestones: , 1.1.0 on Oct 6, 2016
  8. tuespetre commented on Feb 3, 2017

    @tuespetre
    Contributor

    #7543 covers this.

  9. added a commit that references this issue on Feb 6, 2017
    a3b2caa
  10. 18 remaining items

  11. added 3 commits that reference this issue on Mar 11, 2017
  12. assigned and unassigned on Mar 15, 2017
  13. maumar commented on Mar 15, 2017

    @maumar
    Contributor

    Fixed in 03a990c

  14. changed the title [-]Left Join (GroupJoin) always materializes elements resulting in unnecessary data pulling[/-] [+]Query: Left Join (GroupJoin) always materializes elements resulting in unnecessary data pulling[/+] on May 9, 2017
  15. modified the milestones: , 2.0.0 on Oct 15, 2022
  16. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions