Developer knowledge network · moderated exchange

Zajednica UnreliableCode

Zajednica za istraživanje, obrnuti inženjering i programiranje programera

Knowledge indexŽivjeti
4Categories
919Threads
2.8KPostovi
Tutorial

Entity Framework Core Performance: Compiled Queries and AsNoTracking for High-Throughput APIs [StackOverflow Architecture Guide]

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Rep: 103
Datum pridruživanja: Apr 2018
Postovi: 40
Hvala: 24
prije 1 mjeseci · Jul 9, 2026 8:49 PM
#1

Optimizing read-heavy Entity Framework Core queries for maximum QPS:

  1. .AsNoTracking(): Disables EF Core Change Tracker snapshot allocations, saving 50% CPU and memory on read operations.
  2. EF.CompileAsyncQuery: Pre-compiles the LINQ expression tree into a parameterized SQL delegate once at startup, eliminating query compilation overhead on every HTTP request:
CSHARP
private static readonly Func<AppDbContext, int, Task<User?>> GetUserByIdQuery =
    EF.CompileAsyncQuery((AppDbContext ctx, int id) => 
        ctx.Users.AsNoTracking().FirstOrDefault(u => u.Id == id));

// Execution: 4x faster execution!
var user = await GetUserByIdQuery(dbContext, userId);
generic_math_geek
C# 11 Math Guru
MEMBER
Rep: 138
Datum pridruživanja: Jul 2021
Postovi: 10
Hvala: 88
prije 1 mjeseci · Jul 10, 2026 2:05 AM
#2

Compiled queries are fantastic for high-frequency microservice lookups.

csharp_async_master
Async & Task Expert
MEMBER
Rep: 57
Datum pridruživanja: Mar 2019
Postovi: 18
Hvala: 40
prije 1 mjeseci · Jul 10, 2026 9:04 AM
#3

Combined with .AsNoTracking(), EF Core performance gets remarkably close to raw Dapper speeds.