Skip to content

Better translation for COUNT with predicate checking for non-null #26940

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
Tracked by #30173
roji opened this issue Dec 8, 2021 · 0 comments
Open
Tracked by #30173

Better translation for COUNT with predicate checking for non-null #26940

roji opened this issue Dec 8, 2021 · 0 comments

Comments

@roji
Copy link
Member

roji commented Dec 8, 2021

We currently translate the following LINQ query:

_ = ctx.Blogs
    .GroupBy(b => b.Id)
    .Select(g => new { g.Key, Count = g.Select(x => x.Name).Count(n => n != null) })
    .ToList();

... to:

SELECT [b].[Id] AS [Key], COUNT(CASE
    WHEN [b].[Name] IS NOT NULL THEN 1
END) AS [Count]
FROM [Blogs] AS [b]
GROUP BY [b].[Id]

... when we could just translate to:

SELECT [b].[Id] AS [Key], COUNT([b].[Name]) AS [Count]
FROM [Blogs] AS [b]
GROUP BY [b].[Id]

... since COUNT(x) only counts non-null values. This may have a perf impact (index usage).

Related to #26938

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants