Entity Framework C# Optimization Techniques
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, e memory-friendly:
- Split Queries
- AsNoTracking
- Cartesian Explosion
- Projection with Automapper
The Problem: Cartesian Explosion
A Cartesian explosion happens when EF Core loads related collections in a single query using multiple PARTICIPEs.
For example:
var blogs = aguardar contexto.Blogs
.Incluir(b => b.Posts)
.Incluir(b => b.Comentários)
.ToListAsync();Under the hood, EF Core might generate something like:
SELECT b.*, p.*, c.*
DE Blogs b
ESQUERDA PARTICIPE Posts p LIGADO b.Id = p.BlogId
ESQUERDA PARTICIPE Comentários c LIGADO b.Id = c.BlogIdIf a blog has 10 posts and 10 comments, you get 100 rows in memory for 1 blog — multiplying data unnecessarily.
Isso wastes memory, increases network payload, and slows everything down.
Split Queries to the Rescue
EF Core 5+ introduced Split Queries para Include().
Instead of doing one huge PARTICIPE, EF Core can load data in multiple SQL queries:
var blogs = aguardar contexto.Blogs
.Incluir(b => b.Posts)
.Incluir(b => b.Comentários)
.AsSplitQuery()
.ToListAsync();Now EF Core sends:
-- First consulta
SELECT * FROM Blogs;
-- Second consulta
SELECT * DE Posts ONDE BlogId IN (...);
-- Third consulta
SELECT * DE Comentários ONDE BlogId IN (...);Advantages:
- Avoids Cartesian explosion
- Reduces duplicate data
- Smaller memory footprint
Disadvantages:
- More round-trips to the database
- Slightly higher latency if your DB is far away
Tip: You can enable split queries globally:
optionsBuilder.UseSqlServer(connectionString, o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));AsNoTracking for Read-Only Queries
By default, EF Core tracks every entity it loads, so it can detect changes and persist them later.
But if you’re just reading data and not updating it, tracking is pure overhead.
var produtos = aguardar contexto.Produtos
.AsNoTracking()
.ToListAsync();Benefits:
- Faster queries
- Lower memory usage
- No unnecessary
ChangeTrackerwork
You can combine AsNoTracking() with split queries for maximum read performance.
Projection with AutoMapper
If you don’t need the full entity, project directly into a DTO — skipping entity materialization entirely. While writing select statements to pull the columns you need is very tedious, Automapper takes care of that work for you. In the Nano Boilerplate, any repository method that returns mapped DTOs uses projection.
Instead of:
var blogs = aguardar contexto.Blogs
.Incluir(b => b.Posts)
.AsNoTracking()
.ToListAsync();
var dtos = _mapper.Map<Lista<BlogDto>>(blogs); // Mapping in memoryYou can use AutoMapper’s ProjectTo:
var dtos = aguardar _mapper.ProjectTo<BlogDto>(contexto.Blogs
.AsNoTracking()
.AsSplitQuery())
.ToListAsync();Why ProjectTo is better:
- Executes mapping at the database level — only needed columns are selected
- No unnecessary entity tracking
- Prevents Cartesian explosion when combined with split queries
- Avoids multiple passes over the data (no “load then map”)
Example DTO mapping:
público classe BlogDto
{
público int Id { obter; definir; }
público string Title { obter; definir; }
público Lista<PostDto> Posts { obter; definir; }
}
público classe PostDto
{
público int Id { obter; definir; }
público string Title { obter; definir; }
}
público classe BlogProfile : Profile
{
público BlogProfile()
{
CreateMap<Blog, BlogDto>();
CreateMap<Postagem, PostDto>();
}
}Benchmarks — How Much Faster Is This?
I ran a simple benchmark with 10,000 Blogs, each having 5 Posts e 5 Comments, using SQL Server on a local machine.
| Approach | Rows Returned | SQL Queries | Time (ms) | Memory (MB) |
|---|---|---|---|---|
| Single Query + Include | ~250,000 | 1 | 850 ms | 220 MB |
| Split Query + Include | ~20,000 | 3 | 510 ms | 85 MB |
| AsNoTracking + Split Query | ~20,000 | 3 | 430 ms | 65 MB |
| AutoMapper ProjectTo (Split Query) | ~20,000 | 3 | 320 ms | 42 MB |
The difference is especially dramatic in memory usage — projecting with AutoMapper’s ProjectTo pode cut memory by more than half compared to naive includes. This is a basic example. In real world scenarios, its common to have several include statements, which would push the number of rows and memory up exponentially.
Conclusão
EF Core’s defaults prioritize simplicity, but for large read-heavy workloads, you need to be intentional:
- Utilização split queries to avoid Cartesian explosion
- Utilização AsNoTracking for read-only queries
- Utilização projection (preferably with
ProjectTo) to reduce payloads and skip unnecessary entity tracking
When combined, these techniques can deliver massive performance wins — both in speed and memory usage.