我有一个表(比如表1),其主键与其他10个表具有外键关系。所有这些表都有数百万个数据。我需要以性能有效的方式删除Table1中的所有孤立记录。此外,脚本不应阻止对这些表的写入。有人可以帮我查询
我正在使用SQL Server 2014
答案 0 :(得分:0)
我做了类似的删除操作:
对于任何给定的场景,如果您不允许在PROD中执行上述任何步骤,则在线删除将分别变慢
答案 1 :(得分:0)
由于两个表中都有交易并且最小化锁定,因此您可以应用原则分而治之
1-获取表1(父表)的最大ID
2-分阶段运行以下脚本,例如每次迭代100000行
DELETE FROM tabl2 t2
WHERE t2.id < 100000
AND t2.id IN (SELECT t1.id FROM table1 t1)
3-对其他范围重复步骤2
where t2.id BETWEEN 100001 AND 200000
最多为最高ID
您可以使用while语句
自动执行这些步骤答案 2 :(得分:0)
这就是我现在正在尝试的事情:
SET NOCOUNT ON
DECLARE @totalcnt INT
,@Batch INT = 1000
IF OBJECT_ID('tempdb..#TEMPWrk') IS NOT NULL
DROP TABLE #TEMPWrk
IF OBJECT_ID('tempdb..#TEMP') IS NOT NULL
DROP TABLE #TEMP
CREATE TABLE #TEMP (TableKey int NOT NULL PRIMARY KEY)
CREATE TABLE #TEMPWrk (TableKey int NOT NULL PRIMARY KEY)
INSERT INTO #TEMP
SELECT
A.TableKey
FROM
dbo.Table1 A
LEFT JOIN (
SELECT TableKey FROM dbo.Table2 UNION
SELECT TableKey FROM dbo.Table3 UNION
SELECT TableKey FROM dbo.Table4 UNION
SELECT TableKey FROM dbo.Table5 UNION
SELECT TableKey FROM dbo.Table6 UNION
SELECT TableKey FROM dbo.Table7 UNION
SELECT TableKey FROM dbo.Table8
) T ON T.TableKey = A.TableKey
WHERE
T.TableKey IS NULL
SELECT
@totalcnt = COUNT(*)
FROM
#TEMP
WHILE (@totalcnt > 0)
BEGIN
PRINT @totalcnt
DELETE TOP (@batch)
FROM
#TEMP
OUTPUT DELETED.TableKey INTO #TEMPWrk
DELETE T
FROM
dbo.Table1 T
JOIN #TEMPWrk A ON T.TableKey = A.TableKey
DELETE FROM #TEMPWrk
SELECT
@totalcnt = COUNT(*)
FROM
#TEMP
END
SET NOCOUNT OFF