Skip to content

Commit d0eb41a

Browse files
committed
Document split query ordering consistency for EF10 (#5091)
See dotnet/efcore#26808
1 parent 7e64f1f commit d0eb41a

File tree

1 file changed

+47
-0
lines changed
  • entity-framework/core/what-is-new/ef-core-10.0

1 file changed

+47
-0
lines changed

entity-framework/core/what-is-new/ef-core-10.0/whatsnew.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,53 @@ Unfortunately, C# query syntax (`from x select x.Id`) doesn't yet support expres
334334

335335
See [#12793](https://github.com/dotnet/efcore/issues/12793) and [#35367](https://github.com/dotnet/efcore/issues/35367) for more details.
336336

337+
### More consistent ordering for split queries
338+
339+
Split queries can be essential to avoid performance issues associated with JOINs, such as the so-called "cartesian explosion" effect (see [Single vs. Split Queries](xref:core/querying/single-split-queries) to learn more). However, since split queries loads related data in separate SQL queries, consistency issues can arise, possibly leading to non-deterministic, hard-to-detect data corruption.
340+
341+
For example, consider the following query:
342+
343+
```C#
344+
var blogs = await context.Blogs
345+
.AsSplitQuery()
346+
.Include(b => b.Posts)
347+
.OrderBy(b => b.Name)
348+
.Take(2)
349+
.ToListAsync();
350+
```
351+
352+
Prior to EF10, the following two SQL queries were generated:
353+
354+
```sql
355+
SELECT TOP(@__p_0) [b].[Id], [b].[Name]
356+
FROM [Blogs] AS [b]
357+
ORDER BY [b].[Name], [b].[Id]
358+
359+
SELECT [p].[Id], [p].[BlogId], [p].[Title], [b0].[Id]
360+
FROM (
361+
SELECT TOP(@__p_0) [b].[Id], [b].[Name]
362+
FROM [Blogs] AS [b]
363+
ORDER BY [b].[Name]
364+
) AS [b0]
365+
INNER JOIN [Post] AS [p] ON [b0].[Id] = [p].[BlogId]
366+
ORDER BY [b0].[Name], [b0].[Id]
367+
```
368+
369+
Note that the first query (the one reading Blogs) is integrated as a subquery within the second (the one reading Posts). However, the ordering within the subquery omits the `Id` column, leading to possible incorrect data being returned.
370+
371+
EF10 fixes this by ensuring that the ordering is consistent across the queries:
372+
373+
```sql
374+
SELECT [p].[Id], [p].[BlogId], [p].[Title], [b0].[Id]
375+
FROM (
376+
SELECT TOP(@p) [b].[Id], [b].[Name]
377+
FROM [Blogs] AS [b]
378+
ORDER BY [b].[Name], [b].[Id]
379+
) AS [b0]
380+
INNER JOIN [Post] AS [p] ON [b0].[Id] = [p].[BlogId]
381+
ORDER BY [b0].[Name], [b0].[Id]
382+
```
383+
337384
<a name="other-query-improvements"></a>
338385

339386
### Other query improvements

0 commit comments

Comments
 (0)