EF Core 5 Performance: Split Queries

The “Cartesian Explosion” problem is common in ORMs. If you `Include()` multiple collections, the resulting SQL JOIN produces `Parent * Child1 * Child2` rows.

Split Queries

EF Core 5 allows you to opt-in to Split Queries.

var blogs = context.Blogs
    .Include(b => b.Posts)
    .AsSplitQuery()
    .ToList();

Instead of one massive JOIN, EF generates two SELECTS:
1. `SELECT * FROM Blogs`
2. `SELECT * FROM Posts WHERE BlogId IN (…)`

This guarantees consistent performance and avoids transferring redundant data. However, be aware of “Data Consistency” (Phantom reads) if the DB is updated between the two calls. Wrap it in a Transaction if necessary.


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.