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;


  • 相关阅读:
    404 页面不存在
    Elasticsearch 目录总结
    Excel 数据导入至Sqlserver 数据库中 ltrim() 、rtrim() 、replace() 函数 依次空格无效问题
    自动化测试工具-Selenium IDE 教程一
    搜索引擎背后的经典数据结构和算法
    如何收缩SQLServer 数据库日志文件大小?
    如何处理自我感动【转载】
    Microsoft.Jet.OLEDB.4.0 和 Microsoft.ACE.OLEDB.12.0 的区别
    windows 2012 IIS 部署 .net core HTTP Error 502.5
    Windows Server2012 KB2919355 补丁无法安装
  • 原文地址:https://www.cnblogs.com/liuzhuqing/p/7480609.html
Copyright © 2011-2022 走看看