Oracle 分批提交DML

2021-09-01 17:23:57 字數 2374 閱讀 5817

/*

參考於tom程式設計藝術 8.5章在迴圈中提交

1.採用分批操作並不能提高執行速度,執行效率不如單條dml語句。

2.分批插入可以減少對undo空間的占用,但頻繁的提交,可能會導致前面提交的undo空間被其他事務占用而可能導致ora-0155錯誤。

3.若分批操作中間半截失敗掉,會將你資料庫置於一種未知的狀態。(delete操作不會出現這種情況)

*/--分批 update

drop table t2;

create table t2 as select object_name from dba_objects;

select * from t2;

select count(*) from t2;

--is table of 建立乙個xx型別的陣列

declare

type ridarray is table of rowid;

type vcarray is table of t2.object_name%type;

l_rids ridarray;

l_names vcarray;

cursor c is select rowid, object_name from t2;

begin

open c;

loop

fetch c bulk collect into l_rids, l_names limit 10;

forall i in 1 .. l_rids.count

update t2

set object_name = lower(l_names(i))

where rowid = l_rids(i);

commit;

exit when c%notfound;

end loop;

close c;

end;

--分批delete

drop table t3;

create table t3 as select * from dba_objects;

declare

cursor mycursor is select rowid from t3 order by rowid; --------按rowid排序的cursor,刪除條件是***=***x,根據實際情

type rowid_table_type is table of rowid index by pls_integer;

v_rowid rowid_table_type;

begin

open mycursor;

loop

fetch mycursor bulk collect into v_rowid limit 5000; --------每次處理5000行,也就是每5000行一提交

exit when v_rowid.count=0;

forall i in v_rowid.first..v_rowid.last

delete from t3 where rowid=v_rowid(i);

commit;

end loop;

close mycursor;

end;

/--分批insert

drop table t4;

drop table t5;

create table t4 as select * from dba_objects;

create table t5 as select * from t4 where 1=0;

declare

cursor mycursor is select rowid from t4 order by rowid; --------按rowid排序的cursor,刪除條件是***=***x,根據實際情

type rowid_table_type is table of rowid index by pls_integer;

v_rowid rowid_table_type;

begin

open mycursor;

loop

fetch mycursor bulk collect into v_rowid limit 5000; --------每次處理5000行,也就是每5000行一提交

exit when v_rowid.count=0;

forall i in v_rowid.first..v_rowid.last

insert into t5 select * from t4 where rowid=v_rowid(i);

commit;

end loop;

close mycursor;

end;

Oracle Delete分批提交

1 oracle delete操作會產生redo log,undo log,即使delete 語句中加上nologging 引數,還是會記錄日誌的,這樣保證了資料安全性,利於rollback。為了不使undo表空間被撐爆,可能需要分批提交,以下是分批提交語句 declare cnt number 1...

Oracle按資料量分批次提交

留備 declare type cur is ref cursor my cur cur col num scott.emp test rowtype num number 10 begin open my cur for select from scott.emp test loop fetch ...

EF架構 資料分批批量提交

回到目錄 對於大資料量提交,包括插入,更新和刪除,我始終不建議用 自帶的方法,因為它會增加與資料庫的互動次數,一般地,的乙個上下文在提交時會開啟乙個資料連線,然後把轉換成的 語句一條一條的發到資料庫端 然後去提交,試想,如果你的資料量達到萬級別 更不用說百萬,千萬資料了 那對資料庫的壓力是很大的,所...