zoukankan      html  css  js  c++  java
  • 常用的删除大数据方法(游标+分段)

     最近项目上线了,数据量也比较大,表中数据基本上都在千万,有时要清理些数据.由于数据量较大,每次在清理时都比较耗费时间,特别是在几千万的表中直接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;


  • 相关阅读:
    【力扣】11. 盛最多水的容器
    T-SQL 学习笔记 Chapter 6 子查询、表表达式 和排名函数 (一)
    忽然发现只是虚长了年岁,莫名的伤感。
    Gridview 多重表头 (二)
    那些 Cynthia 教我的事 之 PMSec (三)
    那些 Cynthia 教我的事 之 PMSec (二)
    那些 Cynthia 教我的事 之 PMSec (一)
    Gridview 多重表头 (一)
    项目总结之SSI (一)
    项目总结之MIT (一)
  • 原文地址:https://www.cnblogs.com/liuzhuqing/p/7480610.html
Copyright © 2011-2022 走看看