大數據量刪除與插入的PLSQL語句

轉載自:http://yangchao20020.blog.163.com/blog/static/48382247200921321442812/

在PL/SQL中的命令窗口執行下面的語句:(先將下面代碼粘貼到命令窗口,粘貼完後,再在窗口裏輸入"/"鍵則運行你剛粘貼到窗口裏的該程序代碼)

【1】業務邏輯:A表中有300W的數據,B表中有70W的數據,現在要對A表中的部分數據進行刪除,刪除條件是:根據B表中70W數據來對A表進行條件刪除,條件的字段爲usercode。

 

set serveroutput on
    Declare
       rcode integer;
    begin
          loop
            --每次將表a刪除10000條記錄
            delete from a
             where exists (select usercode from b where a.usercode = b.usercode)
               and rownum < 10000;
            --每10000條提交一次
            commit;
            exit when sql%rowcount = 0;
          end loop;
          rcode := 1;
          --delete操作完成後,rcode值爲1
          dbms_output.put_line('結果是:'||to_char(rcode));
    exception
      when others then
        rollback;
        rcode := 0;
        --delete操作失敗後,rcode值爲0
        dbms_output.put_line('結果是:'||to_char(rcode));
    end;

語句的邏輯:每循環一次,刪除a表中的10000條記錄,並commit提交該delete語句,當整個循環執行完後,70W數據成功刪除完後,會在命令窗口打印出(delete出現異常則打印0):

結果是:1
PL/SQL 過程成功完成

【2】業務邏輯:批量插入5000調測試數據到susr_info表和susr_basic表中,做爲測試數據。
    set serveroutput on
    Declare
      cnt             number(10);
      v_userindex     number(20);
      v_customerindex number(20);
      v_usercode      varchar(100);
      rcode           varchar(200);
      v_message       varchar(200);
    begin
          cnt := 1;
          v_usercode:=16500000000;
          loop
                begin
                      sp_get_next_seq('susr_info',v_customerindex);--序列器產生v_customerindex
                      sp_get_next_seq('susr_basic',v_userindex);--同上
                      v_usercode := v_usercode + 1;
                      insert into susr_info
                      (customerindex,
                       customerid,
                       usertruename,
                       useremail,
                       userpwd
                       )
                       values
                      (v_customerindex,
                       v_customerindex,
                       '' || v_usercode,
                       ' ',
                       '55555'
                       );
                       insert into susr_basic
                      (userindex,
                       usercode,
                       accountindex,
                       customerindex,
                       msisdntype
                       )
                       values
                      (v_userindex,
                       '' || v_usercode,
                       0,
                       v_customerindex,
                       1
                       );
                      if mod(v_userindex, 1000= 0 then
                        commit;
                      end if;
                exception
                when others then
                  rollback;
                  v_message := sqlcode||':'||sqlerrm;
                  --將異常記錄做日誌
                  insert into log_info values('zxdbm_ismp.v3_sub_2',v_message,sysdate,v_usercode); 
                  commit;
                end
                cnt := cnt + 1;
                if cnt > 5000 then
                  exit;
                end if;   
          end loop;
          commit;
          rcode := 'over';
          dbms_output.put_line('結果是:'||rcode);
    end;



語句的邏輯:每循環1000次,將LOOP循環體中的兩個insert語句的結果commit,每次循環,如果insert語句只其中一個出現異常,則將異常記錄寫到日誌表log_info中,接着執行下一次循環,當執行insert的次數達到5000,則退出循環,結束整個業務流程。執行完後,會在命令窗口打印出

結果是:over
PL/SQL 過程成功完成


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章