Increasing the command timeout only hides the symptom. The right approach is to measure, understand the plan and fix the cause. 1. Capture the actual SQL and timings - Enable EF Core logging: optionsBuilder.LogTo(Console.WriteLine, LogLevel.Information) or use the Microsoft.EntityFrameworkCore.Database.Command category; query.ToQueryString() shows the SQL for one query. - Check how many queries a request sends; one slow request is often hundreds of small queries (N+1). - Use Application Insights, MiniProfiler or OpenTelemetry to see duration per query. 2. Read the execution plan - Run the SQL in SSMS or Azure Data Studio with the actual execution plan, or check Query Store for regressions. - Look for table or index scans on large tables, key lookups, large row-count estimates that are wrong, implicit conversions, sorts that spill to tempdb, and parameter sniffing issues. 3. Common fixes - Add or adjust indexes matching the WHERE, JOIN and ORDER BY columns, with INCLUDE columns to make them covering. - Project only needed columns with Select instead of loading entities with many Includes. - Use AsSplitQuery for multiple collection includes to avoid cartesian explosion. - Fix non-sargable filters: functions on columns (ToLower(), date conversions), leading-wildcard Contains, or comparing columns with mismatched types (nvarchar vs varchar). - Replace client-side evaluation or loops with set-based queries. - Use AsNoTracking for read-only queries. - Paginate instead of returning unbounded results. - For pathological cases, write the query by hand with FromSql or a database view, or use a compiled query for very hot paths. 4. Watch the database too - Outdated statistics, blocking from long transactions and missing maintenance can make good queries slow. Check wait statistics and blocking sessions. 5. Verify and protect - Measure again with production-like data volumes, since a query that is fast on 1,000 rows can fail on 10 million. - Add a regression test or monitoring alert on query duration. Keep a modest timeout as a safety net — it protects the system — but treat timeouts as a signal to investigate.
Ravindra Wadile · · 0 likes