Entity Framework Core Interview Question #193

How do you diagnose slow EF Core requests in production?

Testing, Production Practices & Senior Real-World Scenarios Senior Advanced

Quick Interview Answer

Separate application time from database time using tracing and metrics. Capture slow command duration, query tags, generated SQL where appropriate, execution plans, row counts, lock waits, and frequency. Check for N+1 queries, missing indexes, oversized result sets, blocking, parameter-sensitive plans, and repeated queries. Optimize only after identifying the dominant cost.

Detailed Explanation

Separate application time from database time using tracing and metrics. Capture slow command duration, query tags, generated SQL where appropriate, execution plans, row counts, lock waits, and frequency. Check for N+1 queries, missing indexes, oversized result sets, blocking, parameter-sensitive plans, and repeated queries. Optimize only after identifying the dominant cost.

Code Example

var query = db.Orders
    .TagWith("Dashboard: recent orders")
    .AsNoTracking()
    .Where(o => o.CreatedAtUtc >= fromUtc);