Using owned types in inheritance hierarchies generates unnecessary joins
Posted: Thu 17 Oct 2019 12:49
Using Owned Entity Types as a property in an inheritance hierarchy generates unnecessary joins, reducing query performance.
Given this hierarchy:
Querying beast riders generates the following SQL:
As BEAST_NAME and BEAST_TYPE are both part of the BEAST table, this join is not necessary. The behaviour is the same when Beast is a property of Rider instead of BeastRider.
When using an owned entity type on a regular entity (not in an inheritance hierarchy), this join is not generated. This is demonstrated in the sample repository below with the "OtherBeastRider" entity. Querying this entity generates the following SQL:
A repository reproducing the issue can be found here: https://github.com/LBRitsSES/devart-efc ... -bug-repro
Given this hierarchy:
Code: Select all
public abstract class Rider
{
public long Id { get; private set; }
}
public class BeastRider : Rider
{
public Beast Beast { get; private set; }
public BeastRider(Beast beast) : base()
{
Beast = beast;
}
}
public class Beast : ValueObject
{
public string Name { get; private set; }
public EquineBeast Type { get; private set; }
public Beast(string name, EquineBeast type)
{
Name = name;
Type = type;
}
protected override IEnumerable<object> GetEqualityComponents()
{
yield return Name;
yield return Type;
}
}
Code: Select all
SELECT
"r".ID,
"r".DISCRIMINATOR,
"t".ID AS ID1,
"t".BEAST_NAME,
"t".BEAST_TYPE
FROM RIDER "r"
LEFT JOIN (
SELECT
"b.Beast".*
FROM RIDER "b.Beast"
WHERE "b.Beast".DISCRIMINATOR = 'BeastRider'
)
"t" ON "r".ID = "t".ID
WHERE "r".DISCRIMINATOR = 'BeastRider'
FETCH FIRST 1 ROWS ONLY
When using an owned entity type on a regular entity (not in an inheritance hierarchy), this join is not generated. This is demonstrated in the sample repository below with the "OtherBeastRider" entity. Querying this entity generates the following SQL:
Code: Select all
SELECT
"o".ID,
"o".ID AS ID1,
"o".BEAST_NAME,
"o".BEAST_TYPE
FROM OTHER_RIDER "o"
FETCH FIRST 1 ROWS ONLY
A repository reproducing the issue can be found here: https://github.com/LBRitsSES/devart-efc ... -bug-repro