efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Query performance issue combining Inheritance and OwnedTypes

Open orbus-dkreivenas opened this issue 5 years ago • 1 comments

Explanation

There is a bug in how EF generates a query if Inherited Type contain OwnedTypes or performing Include() on the sub type while at the same type trying to resolve a property in Select() based on the Type of SubType. This issue was found by executing an OData query against EF database context.

Code Sample

Configuration for the entities are in the sample project.

When running the following query, when HyperlinkAttributeValue and ChoiceAttributeValue both contains an OwnedEntity list:


database
  .Objects
  .Select(e => new
  {
      e.Id,
      AttributeValues = e.AttributeValues.Select(v => new
      {
          v.AttributeName,
          InstanceType =
              v is TextAttributeValue
                  ? "TextAttributeValue"
                  : v is HyperlinkAttributeValue
                      ? "HyperlinkAttributeValue"
                      : v is ChoiceAttributeValue
                          ? "ChoiceAttributeValue"
                          : "AttributeValue"
      })
})

Will generate a query similar to:

SELECT *
FROM (
    SELECT TOP(@__TypedProperty_3) [o].[Id], [o].[Name]
    FROM [Objects] AS [o]
    ORDER BY [o].[Id]
) AS [t]
OUTER APPLY (
    SELECT *
    FROM (
        SELECT TOP(@__TypedProperty_1) [a].[AttributeValueId], [a].[AttributeCategory], [a].[AttributeName], [a].[AttributeValueCategory], [a].[ObjectId], [a].[Value]
        FROM [AttributeValues] AS [a]
        WHERE [t].[Id] = [a].[ObjectId]
        ORDER BY [a].[AttributeValueId]
    ) AS [t0]
    LEFT JOIN [ChoiceValue] AS [c] ON [t0].[AttributeValueId] = [c].[AttributeValueId]
    LEFT JOIN [HyperlinkValue] AS [h] ON [t0].[AttributeValueId] = [h].[AttributeValueId]
    LEFT JOIN [ChoiceValue] AS [c0] ON [t0].[AttributeValueId] = [c0].[AttributeValueId]
    LEFT JOIN [HyperlinkValue] AS [h0] ON [t0].[AttributeValueId] = [h0].[AttributeValueId]
    LEFT JOIN [ChoiceValue] AS [c1] ON [t0].[AttributeValueId] = [c1].[AttributeValueId]
    LEFT JOIN [HyperlinkValue] AS [h1] ON [t0].[AttributeValueId] = [h1].[AttributeValueId]
) AS [t1]
ORDER BY ...

If we would add another subtype to the AttributeValue it would generate additional 2 Left Joins and select additional properties, if the SubType would contain and OwnedType it would add additional 6 Left Joins totalling to 12 Left Joins. As there would be 3 LEFT JOINS of OwendType for each SubType defined for AttributeValue. The query is not affected by how many case statements there are in the Select().

Changing the relationship from OwnedType to a basic relationship the query is resolved correctly by performing the correct resolution of InstanceType using the discriminator property. However once adding the Include to retrieve the related entities, e.g.:

Objects
.Include(e => e.AttributeValues)
   .ThenInclude(e => ((ChoiceAttributeValue)e).Values)
...

Will break the query as well.

Sample Project

Please see the sample project attached to replicate the issue.

EFQueryIssue.zip

Provider and version information

EF Core version: Found in 3.1.8 still replicated in 5.0.3 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .netcore3.1

orbus-dkreivenas avatar Feb 19 '21 13:02 orbus-dkreivenas

note: Issue happens because entities with owned types will cause includes in nav expansion which causes "is" operator to not translate to discriminator. Nav expansion should not introduce includes when "is" operator is used on entity type.

smitpatel avatar Feb 19 '21 19:02 smitpatel