# Microsoft.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated.

- **ID:** `dotnet/ef-core-sql-timeout-large-data`
- **Domain:** dotnet
- **Category:** data_error
- **Error Code:** `0x80131904`
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

SQL query execution exceeds the configured command timeout, typically due to large data volumes, missing indexes, or inefficient LINQ queries generated by Entity Framework Core.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 6.0 | active | — | — |
| 7.0 | active | — | — |
| 8.0 | active | — | — |
| 9.0 | active | — | — |

## Workarounds

1. **Optimize the LINQ query by adding .Where() filters early and using .Select() to project only needed columns. Example: var data = context.Orders.Where(o => o.Date > DateTime.UtcNow.AddDays(-30)).Select(o => new { o.Id, o.Total }).ToList();** (85% success)
   ```
   Optimize the LINQ query by adding .Where() filters early and using .Select() to project only needed columns. Example: var data = context.Orders.Where(o => o.Date > DateTime.UtcNow.AddDays(-30)).Select(o => new { o.Id, o.Total }).ToList();
   ```
2. **Add database indexes on columns used in WHERE, JOIN, and ORDER BY clauses. Use SQL Server Management Studio or EF Core migrations: CREATE INDEX IX_Orders_Date ON Orders (Date);** (90% success)
   ```
   Add database indexes on columns used in WHERE, JOIN, and ORDER BY clauses. Use SQL Server Management Studio or EF Core migrations: CREATE INDEX IX_Orders_Date ON Orders (Date);
   ```
3. **Increase CommandTimeout temporarily for the specific operation using: context.Database.SetCommandTimeout(120); or in connection string: "Command Timeout=120;"** (75% success)
   ```
   Increase CommandTimeout temporarily for the specific operation using: context.Database.SetCommandTimeout(120); or in connection string: "Command Timeout=120;"
   ```

## Dead Ends

- **Increasing CommandTimeout to a very high value (e.g., 300 seconds) without optimizing the query** — Temporary fix that masks the underlying performance issue; query may still time out under heavy load or with larger datasets. (70% fail)
- **Adding .AsNoTracking() to all queries without understanding if tracking is needed** — AsNoTracking improves performance for read-only queries but does not fix inefficient joins or missing indexes. (50% fail)
- **Splitting the query into multiple smaller queries without using .Include() properly** — Multiple round trips can increase total execution time and cause N+1 query problems, worsening performance. (65% fail)
