直接delete会导致db2日志满,不提倡。
-
思路一
export出其他日期的数据,然后直接import空文件或load空文件。再将export出的数据导入。 -
思路二
按时间切割成某几个小片段数据,delete,假设某天要删除10000000条记录,表中有字段时间精确到秒,可以切割到秒或分接级别的数据块,直接delete掉。 -
思路三
表中没有精确的时间字段,通过分批提交delete操作。
项目中用了第三种思路,详细代码如下:
#!/bin/sh
V_deleteCommit=10000
V_date=$1
echo "delete data..."
db2 connect to testdb
rs1=$?
if [ $rs1 -ne 0 ]; then
echo "db2 connect to failed!"
exit $rs1
fi
V_count=`db2 -x "select count(*) from testdb.testtable where date='$V_date' with ur"`
rs2=$?
if [ $rs2 -ne 0 ]; then
echo "select counts failed!"
exit $rs2
fi
V_count_tmp=$V_count
while [ V_count_tmp -gt 0 ]
do
db2 "delete from (select * from (select a.*,rownumber() over() as rn from testdb.testtable a where a.date='$V_date' order by date asc) as b where b.rn<=$V_deleteCommit)"
rs3=$?
if [ $rs3 -gt 0 ]; then
echo "delete failed!"
exit $rs3
fi
echo "deleted!"
V_count_tmp=$[V_count_tmp-V_deleteCommit]
done
echo "run success!"










网友评论