EF10 Upgrade cause Regression in query times due to linq-translations
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 52/100
Research direction
Start by reproducing the supplied LINQ projection with EF Core 10.0.11 and compare its generated SQL and query time with EF9, recording the database provider and target framework that are currently missing. Trace the query translation entry point responsible for the window-function versus OUTER APPLY shapes; done means the regression is explained and covered by a focused test or a confirmed issue with a reliable reproduction.
Written by the indexing model from the issue text.
Description
Bug description
After upgrading to EF10 from EF9 we saw some significant regression in query performance and found it to be caused by the way EF10 translate Linq queries to SQL.
var query = db.Events
.Select(event => new Response
{
// Adding the below forces EF10 to use APPLY in its translation
// When not present, the below SQL is generated instead, which takes performance
// From approximately 100ms to 11 seconds.
Id = event.Id,
LatestComment = event.Comments
.Where(comment => comment.IsVisible)
.OrderByDescending(comment => comment.CreatedDate)
.Take(1)
.Select(comment => new Comment
{
comment.CreatedDate,
comment.Text
})
.FirstOrDefault()
});
The slow SQL translation:
SELECT [w].[Id], [w2].[CreatedDate], [w2].[Text], [w2].[c]
FROM [Events] AS [w]
LEFT JOIN (
SELECT
[w1].[CreatedDate],
[w1].[Text],
[w1].[c],
[w1].[Id]
FROM (
SELECT
[w0].[CreatedDate],
[w0].[Text],
1 AS [c],
[w0].[Id],
ROW_NUMBER() OVER (
PARTITION BY [w0].[Id]
ORDER BY [w0].[CreatedDate] DESC
) AS [row]
FROM [Events] AS [w0]
WHERE [w0].[HasComment] = CAST(1 AS bit)
) AS [w1]
WHERE [w1].[row] <= 1
) AS [w2]
ON [w].[Id] = [w2].[Id]
The "forced" SQL translation (Adding the event.Id to the selector):
SELECT [w].[Id],
[w1].[CorrelationId],
[w1].[CreatedDate],
[w1].[Text],
[w1].[c]
FROM [Events] AS [w]
OUTER APPLY (
SELECT TOP(1)
[w].[Id] AS [CorrelationId],
[w0].[CreatedDate],
[w0].[Text],
1 AS [c]
FROM [Events] AS [w0]
WHERE [w0].[Id] = [w].[Id]
AND [w0].[HasComment] = CAST(1 AS bit)
ORDER BY [w0].[CreatedDate] DESC
) AS [w1]
I am currently not sure if I have been doing it wrong and need to adapt to another way of projecting my selections to get an ideal translation, or if this is a bug in EF10, as EF9 generates a very efficient SQL query.
Your code
var query = db.Events
.Select(event => new Response
{
// Adding the below forces EF10 to use APPLY in its translation
// When not present, the below SQL is generated instead, which takes performance
// From approximately 100ms to 11 seconds.
Id = event.Id,
LatestComment = event.Comments
.Where(comment => comment.IsVisible)
.OrderByDescending(comment => comment.CreatedDate)
.Take(1)
.Select(comment => new Comment
{
comment.CreatedDate,
comment.Text
})
.FirstOrDefault()
});
Stack traces
Verbose output
EF Core version
10.0.11
Database provider
No response
Target framework
No response
Operating system
No response
IDE
No response
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.5k
- Avg merge
- 3d 7h
- Merged PRs (30d)
- 182
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
customer-reported
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
GroupBy element `OrderBy(...).First()`/`MaxBy` over an owned type mapped to its own table fails to translatePossibly taken @cincuranet claimed this today. Openarea-groupby area-query
Difficulty 4/5 3-5 days Newbie friendliness 25/100
dotnet/efcore#39833 · 1 assignee ·
Maintainers usually reply within 2 days
-
SQLite: Polygon.Length translates to GLength, which returns 0Possibly taken @cincuranet claimed this today. Openarea-query area-sqlite
Difficulty 3/5 1-2 days Newbie friendliness 45/100
dotnet/efcore#39826 · 1 assignee ·
Maintainers usually reply within 2 days
-
Migration lock with LockReleaseBehavior.Transaction doesn't prevent concurrent migrations since per-migration transactionsPossibly taken @AndriySvyryd claimed this 1 day ago. Openarea-migrations
dotnet/efcore#39824 · 1 assignee ·
Maintainers usually reply within 2 days
-
Querying a TPC base type with a JSON complex property throwsPossibly taken @cincuranet claimed this 1 day ago. Openarea-query
Difficulty 4/5 3-5 days Newbie friendliness 22/100
dotnet/efcore#39814 · 1 assignee ·
Maintainers usually reply within 2 days
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
SubtitleEdit/subtitleedit#17247 ·
Maintainers usually reply within 1 day
-
Restore test cleanup fails on Windows when restored files are read-onlyPossibly taken @JamBalaya56562 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
duplicati/duplicati#8046 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
Bundles Documentation
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pyrevitlabs/pyRevit#4285 ·
Maintainers usually reply within 1 day