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: Optimize collection join for coalesce cases #16897

Open
smitpatel opened this issue Aug 1, 2019 · 0 comments
Open

Query: Optimize collection join for coalesce cases #16897

smitpatel opened this issue Aug 1, 2019 · 0 comments

Comments

@smitpatel
Copy link
Contributor

                var query = from g1 in context.Gears.Include(g => g.Weapons)
                            join g2 in context.Gears
                                on g1.LeaderNickname equals g2.Nickname into grouping
                            from g2 in grouping.DefaultIfEmpty()
                            select g2 ?? g1;

                var result = query.ToList();

Generates following SQL

SELECT [t].[Nickname], [t].[SquadId], [t].[AssignedCityName], [t].[CityOrBirthName], [t].[Discriminator], [t].[FullName], [t].[HasSoulPatch], [t].[LeaderNickname], [t].[LeaderSquadId], [t].[Rank], [g0].[Nickname], [g0].[SquadId], [g0].[AssignedCityName], [g0].[CityOrBirthName], [g0].[Discriminator], [g0].[FullName], [g0].[HasSoulPatch], [g0].[LeaderNickname], [g0].[LeaderSquadId], [g0].[Rank], [w].[Id], [w].[AmmunitionType], [w].[IsAutomatic], [w].[Name], [w].[OwnerFullName], [w].[SynergyWithId]
FROM [Gears] AS [g0]
LEFT JOIN (
    SELECT [g].[Nickname], [g].[SquadId], [g].[AssignedCityName], [g].[CityOrBirthName], [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 [g0].[LeaderNickname] = [t].[Nickname]
LEFT JOIN [Weapons] AS [w] ON [g0].[FullName] = [w].[OwnerFullName]
WHERE [g0].[Discriminator] IN (N'Gear', N'Officer')
ORDER BY [g0].[Nickname], [g0].[SquadId], [w].[Id]

Left Join on Weapons could be restricted so that it only applies when g1 is selected in LINQ.
Currently it generate SQL as if it applies include on both though generate correct result (i.e. including on client only when particular gear is selected.

smitpatel added a commit that referenced this issue Aug 1, 2019
Fixed in new nav expansion
Filed #16897 for further improvement in SQL
smitpatel added a commit that referenced this issue Aug 1, 2019
Fixed in new nav expansion
Filed #16897 for further improvement in SQL
smitpatel added a commit that referenced this issue Aug 2, 2019
Fixed in new nav expansion
Filed #16897 for further improvement in SQL
@ajcvickers ajcvickers added this to the Backlog milestone Aug 5, 2019
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

2 participants