我在 tsql 下运行以从主副本中查找辅助 AlwaysON 服务器:
set transaction isolation level read uncommitted
set statistics time, io on
SELECT
ISNULL(agstates.primary_replica, '') AS [Primary_AG_Server],
dbcs.database_name AS [DatabaseName]
,ar.replica_server_name as [Secondary_AG_Servers]
FROM master.sys.availability_groups AS AG
LEFT OUTER JOIN master.sys.dm_hadr_availability_group_states as agstates
ON AG.group_id = agstates.group_id
INNER JOIN master.sys.availability_replicas AS AR
ON AG.group_id = AR.group_id
INNER JOIN master.sys.dm_hadr_availability_replica_states AS arstates
ON AR.replica_id = arstates.replica_id AND arstates.is_local <> 1 -- is_local is not 1 .. so we get secondary server names
INNER JOIN master.sys.dm_hadr_database_replica_cluster_states AS dbcs
ON arstates.replica_id = dbcs.replica_id
group by agstates.primary_replica, dbcs.database_name, ar.replica_server_name
set statistics time, io off
上面的查询需要12 分 38 秒才能运行并返回 34 行。下面是统计IO和时间输出:
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server parse and compile time:
CPU time = 10 ms, elapsed time = 10 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
(34 row(s) affected)
Table 'Workfile'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'sysguidrefs'. Scan count 0, logical reads 212, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'sysclsobjs'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
SQL Server Execution Times:
CPU time = 1266 ms, elapsed time = 747852 ms.
有什么办法可以让它更快?
以下来自计划资源管理器
回答我自己的问题,这可能对未来的访客有帮助..
Aaron 的评论让我想如果我们将数据转储到临时表中会加快速度吗?
是的,由于大量的可用性组 53(我知道很多,我们计划很快卸载它们),它确实加快了速度,
sys.availability_groups
并且sys.availability_replicas
选择需要相当长的时间。下面的 tsql 在 1 分 03 秒内运行 :-)