我想根据此标准(返回 > 1 亿条记录)从 Table1 中删除记录(行 > 3 亿条记录):
Column1 IS NULL AND Column2 IS NULL AND Column3 IS NULL AND Column4 IS NULL AND Column5 IS NULL AND Column6 IS NULL AND Column7 IS NULL
表定义:
CREATE TABLE [dbo].[Table1](
[ID] [uniqueidentifier] NOT NULL,
[Column1] [float] NULL,
[Column2] [float] NULL,
[Column3] [float] NULL,
[Column4] [float] NULL,
[Column5] [float] NULL,
[Column6] [float] NULL,
[Column7] [float] NULL,
[Table2ID] [uniqueidentifier] NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
注意:Table2ID 是来自 Table2 的外键约束
CREATE TABLE [dbo].[Table2](
[ID] [uniqueidentifier] NOT NULL,
[Column1] [date] NULL,
[Column2] [tinyint] NULL,
[Column3] [tinyint] NULL,
CONSTRAINT [PK_Table2_ID] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
我还想从 Table2 中删除所有在 Table1 中不再引用的孤立记录(行数约为 1000 万条记录)。以下是我采取的方法:
一个。使用 Table1ID 和 Table2ID 列创建临时表 #table1。捕获所有相关的 Table1ID 并运行以下删除查询。
Delete from Table1 where ID in (select Table1ID from #table1)
Delete from Table2 where ID in (select Table2ID from #table1)
上述查询可能由于表扫描而花费了大量时间。
湾。一切都与上面相同,但在 #table1 中的 Table1ID 列和 Table2ID 上创建唯一聚集索引并运行以下查询:
Delete from Table1 t1 join #table1 tmp1 on t1.ID=tmp1.Table1ID
Delete from Table2 t2 join #table1 tmp1 on t1.ID=tmp1.Table2ID
有没有更好的方法来处理这种情况?从 Table1 和 Table2 中删除记录的最佳方法是什么?
注意:我知道我们创建一个新表、转储所有相关数据并重命名它的方法。请提供有关替代方法的建议 - 利弊。