DB2:从表中清除大量记录

时间:2013-05-07 18:42:08

标签: database performance db2 db2-luw

我正在使用DB2 9.7 FP5 for LUW。我有一个250万行的表,我想删除大约100万行,这个删除操作分布在表中。我正在使用5个删除语句删除数据。

delete from tablename where tableky between range1 and range2
delete from tablename where tableky between range3 and range4
delete from tablename where tableky between range5 and range5
delete from tablename where tableky between range7 and range8
delete from tablename where tableky between range9 and range10

执行此操作时,前3个删除工作正常但第4个失败并且DB2挂起,什么都不做。以下是我遵循的流程,请帮助我:

1. Set following profile registry parameters: DB2_SKIPINSERTED,DB2_USE_ALTERNATE_PAGE_CLEANING,DB2_EVALUNCOMMITTED,DB2_SKIPDELETED,DB2_PARALLEL_IO

2.Alter bufferpools for automatic storage.

3. Turn off logging for tables (alter table tabname activate not logged initially) and delete records

4. Execute the script with +c to make sure logging is off

删除大量数据的最佳做法是什么?为什么它在删除同一个表中的数据并且性质相同时失败了?

6 个答案:

答案 0 :(得分:5)

这是一项非常棘手的任务。事务的大小(例如,用于安全回滚)受事务日志大小的限制。事务日志不仅由您的sql命令填充,而且还由在同一时刻使用db的其他用户的命令填充。

我建议使用以下方法之一/或以下方法的组合

1。提交

经常提交 - 在你的情况下,我会在每个删除命令之后放一个提交

2。增加事务日志的大小

我记得默认的db2事务日志不是很大。应分别为每个数据库计算/调整事务日志的大小。参考here以及更多详情here

3。存储过程

编写并调用以块为单位删除的存储过程,例如:

-- USAGE - create: db2 -td@ -vf del_blocks.sql
-- USAGE - call: db2 "call DEL_BLOCKS(4, ?)"

drop PROCEDURE DEL_BLOCKS@

CREATE PROCEDURE DEL_BLOCKS(IN PK_FROM INTEGER, IN PK_TO INTEGER)
LANGUAGE SQL
BEGIN
    declare v_CNT_BLOCK     bigint;

    set v_CNT_BLOCK   = 0;

    FOR r_cur as c_cur cursor with hold for
        select tableky from tablename 
        where tableky between pk_from and pk_to
        for read only
    DO
            delete from tablename where tableky=r_cur.tableky;

            set v_CNT_BLOCK=v_CNT_BLOCK+1;

            if v_CNT_BLOCK >= 5000 then
                set v_CNT_BLOCK = 0;
                commit;
            end if;
    END FOR;

    commit;
END@

4。使用替换选项

导出+导入

在某些情况下,当我需要清除非常大的表或只留下少量记录(并且没有FK约束)时,我使用了export + import(replace)。替换导入选项非常具有破坏性 - 它在导入新记录之前清除整个表(引用of db2 import command),因此请确保您正在做什么并在之前进行备份。对于这样的敏感操作,我创建3个脚本并分别运行:备份,导出,导入。以下是导出脚本:

echo '===================== export started '; 
values current time;

export to tablename.del of del  
select *  from tablename where (tableky between 1 and 1000 
    or tableky between 2000 and 3000 
    or tableky between 5000 and 7000 
    ) ; 
echo '===================== export finished ';  
values current time;

以下是导入脚本:

echo '===================== import started ';  
values current time;

import from tablename.del of del  allow write access commitcount 2000
-- !!!! this is IMPORTANT and VERY VERY destructive option  
replace  
into tablename ;

echo '===================== import finished ';

5。截断命令

版本9.7中的Db2引入了TRUNCATE语句:

  

删除表格中的所有行。

基本上:

TRUNCATE TABLE <tablename> IMMEDIATE

我没有在db2中使用TRUNCATE的经验,但在其他一些引擎中,命令非常快并且不使用事务日志(至少不是通常的方式)。请检查所有详细信息hereofficial documentation。作为解决方案4,此方法非常具有破坏性 - 它会清除整个表,因此在发出命令之前要非常小心。确保先前使用表/数据库备份执行的状态。

请注意何时执行此操作

当db上没有其他用户时,或通过锁定表来确保这一点。

关于回滚的注意事项

在事务db(如db2)中,回滚可以将db状态恢复到事务启动时的状态。在方法1,3和4中,这是无法实现的,所以如果你需要“恢复到原始状态”的功能,唯一的选择是确保这是方法nr。 2 - 增加交易日志

答案 1 :(得分:1)

delete from ordpos where orderid in ((select orderid from ordpos where orderid not in (select id from ordhdr) fetch first 40000 rows only));

希望这会解决您的疑问:)

答案 2 :(得分:0)

DB2不太可能“挂起” - 更有可能是在DELETE操作填充事务日志后进行回滚的过程中。

确保您在每个DELETE声明之后提交。如果使用DB2 CLP的+c选项执行脚本,请确保在每个COMMIT之间包含明确的DELETE语句。

答案 3 :(得分:0)

删除具有数百万行的数据的最佳做法是在删除之间使用提交。在您的情况下,您可以在每个删除语句后使用提交。

它的作用是清除转换日志并为其他delte操作提供空间。

或者,5个删除语句的instad使用循环并将delete语句传递给delete,循环的一次迭代执行一次提交后,数据库永远不会挂起,同时你的数据将被删除。

使用这样的东西。

while(count<no of records)
delete from (select * from table fetch fist 50000 records only)
commit;
count= total records- no of records.

答案 4 :(得分:0)

如果SELECT WHERE FETCH FIRST 10 ROWS ONLY只能插入几块记录,例如10块,那么你可以将其作为输入提供给另一个脚本,然后删除这些记录。冲洗并重复......

答案 5 :(得分:-2)

为了每个人的利益,here是关于同一问题的developerWorks文章的链接。我尝试了不同的东西,我在这篇文章中分享的那个对我很有用。

相关问题