Skip to content

[release/10.0] Fix split query paging order when limit/offset is pushed into a subquery - #39865

Open
AndriySvyryd wants to merge 2 commits into
release/10.0from
andriysvyryd-backport-39687-split-query-paging
Open

AndriySvyryd wants to merge 2 commits into
release/10.0from
andriysvyryd-backport-39687-split-query-paging

Conversation

@AndriySvyryd

@AndriySvyryd AndriySvyryd commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #39206
Backports #39687

Description
In EF Core 10 (#34097), split queries with Skip/Take and a non-unique OrderBy were made deterministic by appending key orderings. However, when the query also joins a reference navigation (in a projection or via Include), EF pushes the paging into a subquery before those key orderings are added, so they only reach the outer query. The subquery that selects the page is still ordered only by the non-unique column, so the main query and the split (collection) queries can select different rows.

Customer impact
Child collections can silently come back empty (or incomplete) with no error for the above case, possibly resulting in data corruption:

db.Parents.AsSplitQuery()
    .Select(p => new { p.Name, LocationName = p.Location.Name, ChildIds = p.Children.Select(c => c.Id) })
    .OrderBy(x => x.Name) // not unique
    .Skip(0).Take(10);

Workaround: add a unique column (e.g. the key) to the ordering, e.g. .OrderBy(x => x.Name).ThenBy(x => x.Id). This is what was recommended before EF 10, but the EF 10 docs suggest it's no longer necessary, so it's not easily discoverable.

How found
4 customer reports on 10.0.x/11.0.x

Regression
No. The automatic key ordering for split queries was introduced in 10.0.0, but this case was missed.

Testing
2 new tests.

Risk
Low. Only affects split queries whose paging was pushed into a subquery. Quirk added (Microsoft.EntityFrameworkCore.Issue39206).

@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner December 1, 2026 00:02
Copilot AI balanced review requested due to automatic review settings December 1, 2026 00:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The paging fix still misses subqueries behind unpaged DISTINCT wrappers, risking mismatched parents and omitted children, and the new tests bypass SQL Server configuration-based skipping.

2 open issues
What changed in this PR

Backports #39687 to EF Core 10.0 to address #39206: split queries can return missing child collections when paging with non-unique ordering is pushed into a subquery.

Changes:

  • Adds identifier orderings to paged subqueries in both main and collection queries.
  • Adds an AppContext switch to restore the previous behavior.
  • Adds projection and Include regression tests and updates SQL Server baselines.
File Description
test/​EFCore.SqlServer.FunctionalTests/​Query/​NorthwindSplitIncludeQuerySqlServerTest.cs Updates tracking split-query baselines with key ordering inside APPLY.
test/​EFCore.SqlServer.FunctionalTests/​Query/​NorthwindSplitIncludeNoTrackingQuerySqlServerTest.cs Updates equivalent no-tracking baselines.
test/​EFCore.SqlServer.FunctionalTests/​Query/​ComplexNavigationsCollectionsSplitQuerySqlServerTest.cs Updates paged subquery baselines with identifier orderings.
test/​EFCore.SqlServer.FunctionalTests/​Query/​AdHocQuerySplittingQuerySqlServerTest.cs Adds SQL baselines for both regression scenarios.
test/​EFCore.Relational.Specification.Tests/​Query/​AdHocQuerySplittingQueryTestBase.cs Adds sync/async tests for projection and reference Include scenarios.
src/​EFCore.Relational/​Query/​SqlExpressions/​SelectExpression.cs Propagates identifier ordering into paged subqueries, guarded by a compatibility switch.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 1, 2026 02:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the split-query paging fix.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@artl93 artl93 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New-ish feature, 4 reports from users. Approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants