Entity Framework Core Interview Question #132

What is cartesian explosion in EF Core queries?

Performance, Raw SQL, Stored Procedures & Advanced Queries Senior Advanced

Quick Interview Answer

Cartesian explosion can occur when one SQL query joins multiple collection navigations at the same level. Rows from one collection can be multiplied by rows from another, creating a much larger result set than expected. Projection, purpose-specific queries, or AsSplitQuery can reduce the problem depending on the required data shape.

Detailed Explanation

Cartesian explosion can occur when one SQL query joins multiple collection navigations at the same level. Rows from one collection can be multiplied by rows from another, creating a much larger result set than expected. Projection, purpose-specific queries, or AsSplitQuery can reduce the problem depending on the required data shape.

Code Example

var blogs = await db.Blogs
    .Include(b => b.Posts)
    .Include(b => b.Contributors)
    .AsSplitQuery()
    .ToListAsync();