1 か月前 · Jul 9, 2026 8:49 PM
Optimizing read-heavy Entity Framework Core queries for maximum QPS:
.AsNoTracking(): Disables EF Core Change Tracker snapshot allocations, saving 50% CPU and memory on read operations.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);