查询处于非活动状态后Azure SQL超时



我的托管Azure SQL实例经常超时。当一段时间内没有发生查询活动时,第一个查询(预计返回500-2000行(将使用我的定价层(S2 50 DTU计划(中的所有可用DTU,并总是导致以下异常:

System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (258): The wait operation timed out
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
ClientConnectionId:903324e4-4eba-4522-bae8-228a23c0e51c
Error Number:-2,State:0,Class:11
ClientConnectionId before routing:24ca7fb2-4c3b-44ad-b393-d8cda9dda172

当紧接着执行相同的查询时,服务器会立即做出响应。可能是因为最近的查询已加载到内存中。SQL Server用于物联网设置,因此来自设备的数据流(批量插入(在一天中不断发生。我试图通过让Azure函数每小时执行一次查询来将频繁访问的数据加载到内存中来解决这个问题,但这只会解决我在Azure函数中查询的特定实体的问题。根据执行计划,我的所有查询都使用了正确的索引。

我认为实施重试策略不是一个可以接受的解决方案。从我的角度来看,这只会进一步降低用户体验。有人有这个问题的经验吗?

编辑:此处为执行计划:https://www.brentozar.com/pastetheplan/?id=rJQvb7tRm

实际的执行计划显示,在DeviceUniqueIdentifier聚集索引键上的聚集索引搜索读取了1187行,尽管没有行满足其他条件。将InstanceIdNVEControllerTimestamp添加到聚集索引键将避免不必要地接触这些行。

通常,支持返回所有列的平凡查询的最佳索引是一个聚集索引,该索引首先包含相等谓词键列(DeviceUniqueIdentifierInstanceId(,然后是不等列(在NVEControllerTimestamp上进行范围搜索(。

可以使用CREATE INDEX...WITH (DROP_EXISTING = ON)将其他列添加到现有聚集索引中。

最新更新