.NET Core生产环境Payments表查询超时问题排查求助
Let’s walk through the key troubleshooting steps for this intermittent timeout issue—since your table only has ~400 rows, this is almost certainly not a data volume problem, but rather an environment, execution plan, or resource-related quirk.
1. Investigate Parameter Sniffing & Execution Plan Issues
Even small tables can suffer from poor query performance if SQL Server picks a bad execution plan due to parameter sniffing. Here’s what to check:
- Capture the actual execution plan for the problematic query in production. You can use Azure SQL Database’s Query Store to track slow queries and their plans, or add a query tag with EF Core’s
.TagWith("PaymentsLookupQuery")to identify it in logs. - Test adding
OPTION (RECOMPILE)to the query to force SQL Server to generate a fresh plan for each execution. In EF Core, you can adapt your query like this:var query = _context.Payments .Where(filter => filter.ClientClubType == request.ClientClubType && filter.ProductType == request.ProductType && filter.PaymentsNumber == request.PaymentsNumber) .AsQueryable() .FromSqlRaw(@"SELECT [p].[Id], [p].[ClientClubType], [p].[PaymentsNumber], [p].[ProductType], [p].[Rate] FROM [Payments] AS [p] WHERE ([p].[ClientClubType] = @ClientClubType OR @ClientClubType IS NULL) AND ([p].[ProductType] = @ProductType OR @ProductType IS NULL) AND ([p].[PaymentsNumber] = @PaymentsNumber OR @PaymentsNumber IS NULL) OPTION (RECOMPILE)", new SqlParameter("@ClientClubType", request.ClientClubType ?? (object)DBNull.Value), new SqlParameter("@ProductType", request.ProductType ?? (object)DBNull.Value), new SqlParameter("@PaymentsNumber", request.PaymentsNumber ?? (object)DBNull.Value)); - Ensure your table’s statistics are up to date. Outdated stats can lead to bad plan choices. Run
UPDATE STATISTICS Payments;in SSMS for the table.
2. Check Azure SQL Database Resource Constraints
Intermittent timeouts often correlate with peak resource usage:
- In the Azure Portal, navigate to your SQL Database’s Performance tab and check metrics like DTU/CPU Usage, Memory Usage, and IO Wait Time. If DTU/CPU hits 100% around the time of timeouts, your database is being throttled.
- Consider scaling up your SQL Database tier temporarily to see if the issue disappears (this is a quick way to rule out resource limits).
3. Validate Database Connection Pool Configuration
Switching to AddDbContextPool was a good move, but let’s verify the pool is behaving correctly:
- Check if connection pool exhaustion is occurring. The default max pool size is 100; if your WebApp has high concurrent requests, you might hit this limit. Adjust it in your connection string with
Max Pool Size=200;(test with a reasonable value). - Look for connection leaks. Ensure your
DbContextis being properly disposed—since you’re using dependency injection with scoped contexts, this should be handled automatically, but you can confirm by checkingsys.dm_exec_connectionsin SQL Server during peak traffic to see if connections are lingering.
4. Verify Network & Azure Environment Stability
Network blips between your Linux WebApp and SQL Database can cause unexpected timeouts:
- Confirm your WebApp and SQL Database are in the same Azure region—cross-region latency can add up, especially with retries.
- Check Application Insights dependency logs for spikes in query response time. Look for any correlation between timeouts and network latency increases.
- Ensure there are no firewall/NSG rules that might be interrupting connections intermittently.
5. Test Specific Parameter Combinations
Since the issue is intermittent, it’s likely tied to specific parameter values:
- Pull the parameter values from the Application Insights timeout logs (you can see
@__request_ClientClubType_0and@__request_ProductType_1in your failed command log) and run the same parameterized query in SSMS. Make sure to use parameterized queries (not hardcoded values) to replicate the exact execution plan EF Core uses. - Check if those parameter values correspond to edge cases (e.g., a
ClientClubTypethat matches almost all rows, leading to a full table scan that’s slow under load).
6. Check for Blocking or Deadlocks
Timeouts can happen if your query is blocked by another long-running transaction:
- Use
sys.dm_exec_requestsin SQL Server to check for blocking sessions during a timeout. Look for rows whereblocking_session_id != 0. - Enable deadlock tracking in Azure SQL Database (via the Portal or T-SQL) to see if deadlocks are occurring even though your table is small.
内容的提问来源于stack exchange,提问作者Shahar Shokrani




