Skip to content

Commit 9fb89ed

Browse files
committed
Document split query ordering consistency for EF10
See dotnet/efcore#26808
1 parent 800624b commit 9fb89ed

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

  • entity-framework/core/what-is-new/ef-core-10.0

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
@@ -273,6 +273,53 @@ var query = context.Students
273273
274274
See [#12793](https://github.com/dotnet/efcore/issues/12793) and [#35367](https://github.com/dotnet/efcore/issues/35367) for more details.
275275

276+
### More consistent ordering for split queries
277+
278+
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.
279+
280+
For example, consider the following query:
281+
282+
```C#
283+
var blogs = await context.Blogs
284+
.AsSplitQuery()
285+
.Include(b => b.Posts)
286+
.OrderBy(b => b.Name)
287+
.Take(2)
288+
.ToListAsync();
289+
```
290+
291+
Prior to EF10, the following two SQL queries were generated:
292+
293+
```sql
294+
SELECT TOP(@__p_0) [b].[Id], [b].[Name]
295+
FROM [Blogs] AS [b]
296+
ORDER BY [b].[Name], [b].[Id]
297+
298+
SELECT [p].[Id], [p].[BlogId], [p].[Title], [b0].[Id]
299+
FROM (
300+
SELECT TOP(@__p_0) [b].[Id], [b].[Name]
301+
FROM [Blogs] AS [b]
302+
ORDER BY [b].[Name]
303+
) AS [b0]
304+
INNER JOIN [Post] AS [p] ON [b0].[Id] = [p].[BlogId]
305+
ORDER BY [b0].[Name], [b0].[Id]
306+
```
307+
308+
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.
309+
310+
EF10 fixes this by ensuring that the ordering is consistent across the queries:
311+
312+
```sql
313+
SELECT [p].[Id], [p].[BlogId], [p].[Title], [b0].[Id]
314+
FROM (
315+
SELECT TOP(@p) [b].[Id], [b].[Name]
316+
FROM [Blogs] AS [b]
317+
ORDER BY [b].[Name], [b].[Id]
318+
) AS [b0]
319+
INNER JOIN [Post] AS [p] ON [b0].[Id] = [p].[BlogId]
320+
ORDER BY [b0].[Name], [b0].[Id]
321+
```
322+
276323
<a name="other-query-improvements"></a>
277324

278325
### Other query improvements

0 commit comments

Comments
 (0)