oracle使用bulk collect insert實現大資料快速遷移
阿新 • • 發佈:2019-02-12
在Oracle 資料庫中,將一張表的部分欄位轉換匯入到另外一張表的部分欄位中,如"insert into t_target(owner,object_name) from t_source;" 。
這也是解決此類資料遷移的的常規操作方法。
如果源表記錄數不是很多,操作操作也無妨。但是若源表記錄數一旦超過百萬千萬,執行時間就長了,佔用的回滾段也大。不小心執行失敗,又要全部回滾。有沒有更好的方法,加hint 如nologging append, 甚至parallel。這些我認為都不是很好的方法,有有一種方法,稱之為bulk collect into 。使用它可以減少操作時間。這是基於資料塊的插入方式。
目標表的結構如下:
源表為dba_objects ,表結構在資料庫中可以查到。
需要將源表dba_objects 中兩個欄位object_name,owner 兩個欄位的值複製到t_target 表中。
1.Explicit cursors doing a BULK COLLECT into a local collection (with an appropriate LIMIT) and using FORALL to write back to the database.
2.Implicit cursors doing a BULK COLLECT for you behind the scenes along with single-row writes back to the datbase.
3.Explicit cursors that are not doing a BULK COLLECT and not taking advantage of PL/SQL collections.
這也是解決此類資料遷移的的常規操作方法。
如果源表記錄數不是很多,操作操作也無妨。但是若源表記錄數一旦超過百萬千萬,執行時間就長了,佔用的回滾段也大。不小心執行失敗,又要全部回滾。有沒有更好的方法,加hint 如nologging append, 甚至parallel。這些我認為都不是很好的方法,有有一種方法,稱之為bulk collect into 。使用它可以減少操作時間。這是基於資料塊的插入方式。
目標表的結構如下:
create table t_target (id number, owner varchar2(30), object_name varchar2(128), object_id number, xx date, yy varchar2(10) )
源表為dba_objects ,表結構在資料庫中可以查到。
需要將源表dba_objects 中兩個欄位object_name,owner 兩個欄位的值複製到t_target 表中。
Fastestdeclare type t_array is table of t_target%rowtype; t_data t_array; cursor c is select null id, owner, object_name, null object_id, null xx, null yy from dba_objects; begin open c; loop fetch c bulk collect into t_data limit 100; forall i in 1 .. t_data.count insert into t_target values t_data (i); exit when c%notfound; end loop; close c; commit; end;
1.Explicit cursors doing a BULK COLLECT into a local collection (with an appropriate LIMIT) and using FORALL to write back to the database.
2.Implicit cursors doing a BULK COLLECT for you behind the scenes along with single-row writes back to the datbase.
3.Explicit cursors that are not doing a BULK COLLECT and not taking advantage of PL/SQL collections.