General

Introduction

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId
				
			

Generals

Requirements

Entity Framework Core is an incredibly powerful ORM — but with great power comes great responsibility. If you’re not careful, a single LINQ statement can balloon into a massive SQL query, making performance terrible. I recently had this problem when building a SaaS tool. The problem was that some queries loaded a lot of related data, which very quickly resulted in out of memory exceptions. The problem was not easy to track down at first because the exceptions only happened on an Azure environment; everything worked fine on locally.
In this post, we’ll talk about four important concepts to keep your queries fast, predictable, and memory-friendly:

The Problem: Cartesian Explosion

A Cartesian explosion happens when EF Core loads related collections in a single query using multiple JOINs. For example:

				
					var blogs = await context.Blogs  
.Include(b => b.Posts)
.Include(b => b.Comments) 
.ToListAsync();
				
			
Under the hood, EF Core might generate something like:
				
					SELECT b.*, p.*, c.*
FROM Blogs b
LEFT JOIN Posts p ON b.Id = p.BlogId
LEFT JOIN Comments c ON b.Id = c.BlogId