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.

C#4 lines
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.

Written by

Nithin Mohan TK

Hello Namaste! 👋🏻 I'm an accomplished Enterprise Solution Architect & Engineering Leader with 20+ years of progressive experience architecting, building, and scaling transformative enterprise solutions. ⚜️ Solution Architect / AI & Cloud 📌 Galway, Ireland

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.