Entity Framework Core Interview Question #149

How should EF Core queries be profiled and diagnosed in production?

Performance, Raw SQL, Stored Procedures & Advanced Queries Senior Advanced

Quick Interview Answer

Use application metrics, structured logs, distributed traces, database monitoring, and execution plans to determine where database time is spent. Look for slow queries, N+1 patterns, excessive rows, lock waits, timeouts, missing indexes, high-frequency queries, and expensive plans. Diagnostics must protect sensitive data; detailed or sensitive-data logging should only be enabled when appropriate.

Detailed Explanation

Use application metrics, structured logs, distributed traces, database monitoring, and execution plans to determine where database time is spent. Look for slow queries, N+1 patterns, excessive rows, lock waits, timeouts, missing indexes, high-frequency queries, and expensive plans. Diagnostics must protect sensitive data; detailed or sensitive-data logging should only be enabled when appropriate.

Code Example

var query = db.Products
    .TagWith("Product search")
    .Where(p => p.IsPublished);

_logger.LogDebug("SQL: {Sql}", query.ToQueryString());