1.对于分区表中的数据,直接删除掉分区表,alter table table_name drop partition partition_name; 因为分区表的分区相当于一个独立的表,删除分区相当于直接删除一个表速度快。
2.在删除大量数据时,批量删除,使用rownum一次只删除一部分,这样删除的语句可能存在一个循环,如:有多个表,每个表每次删除1000条数据,这样会存在一个问题,当只有一个表中有数据需要删除时,删除其他表的sql其实是多余的
解决方法:使用数组来保存上次删除的条数(sql%count),这样如果一个表中没有需要删除的数据,下次执行sql时不会再执行
3.对于多个sql中相同的部分的结果可以提取出来放到临时表中
如:delete * from memeber where groupid in (select id from group where valid=1 and time < sysdate);
可以将数据保存在一个临时表:
create global temporary table temp_test(id varchar2(15)) on commit preserve rows;(preserve表示在commit时保存数据,这样可以跨越多个事务,即提交不会清空临时表)
insert into temp_test (select id from group where valid=1 and time < sysdate);
在使用到的地方,用 (select id from temp_test )来代替.
2.在删除大量数据时,批量删除,使用rownum一次只删除一部分,这样删除的语句可能存在一个循环,如:有多个表,每个表每次删除1000条数据,这样会存在一个问题,当只有一个表中有数据需要删除时,删除其他表的sql其实是多余的
解决方法:使用数组来保存上次删除的条数(sql%count),这样如果一个表中没有需要删除的数据,下次执行sql时不会再执行
3.对于多个sql中相同的部分的结果可以提取出来放到临时表中
如:delete * from memeber where groupid in (select id from group where valid=1 and time < sysdate);
可以将数据保存在一个临时表:
create global temporary table temp_test(id varchar2(15)) on commit preserve rows;(preserve表示在commit时保存数据,这样可以跨越多个事务,即提交不会清空临时表)
insert into temp_test (select id from group where valid=1 and time < sysdate);
在使用到的地方,用 (select id from temp_test )来代替.
本文介绍在数据库操作中如何高效地删除数据,包括针对分区表的快速删除方法、批量删除策略及利用临时表优化多表查询的技术。
319

被折叠的 条评论
为什么被折叠?



