gpt4 book ai didi

sql-server - Azure SQL 频繁连接超时

转载 作者:行者123 更新时间:2023-12-02 12:52:04 37 4
gpt4 key购买 nike

我们正在 Azure 上运行一个 Web 应用程序(2 个实例),由 SQL Azure 数据库支持。在任何给定时间都有 50-150 个用户使用该网站。数据库以 S2 性能级别运行。 DTU 平均约为 20%。

但是,每天都有几次我的日志中突然出现数百个超时错误,如下所示:

An error occurred while executing the command definition. See the inner exception for details.

The wait operation timed out.

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. This failure occurred while attempting to connect to the routing destination. The duration spent while attempting to connect to the original server was - [Pre-Login] initialization=1; handshake=21; [Login] initialization=0; authentication=0; [Post-Login] complete=1;

我们使用 EF6 进行具有默认命令超时的查询。我已经配置了这个执行策略:

SetExecutionStrategy("System.Data.SqlClient", 
() => new SqlAzureExecutionStrategy(10, TimeSpan.FromSeconds(15)));

数据库(总共约 15GB)有大量索引。这些错误随处可见,通常在 1-2 分钟内出现数十到数百个错误。

我可以采取什么措施来防止这种情况发生?

最佳答案

事实上,它在 1-2 分钟内发生可能意味着事件突然爆发或某些进程可能会锁定表。

如果您的 DTU 在这些时间内处于 20%,则不是 CPU 问题,但您始终可以通过在数据库上运行此查询来找到哪些是瓶颈:

SELECT TOP 10 
total_worker_time/execution_count AS Avg_CPU_Time
,execution_count
,total_elapsed_time/execution_count as AVG_Run_Time
,(SELECT
SUBSTRING(text,statement_start_offset/2,(CASE
WHEN statement_end_offset = -1 THEN LEN(CONVERT(nvarchar(max), text)) * 2
ELSE statement_end_offset
END -statement_start_offset)/2
) FROM sys.dm_exec_sql_text(sql_handle)
) AS query_text
FROM sys.dm_exec_query_stats
ORDER BY Avg_CPU_Time DESC

即使数据库有大量索引,索引也会碎片化,我建议运行它来检查当前碎片:

select a.*,b.AverageFragmentation from 
( SELECT tbl.name AS [Table_Name], tbl.object_id, i.name AS [Name], i.index_id, CAST(CASE i.index_id WHEN 1 THEN 1 ELSE 0 END AS bit) AS [IsClustered],
CAST(case when i.type=3 then 1 else 0 end AS bit) AS [IsXmlIndex], CAST(case when i.type=4 then 1 else 0 end AS bit) AS [IsSpatialIndex]
FROM
sys.tables AS tbl
INNER JOIN sys.indexes AS i ON (i.index_id > 0 and i.is_hypothetical = 0) AND (i.object_id=tbl.object_id))a
inner join
( SELECT tbl.object_id, i.index_id, fi.avg_fragmentation_in_percent AS [AverageFragmentation]
FROM
sys.tables AS tbl
INNER JOIN sys.indexes AS i ON (i.index_id > 0 and i.is_hypothetical = 0) AND (i.object_id=tbl.object_id)
INNER JOIN sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'LIMITED') AS fi ON fi.object_id=CAST(i.object_id AS int) AND fi.index_id=CAST(i.index_id AS int)
)b
on a.object_id=b.object_id and a.index_id=b.index_id
order by AverageFragmentation desc

您还可以使用 Azure 自动化来安排碎片索引的自动重建,请参阅答案:Why my Azure SQL Database indexes are still fragmented?

关于sql-server - Azure SQL 频繁连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36056475/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com