SQL Server Interview Question #296

How do you diagnose a slow EF Core query?

ASP.NET Core, EF Core & SQL Server Real-World Scenarios Senior Advanced

Detailed Explanation

Capture the generated SQL and parameter values, then analyze the database execution rather than tuning only the LINQ syntax.

Check the actual execution plan, Query Store, logical reads, row estimates, indexes, blocking, parameter types, result-set size, and server waits. In the application, check tracking, projection, Include usage, N+1 queries, query count, pagination, and premature ToList calls.

EF Core logging and ToQueryString can help inspect generated SQL during development. Production diagnostics should avoid exposing sensitive parameter data unnecessarily.

Code Example

var query = db.Products
    .Where(p => p.CategoryId == categoryId)
    .OrderByDescending(p => p.Rating);

var sql = query.ToQueryString();