DEV Community

Cover image for AsSplitQuery() vs Cartesian Explosion
Maayur Dahake
Maayur Dahake

Posted on

AsSplitQuery() vs Cartesian Explosion

AsSplitQuery() vs Cartesian Explosion

During a recent database profiler session, I noticed an endpoint that spiked CPU and network utilization whenever it fetched an order aggregate. The culprit was a classic cartesian explosion caused by chaining multiple .Include() calls on a single LINQ query.

When EF Core generates a single SQL query with multiple collection includes (like Order -> Items -> Logs), SQL Server returns a cartesian product. If an order has 10 items and 10 logs, the database streams 100 rows containing duplicated parent order data. This inflates network packets and increases memory allocations during materialization.

Before

var q = db.Orders
  .Include(o => o.Items)
  .Include(o => o.Logs)
  .ToList();
Enter fullscreen mode Exit fullscreen mode

After

var q = db.Orders
  .Include(o => o.Items)
  .Include(o => o.Logs)
  .AsSplitQuery()
  .ToList();
Enter fullscreen mode Exit fullscreen mode

Why it matters

• Network payload: Eliminates redundant parent column transmission across the wire.
• Memory pressure: Reduces Gen0 allocations by avoiding massive duplicate instance graphs.
• Query mechanics: EF Core executes sequential SQL commands and stitches the entities back together client-side.

Watch out

Avoid applying .AsSplitQuery() globally or on tiny collections. The overhead of multiple database roundtrips and connection handle management can outweigh the savings if the dataset is small.

When do you decide to split queries versus sticking to a single JOIN in your repositories?

Top comments (0)