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

Entity with OwnsOne property providing not optimal SQL #20763

Closed
iuridosanjos opened this issue Apr 27, 2020 · 5 comments
Closed

Entity with OwnsOne property providing not optimal SQL #20763

iuridosanjos opened this issue Apr 27, 2020 · 5 comments

Comments

@iuridosanjos
Copy link

iuridosanjos commented Apr 27, 2020

Database entities

public class Customer
{
	public int Id { get; set; }
	public string Name { get; set; }
	public Address Address { get; set; }
}

public class Address
{
	public string StreetAddress { get; set; }
	public string City { get; set; }
	public string State { get; set; }
	public string ZipCode { get; set; }
}

DbContext

public class MyContext : DbContext
{
	public DbSet<Customer> Customers { get; set; }

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

		modelBuilder.Entity<Customer>().OwnsOne(c => c.Address);
	}
}

Problem

When I load all the Costumers from the Database, it runs the following SQL:

SELECT [c].[Id], [c].[Name], [t].[Id], [t].[Address_City], [t].[Address_State], [t].[Address_StreetAddress], [t].[Address_ZipCode]
FROM [Customers] AS [c]
LEFT JOIN (
    SELECT [c0].[Id], [c0].[Address_City], [c0].[Address_State], [c0].[Address_StreetAddress], [c0].[Address_ZipCode]
    FROM [Customers] AS [c0]
    WHERE [c0].[Address_ZipCode] IS NOT NULL OR ([c0].[Address_StreetAddress] IS NOT NULL OR ([c0].[Address_State] IS NOT NULL OR [c0].[Address_City] IS NOT NULL))
) AS [t] ON [c].[Id] = [t].[Id]

Note the unneeded and undesired nested select just to get Address properties. I want EF Core to load all of Address properties as if Address could not be null (the internal properties could be null, but not Customer.Addresss), but OwnedNavigationBuilder<TEntity, TRelatedEntity> don't have anything like "IsRequired" (for the Customer.Address property). How can I do that?

The SQL should look like this:

SELECT [c].[Id], [c].[Name], [c].[Address_City], [c].[Address_State], [c].[Address_StreetAddress], [c].[Address_ZipCode]
FROM [Customers] AS [c]

That way, I wanted to always create and "new Address()" with those results.

Can I change anything in my model configuration to tell EF Core to behave like this?

Note

This case is just an example, but shows exactly the problem I'm having in my product. The basic difference is that I'm using the Owned class for multiple properties, same as if the shown example used like 5 to 10 properties of that type (not collection). The SQL that EF Core is executing is not good and it's performancing poorly than we expected it should.

Technical Information

EF Core Version tested here: 3.1.3
SQL Server Version: SQL Server 2019
Using .NET Core 3.1

@iuridosanjos iuridosanjos changed the title Entity with OwnsOne property providing weird SQL Entity with OwnsOne property providing not optimal SQL Apr 27, 2020
@ajcvickers
Copy link
Member

@iuridosanjos This looks like a duplicate of #18299, which has been fixed in EF Core 5.0 preview 3. Can you try preview 3 and report back if it does not fix the issues you are seeing?

@iuridosanjos
Copy link
Author

@ajcvickers I've tested on the EF Core 5.0 preview 3 you've said and it's fixed. The SQL is not making unnecessary JOINs, the result of the above example is only one simple SELECT.

Thanks.

@dr130873
Copy link

Any plans of putting this fix on EF core 3.1

@ajcvickers
Copy link
Member

@dr130873 As discussed on #18299, it is not feasible to port this change to 3.1.

@MikeStonoga
Copy link

@iuridosanjos This looks like a duplicate of #18299, which has been fixed in EF Core 5.0 preview 3. Can you try preview 3 and report back if it does not fix the issues you are seeing?

That works for me!
Thanks a lot!
A simple query downgrade from 4s to 0.25s

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants