An EF Core 10 project dashboard loads each project's work items and members in one query and has become slow; how do AsSplitQuery and QuerySplittingBehavior help, and what do they cost?
answer
- two sibling collections multiply rows
- MultipleCollectionIncludeWarning in the log
- AsSplitQuery per query, AsSingleQuery override
- UseQuerySplittingBehavior on the provider options
- extra round trips, no snapshot consistency
basics
~10 sLoading two sibling collections in one SQL statement multiplies rows per project. AsSplitQuery, or UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery) globally, sends one statement per collection instead, at the cost of extra round trips and no single-snapshot consistency.
solid answer
~50 sBy default EF Core uses `QuerySplittingBehavior.SingleQuery`: one SQL statement with a `LEFT JOIN` per collection. Two sibling collections such as `WorkItems` and `Members` produce items × members rows per project, and EF logs `MultipleCollectionIncludeWarning` when a query loads more than one collection, via `Include` or a projection, with no splitting behaviour configured. Adding `.AsSplitQuery()` makes EF send one statement for the projects and one per collection, joined back on the key, so rows add up instead of multiplying. I can make it the default with `UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)` in the provider options and opt single queries back in with `.AsSingleQuery()`. The costs: one round trip per collection; no consistency guarantee if data changes between the statements unless a snapshot or serializable transaction wraps them; and on most databases the earlier results are buffered before the next statement runs.
code
csharp · 10 linesvar cards = await db.Projects
.Where(p => p.IsActive)
.OrderBy(p => p.Name).ThenBy(p => p.Id)
.Select(p => new ProjectCard(
p.Id, p.Name,
p.WorkItems.Where(w => !w.IsDone)
.Select(w => new ItemRow(w.Id, w.Title)).ToList(),
p.Members.Select(m => m.DisplayName).ToList()))
.AsSplitQuery()
.ToListAsync(ct);go deeper
Recall that EF Core loads collections in one SQL statement by default and that AsSplitQuery() sends separate statements instead.
Explain why two sibling collections multiply rows, what MultipleCollectionIncludeWarning tells you, and how AsSplitQuery, AsSingleQuery and UseQuerySplittingBehavior relate.
Weigh round trips, buffering and consistency against row multiplication with measurements, and know the paging-order fix that EF Core 10 brought to split queries.
Set a team default for splitting and the rule for exceptions, considering database latency, transaction isolation and how reports demand consistency.
## Why the single query got slow EF Core's default **query splitting behaviour** is `QuerySplittingBehavior.SingleQuery`: related collections are loaded in the same SQL statement as their parent, with a `LEFT JOIN` per collection. For a project dashboard card: ```csharp var cards = await db.Projects .Where(p => p.IsActive) .OrderBy(p => p.Name) .Select(p => new ProjectCard( p.Id, p.Name, p.WorkItems.Where(w => !w.IsDone).Select(w => new ItemRow(w.Id, w.Title)).ToList(), p.Members.Select(m => m.DisplayName).ToList())) .ToListAsync(); ``` `WorkItems` and `Members` are **sibling collections** of `Project`. Joined in one statement, a project with 200 open items and 15 members returns 3,000 rows, each repeating the project columns. EF de-duplicates them while materializing, so the result is correct, but the database, the network and the materializer all pay for the multiplied rows. The ORM-neutral theory of that fan-out lives elsewhere; what matters here is how EF Core tells you about it and lets you change it. ## How EF Core warns you When a query loads **more than one collection navigation**, through `Include` *or* through a projection like the one above, and no splitting behaviour is configured either globally or on the query, EF Core logs `RelationalEventId.MultipleCollectionIncludeWarning`. The message says it is using `QuerySplittingBehavior.SingleQuery` by default and that this "can potentially result in slow query performance". To find the offending query in development you can promote that event to an exception with `ConfigureWarnings(w => w.Throw(RelationalEventId.MultipleCollectionIncludeWarning))`. ## The switch: per query or globally - **Per query:** add `.AsSplitQuery()` anywhere in the LINQ chain. EF sends one statement for the projects and one per collection, each joined back to the filtered, ordered projects so it fetches only their children. - **Globally:** set the default on the provider's options builder, then opt individual queries back with `.AsSingleQuery()`: ```csharp builder.Services.AddDbContext<TrackerDbContext>(o => o.UseNpgsql(connectionString, npgsql => npgsql.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery))); ``` Configuring either one also silences the warning, because you have made the decision explicit. ## What splitting costs | | `SingleQuery` (default) | `SplitQuery` | |---|---|---| | Statements | 1 | 1 + one per collection | | Rows for sibling collections | multiplied | added | | Consistency | one statement, one snapshot | each statement sees the data as it is when it runs | | Memory | rows can be streamed | earlier results usually buffered before the next statement | 1. **Round trips.** Every collection is another network round trip; with a high-latency database a small dashboard may be faster as a single query. 2. **Consistency.** If a work item is added between the first and second statement, the card can show a child whose parent row was read before it existed, or miss one. Wrapping the read in a serializable or snapshot transaction restores consistency at its own cost. 3. **Buffering.** Most databases allow one active result set per connection, so EF buffers earlier results in memory before running the next statement. 4. **Reference navigations** are repeated as joins in each split statement. ## Paging and EF Core 10 With `Skip`/`Take`, each split statement re-applies the parent ordering. Before EF Core 10 the subquery could drop the key from the `ORDER BY`, so an ordering that was not unique (by name alone) could pick different parents in different statements and attach children to the wrong page. EF Core 10 keeps the ordering consistent across the statements; on older versions you had to order by a unique column yourself. ## How to choose - Measure: look at row counts and timings for both modes on realistic data. - Split when sibling collections are large; keep a single query when collections are small or latency dominates. - Remember that one-to-one references always stay as joins in the same statement.
- Your context defaults to SplitQuery, but one report needs a consistent snapshot of a small project and its items; what do you do?Add `.AsSingleQuery()` to that query. It overrides the global `QuerySplittingBehavior.SplitQuery` for this query only, so the project and its items come from one statement. With small collections the multiplication is cheap and you get single-statement consistency without wrapping the read in a transaction.
- Why might a split query be slower than the single query it replaced?Each collection adds a network round trip, and on most databases EF must buffer the earlier results before running the next statement. When collections are small or the database is far away, those round trips cost more than the duplicated rows saved, so the choice should be measured.
- Does a query that loads one collection plus a reference navigation trigger MultipleCollectionIncludeWarning?No. The warning fires only when more than one collection navigation is loaded without a configured behaviour. Reference navigations are joined in the same statement in either mode, so one collection plus references stays a single statement without a warning.
- Split queries are the default in EF Core 10.
- AsNoTracking removes the duplicated rows from the join.
- Split queries are always faster than a single query.
- Split queries give the same consistency as one SQL statement.
- Distinct() in LINQ fixes the cartesian explosion.
Assumes EF Core 10, which keeps the parent ordering consistent across split statements when paging; earlier versions needed a fully unique ordering.