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: Include on derived entity with composite key produced duplicated Discriminator column predicates #11050

Closed
maumar opened this issue Feb 23, 2018 · 6 comments
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented Feb 23, 2018

Introduced by fix to #9395 - before the join predicates on derived types were not sargable. However after the fix, SQL we generate for derived entities with composite keys has some redundant terms.

We should add optimization to remove the duplicated terms if all expressions are connected with AndAlso operator.

@maumar maumar changed the title Query: join on derived entity with composite key produced duplicated Discriminator column predicates Query: Include on derived entity with composite key produced duplicated Discriminator column predicates Feb 23, 2018
@maumar
Copy link
Contributor Author

maumar commented Feb 23, 2018

Example:

Include_reference_on_derived_type_using_lambda_with_tracking produces:

SELECT [l].[Name], [l].[Discriminator], [l].[LocustHordeId], [l].[ThreatLevel], [l].[DefeatedByNickname], [l].[DefeatedBySquadId], [l].[HighCommandId], [t].[Nickname], [t].[SquadId], [t].[AssignedCityName], [t].[CityOrBirthName], [t].[Discriminator], [t].[FullName], [t].[HasSoulPatch], [t].[LeaderNickname], [t].[LeaderSquadId], [t].[Rank]
FROM [LocustLeaders] AS [l]
LEFT JOIN (
    SELECT [l.DefeatedBy].*
    FROM [Gears] AS [l.DefeatedBy]
    WHERE [l.DefeatedBy].[Discriminator] IN (N'Officer', N'Gear')
) AS [t] ON (([l].[Discriminator] = N'LocustCommander') AND ([l].[DefeatedByNickname] = [t].[Nickname])) AND (([l].[Discriminator] = N'LocustCommander') AND ([l].[DefeatedBySquadId] = [t].[SquadId]))
WHERE [l].[Discriminator] IN (N'LocustCommander', N'LocustLeader')

@DibyodyutiMondal
Copy link

Is there any way to remove the dicriminator predicate for a query?
For example, if I have a query that only involves properties of the base class, but not of the derived class, ef core still adds an extra where clause with an in. Is there any way to opt-out of this behaviour, individually, or globally?

@ajcvickers
Copy link
Contributor

@DibyodyutiMondal I don't believe there is currently a way to opt out of this behavior.

@smitpatel
Copy link
Contributor

Generated SQL in new pipeline

SELECT [l].[Name], [l].[Discriminator], [l].[LocustHordeId], [l].[ThreatLevel], [l].[ThreatLevelByte], [l].[ThreatLevelNullableByte], [l].[DefeatedByNickname], [l].[DefeatedBySquadId], [l].[HighCommandId], [t].[Nickname], [t].[SquadId], [t].[AssignedCityName], [t].[CityOfBirthName], [t].[Discriminator], [t].[FullName], [t].[HasSoulPatch], [t].[LeaderNickname], [t].[LeaderSquadId], [t].[Rank]
FROM [LocustLeaders] AS [l]
LEFT JOIN (
    SELECT [g].[Nickname], [g].[SquadId], [g].[AssignedCityName], [g].[CityOfBirthName], [g].[Discriminator], [g].[FullName], [g].[HasSoulPatch], [g].[LeaderNickname], [g].[LeaderSquadId], [g].[Rank]
    FROM [Gears] AS [g]
    WHERE [g].[Discriminator] IN (N'Gear', N'Officer')
) AS [t] ON ([l].[DefeatedByNickname] = [t].[Nickname]) AND ([l].[DefeatedBySquadId] = [t].[SquadId])
WHERE [l].[Discriminator] IN (N'LocustLeader', N'LocustCommander')

After discriminator mapping being complete

SELECT [l].[Name], [l].[Discriminator], [l].[LocustHordeId], [l].[ThreatLevel], [l].[ThreatLevelByte], [l].[ThreatLevelNullableByte], [l].[DefeatedByNickname], [l].[DefeatedBySquadId], [l].[HighCommandId], [g].[Nickname], [g].[SquadId], [g].[AssignedCityName], [g].[CityOfBirthName], [g].[Discriminator], [g].[FullName], [g].[HasSoulPatch], [g].[LeaderNickname], [g].[LeaderSquadId], [g].[Rank]
FROM [LocustLeaders] AS [l]
LEFT JOIN [Gears] AS [g] ON ([l].[DefeatedByNickname] = [g].[Nickname]) AND ([l].[DefeatedBySquadId] = [g].[SquadId])

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Mar 15, 2020
@smitpatel smitpatel self-assigned this Mar 15, 2020
@smitpatel smitpatel modified the milestones: Backlog, 3.1.0 Mar 15, 2020
@szszoke
Copy link

szszoke commented Jun 5, 2020

@ajcvickers

Are there any plans to optimize the discriminator predicate in cases where only the base class is queried?

I have a DbSet for an abstract base class and two more more specialized DbSets for classes that inherit the base class.

I have a boolean discriminator field that cannot be null.

The discriminator predicate can be omitted entirely for queries against the base DbSet but EF still generates this: IN (FALSE, TRUE).

@ajcvickers
Copy link
Contributor

@szszoke See #18106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

5 participants