【问题标题】:Query times out on Azure but not from localhost on same db查询在 Azure 上超时,但不是来自同一数据库上的本地主机
【发布时间】:2021-09-29 01:19:36
【问题描述】:

我有一个从 Azure 应用服务运行时超时的查询(命令超时设置为 600 秒以进行测试)。当我从 Visual Studio 在本地运行网站并连接到与应用服务完全相同的数据库时,相同的查询需要 9-11 秒才能运行。

Azure 应用服务计划 - P2V2 Azure SQL Server 计划 - S4

网站是 ASP Core 3.1 使用 EF Core 3.1

我进行了一些研究,发现来自不同环境的不同查询行为的可能原因可能是由于参数嗅探。不幸的是,我不知道我能做些什么来解决这个问题。

为了提供更多上下文,我今天在尝试查询之前尝试重新计算数据库索引,并且我还运行了DBCC FREEPROCCACHE

具有相同参数的相关查询的日志显示:

Azure 应用服务:

[41m[30mfail[39m[22m[49m: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (600,117ms) 
[Parameters=
[@__impersonateId_0='76889' (Nullable = true), 
@__impersonateId_1='76889' (Nullable = true), 
@__reportFilter_From_2='2019-01-01T00:00:00' (Nullable = true), 
@__reportFilter_To_3='2019-12-31T00:00:00' (Nullable = true), 
@__key_TransportationType_4='Rail' (Size = 450), 
@__key_CostCenter_5='' (Size = 4000)], 
CommandType='Text', CommandTimeout='600']

本地主机:

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (11,534ms) 
[Parameters=
[@__impersonateId_0='76889' (Nullable = true), 
@__impersonateId_1='76889' (Nullable = true), 
@__reportFilter_From_2='2019-01-01T00:00:00' (Nullable = true), 
@__reportFilter_To_3='2019-12-31T00:00:00' (Nullable = true), 
@__key_TransportationType_4='Rail' (Size = 450), 
@__key_CostCenter_5='' (Size = 4000)], 
CommandType='Text', CommandTimeout='600']

堆栈跟踪如下:

[41m[30mfail[39m[22m[49m: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type '...'.
Microsoft.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
 ---> System.ComponentModel.Win32Exception (258): Unknown error 258
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()
   at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
ClientConnectionId:...
Error Number:-2,State:0,Class:11
ClientConnectionId before routing:...
Routing Destination:...-a.worker.database.windows.net,11016

我可以做些什么来进一步研究从 Azure 应用服务中运行查询超时的原因?

【问题讨论】:

  • 您能分享您的查询或解释它的作用吗?
  • 因为参数嗅探只是一个假设。可以分享一下异常的错误信息或者堆栈跟踪吗?
  • @KarthikBhyresh-MT 首先,我不确定为什么有人会否决您的答案,因为它确实提供了一些有价值的信息(无论我已经看过其中的一些)。有时我希望 SO 能让人们稍微说明一下他们认为帖子被否决的原因。无论如何,根据您的评论,我发布了堆栈跟踪。
  • 很高兴您确认,检查跟踪

标签: asp.net-core entity-framework-core azure-web-app-service azure-sql-database


【解决方案1】:

由于您提到并且从跟踪中可以看出,相同的操作在 localhost 上完美运行,但限制了 App 服务,可能应该升级当前的应用程序服务计划。

如果您使用基于消费的计划,则超时值可以是 10 分钟,或者如果您使用高级版,则超时值是无限制的。如果触发器是 HTTP,那么无论如何超时都是 230 秒,这是由于 Azure 负载均衡器的默认空闲 timeout

您可以尝试使用article 进行故障排除或参考:here 类似问题。

缓解这种情况的一些常用方法:

  1. Scale the web app
  2. Use AutoHeal
  3. Restart the web app

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 2020-12-18
    • 2013-08-25
    • 2017-09-06
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    相关资源
    最近更新 更多