常用的删除大数据方法(游标+分段)

 最近项目上线了,数据量也比较大,表中数据基本上都在千万,有时要清理些数据.由于数据量较大,每次在清理时都比较耗费时间,特别是在几千万的表中直接delete数据,有时几个小时都删除不了,大大的耗费时间,工作效率不高.每每想起以前处理的方法,只是很久没有用比较生疏,有时就是记不起,因此趁现在记得,保留下方便以后用时查看:

create or replace procedure p_del_bigtab
as
   v_count   number := 0;     --记录行数
   v_max     number := 100000;  --最大赋值
   v_id      dbms_sql.Varchar2_Table;  --数组
   v_no      dbms_sql.Varchar2_Table;
   v_pid     dbms_sql.Varchar2_Table;
   cursor cur_acc_customer      --游标
   is
   select v_acc,v_customer_no,v_product_id
     from tb_acc_customer t
    where exists (select 1
            from tb_cert t1
           where t1.v_acc = t.v_acc
             and t1.v_customer_no = t.v_customer_no
             and t1.v_product_id = t.v_product_id
             and t1.n_end_date is not null);
begin

   open cur_acc_customer;  --打开游标

   loop    --循环取游标数,将其赋值给数组

      fetch cur_acc_customer bulk collect into v_id,v_no,v_pid limit v_max;

        forall i in 1 .. v_id.count    --使用forall删除数据
          delete from tb_acc_customer
           where v_acc = v_id(i)
             and v_customer_no = v_no(i)
             and v_product_id = v_pid(i);

        v_count := sql%rowcount + v_count;  --记录行数
        commit;
      exit when cur_acc_customer%notfound; --取完游标后退出
   end loop;
   dbms_output.put_line(v_count);
   commit;
   close cur_acc_customer;  --关闭游标
end;


转载于:https://www.cnblogs.com/liuzhuqing/archive/2013/03/12/7480609.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值