Tutorials Entity Framework Core Tutorial
Query Performance in EF Core
Query Performance in EF Core: free step-by-step lesson with examples, common mistakes, and interview tips — part of Entity Framework Core Tutorial on Toolliyo Academy.
On this page
Entity Framework Core Tutorial · Lesson 61 of 100
Query Performance in EF Core
Beginner ✓ → Intermediate ✓ → Advanced → Professional
Advanced · 3 — Production skills · ~10 min · Module 7: Performance Optimization
What is this?
Query performance measures end-to-end latency and resource use — SQL execution time, round trips, rows returned, and ORM materialization cost.
Why should you care?
ShopNest SLA requires product search under 200ms p95 — slow queries block revenue during sales.
See it live — copy this example
Paste into a .NET project with EF Core packages, then run with LocalDB/SQL Server (dotnet ef / dotnet run).
// Diagnose hot query
var sql = _context.Products
.Where(p => p.CategoryId == id)
.Select(p => new { p.Id, p.Name })
.TagWith("CategoryProductCards")
.ToQueryString();
_logger.LogInformation("SQL: {Sql}", sql);
var sw = Stopwatch.StartNew();
var data = await _context.Products.Where(p => p.CategoryId == id).Select(p => new { p.Id, p.Name }).ToListAsync();
_logger.LogInformation("Elapsed {Ms}ms", sw.ElapsedMilliseconds);
What happened?
- ToQueryString previews SQL.
- TagWith identifies query in logs.
- Stopwatch measures round trip including network and materialization.
Practice next
- Tag hot queries and enable logging in staging.
- Capture execution plan in SSMS for ToQueryString output.
- Fix missing indexes before micro-optimizing LINQ.
- Add compiled + AsNoTracking + projection together and benchmark.
- Compare read replica vs primary for report queries.
Remember
Measure SQL and ORM together. TagWith and ToQueryString aid diagnosis. Indexes often beat clever LINQ.
ShopNest p95 initiative
APM shows CategoryProductCards query over SLA — team adds composite index.
Outcome: p95 drops from 420ms to 85ms without code change beyond TagWith visibility.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!