How would you optimize a slow EF Core query?
Quick Interview Answer
Detailed Explanation
Start by measuring and inspecting the generated SQL rather than assuming EF Core itself is the problem.
Check whether the query loads unnecessary columns or rows, performs N+1 queries, uses inefficient Includes, materializes too early, or executes client-side work. Use Select projections, AsNoTracking for read-only data, filtering before materialization, deterministic pagination, and appropriate query composition.
At the database level, inspect the actual execution plan, indexes, joins, scans, statistics, parameter behavior, and data distribution. An efficient LINQ expression cannot compensate for a missing critical database index.
Also review whether repeated queries can be cached, whether split or single queries are appropriate for large Include graphs, and whether bulk operations require specialized approaches.
After each optimization, measure again using realistic data and workload to verify that latency, CPU, memory, and database resource usage actually improved.