我的一些表是 REPLICATE 类型的。在我开始查询我的数据之前,我希望这些表被实际复制(而不是挂起)。这将帮助我避免数据移动。
我有一个脚本,我在网上找到了它,它循环运行并在所有设置为复制的表上执行 SELECT TOP 1,但有时脚本会运行几个小时。即使您从 foo 执行 SELECT TOP 1,服务器有时也可能不会触发复制。
如何强制 SQL 数据仓库完成复制?
脚本看起来像这样:
begin
CREATE TABLE #tbl
WITH
( DISTRIBUTION = ROUND_ROBIN
)
AS
SELECT
ROW_NUMBER() OVER(
ORDER BY
(
SELECT
NULL
)) AS Sequence
, CONCAT('SELECT TOP(1) * FROM ', s.name, '.', t.[name]) AS sql_code
FROM sys.pdw_replicated_table_cache_state AS p
JOIN sys.tables AS t
ON t.object_id = p.object_id
JOIN sys.schemas AS s
ON t.schema_id = s.schema_id
WHERE p.[state] = 'NotReady';
DECLARE @nbr_statements INT=
(
SELECT
COUNT(*)
FROM #tbl
), @i INT= 1;
WHILE @i <= @nbr_statements
BEGIN
DECLARE @sql_code NVARCHAR(4000)= (SELECT
sql_code
FROM #tbl
WHERE Sequence = @i);
EXEC sp_executesql @sql_code;
SET @i+=1;
END;
DROP TABLE #tbl;
SET @i = 0;
WHILE
(
SELECT TOP (1)
p.[state]
FROM sys.pdw_replicated_table_cache_state AS p
JOIN sys.tables AS t
ON t.object_id = p.object_id
JOIN sys.schemas AS s
ON t.schema_id = s.schema_id
WHERE p.[state] = 'NotReady'
) = 'NotReady'
BEGIN
IF @i % 100 = 0
BEGIN
RAISERROR('Replication in progress' , 0, 0) WITH NOWAIT;
END;
SET @i = @i + 1;
END;
END