You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Group By combined with Where Exists generates inefficient SQL
After upgrading to .net 6 and ef core 6 one of our queries started breaking. The query contained a group by and a where exists clause and on examination we found that essentially the entire query was being reproduced as a sub query just to apply an aggregate function to the group. The query failed because one of the where clauses was referencing a column that was not included in the group by which I couldn't reproduce as the query that broke was very complicated but fixing the inefficient sql generation would fix the breakage anyway. In our specific case we managed to work around the issue by replacing the "where exists" with a join but since this does not necessarily result in failure but rather inefficient sql I thought it was worth raising.
I have attached a complete project reproducing the issue but it comes down to the fact that this code: GroupByIssue.zip
SELECT
[b].[BlogId]
,(SELECTMIN([b0].[Url])
FROM [Blogs] AS [b0]
WHERE (EXISTS (SELECT1FROM [Posts] AS [p0]
WHERE [p0].[BlogId] = [b0].[BlogId])
AND ([b0].[BlogId] >0))
AND ([b].[BlogId] = [b0].[BlogId]))
AS [Url]
FROM [Blogs] AS [b]
WHERE EXISTS (SELECT1FROM [Posts] AS [p]
WHERE [p].[BlogId] = [b].[BlogId])
AND ([b].[BlogId] >0)
GROUP BY [b].[BlogId]
But I would expect it to produce this sql
SELECT
[b].[BlogId]
,MIN([b].[Url]) AS [Url]
FROM [Blogs] AS [b]
WHERE EXISTS (SELECT1FROM [Posts] AS [p]
WHERE [p].[BlogId] = [b].[BlogId])
AND ([b].[BlogId] >0)
GROUP BY [b].[BlogId]
Provider and version information
EF Core version: 6.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows
IDE: Visual Studio Code
The text was updated successfully, but these errors were encountered:
Group By combined with Where Exists generates inefficient SQL
After upgrading to .net 6 and ef core 6 one of our queries started breaking. The query contained a group by and a where exists clause and on examination we found that essentially the entire query was being reproduced as a sub query just to apply an aggregate function to the group. The query failed because one of the where clauses was referencing a column that was not included in the group by which I couldn't reproduce as the query that broke was very complicated but fixing the inefficient sql generation would fix the breakage anyway. In our specific case we managed to work around the issue by replacing the "where exists" with a join but since this does not necessarily result in failure but rather inefficient sql I thought it was worth raising.
I have attached a complete project reproducing the issue but it comes down to the fact that this code:
GroupByIssue.zip
generates this sql
But I would expect it to produce this sql
Provider and version information
EF Core version: 6.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows
IDE: Visual Studio Code
The text was updated successfully, but these errors were encountered: