删除大量行

时间:2013-07-08 10:20:59

标签: sql-server performance sql-delete

我想根据此条件从表1中删除记录(行数> 3亿条记录)(返回> 1亿条记录): 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中不再引用的表2中的所有孤立记录(行数大约为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)

上述查询可能因为表扫描而花费大量时间。

湾与上述相同,但在Table1ID列上创建唯一的聚簇索引,在#table1中的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中删除记录的最佳方法是什么?

注意:我知道创建新表的方法,转储所有相关数据并重命名。请提供有关替代方法的建议 - 优点和缺点。

2 个答案:

答案 0 :(得分:3)

因为你真的想删除高百分比,所以你可以考虑这种方法

 SELECT col1, col2, ... INTO #SomeHoldingtable
            FROM MyTable WHERE ..condition..

 TRUNCATE TABLE MyTable

 INSERT MyTable (col1, col2, ...)
           SELECT col1, col2, ... FROM #SomeHoldingtable

答案 1 :(得分:0)

如果我理解正确,过滤索引和批量删除应该有帮助(SQL Server 2008或更高版本):

create index IX_1 on dbo.Table1 (Column1, Column2, ...) include (Table2ID) where Column1 is NULL and Column2 is NULL and ...

create table #Deleted (Table2ID uniqueidentifier NULL)
create index #IX_Deleted on #Deleted (Table2ID) where Table2ID is not NULL


select 1
while @@rowcount > 0
    delete top (50000) T
    output deleted.Table2ID into #Deleted (Table2ID)
    from dbo.Table1 T
    where Column1 is NULL and Column2 is NULL and ...

select 1
while @@rowcount > 0
    delete top (50000) T
    from dbo.Table2 T
        join (select distinct Table2ID from #Deleted where Table2ID is not NULL) D on D.Table2ID = T.ID

drop table #Deleted
drop index IX_1 on dbo.Table1

并且,根据需要(一次性操作或期刊),可以保留索引IX_1

在SQL Server 2005上,使用索引计算列可以完成相同的操作。