0Pricing
C# Academy · Lesson

Compiled Queries & Performance

Boost query performance using EF.CompileQuery, AsNoTracking, and projection with Select.

EF Core Query Compilation

Every time EF Core executes a LINQ query, it must parse the expression tree, translate it to SQL, and validate it. For hot paths this overhead adds up. EF Core provides several tools to minimize it.

AsNoTracking for Read-Only Queries

AsNoTracking() skips change tracker registration. For read-only queries (API responses, reports), this is the single biggest easy win — typically 2–5x faster and less memory.

// With tracking (default) — for entities you'll modify
var tracked = await _db.Products.FirstOrDefaultAsync(p => p.Id == 1);

// Without tracking — for read-only
var dto = await _db.Products
    .AsNoTracking()
    .Where(p => p.IsActive)
    .Select(p => new ProductDto { Id = p.Id, Name = p.Name })
    .ToListAsync();

All lessons in this course

  1. Eager, Lazy & Explicit Loading
  2. Raw SQL, Stored Procedures & Interpolation
  3. Compiled Queries & Performance
  4. Global Query Filters & Owned Entities
← Back to C# Academy