Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query :: we could be smarter about removing redundant joins when expanding multiple navigations #3816

Closed
maumar opened this issue Nov 20, 2015 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented Nov 20, 2015

Repro:

    public class Contact
    {
        public int Id { get; set; }
        public int CustomerID { get; set; }
        public virtual Customer Customer { get; set; }
    }

    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Contact> Contacts { get; set; }
        public virtual ICollection<Payment> Payments { get; set; }
    }

    public class Payment
    {
        public int Id { get; set; }
        public int CustomerID { get; set; }
        public virtual Customer Customer { get; set; }

        public decimal PaymentAmount { get; set; }
    }

    public class MyContext : DbContext
    {
        public DbSet<Contact> Contacts { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Payment> Payments { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Customer>().HasMany(e => e.Contacts).WithOne(e => e.Customer).HasForeignKey(e => e.CustomerID);
            modelBuilder.Entity<Customer>().HasMany(e => e.Payments).WithOne(e => e.Customer).HasForeignKey(e => e.CustomerID);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(new SqlConnectionStringBuilder { DataSource = ".", InitialCatalog = "Repro3791", MultipleActiveResultSets = true, IntegratedSecurity = true }.ToString());
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var ctx = new MyContext())
            {
                ctx.Database.EnsureDeleted();
                ctx.Database.EnsureCreated();
            }

            using (var ctx = new MyContext())
            {
                var list5 = ctx.Contacts.Select(x => new
                {
                    Customer = x.Customer.Name,
                    PaymentTotal = x.Customer.Payments.Sum(p => p.PaymentAmount)
                }).ToList();
            }
        }
    }

Generated SQL:

SELECT (
    SELECT SUM([p].[PaymentAmount])
    FROM [Payment] AS [p]
    WHERE [x.Customer0].[Id] = [p].[CustomerID]
), [x.Customer].[Name]
FROM [Contact] AS [x]
INNER JOIN [Customer] AS [x.Customer] ON [x].[CustomerID] = [x.Customer].[Id]
INNER JOIN [Customer] AS [x.Customer0] ON [x].[CustomerID] = [x.Customer0].[Id]

[x.Customer] and [x.Customer0] are the same thing - ideally we could detect this and prune second one (and re-wire all the references to point to the first)

@maumar maumar changed the title Query :: we could be smarted about removing redundant joins when expanding multiple navigations Query :: we could be smarted about removing redundant joins when expanding multiple navigationr Nov 20, 2015
@maumar maumar changed the title Query :: we could be smarted about removing redundant joins when expanding multiple navigationr Query :: we could be smarter about removing redundant joins when expanding multiple navigations Nov 20, 2015
@rowanmiller rowanmiller added this to the 7.0.0 milestone Nov 20, 2015
@rowanmiller rowanmiller modified the milestones: 1.0.1, 1.0.0 May 4, 2016
@rowanmiller rowanmiller removed the pri0 label Jul 6, 2016
@anpete
Copy link
Contributor

anpete commented Oct 3, 2016

@maumar Can we close this now?

@maumar
Copy link
Contributor Author

maumar commented Oct 3, 2016

@anpete yes, this is now fixed. SQL that we generate now is as follows:

SELECT [x.Customer].[Name], (
    SELECT SUM([p0].[PaymentAmount])
    FROM [Payments] AS [p0]
    WHERE [x.Customer].[Id] = [p0].[CustomerID]
)
FROM [Contacts] AS [x]
INNER JOIN [Customers] AS [x.Customer] ON [x].[CustomerID] = [x.Customer].[Id]

@maumar maumar closed this as completed Oct 3, 2016
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 3, 2016
@maumar
Copy link
Contributor Author

maumar commented Oct 3, 2016

most likely fixed in 9897064

@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants