高效的PL SQL程式設計 批量處理

2021-08-31 05:09:22 字數 1583 閱讀 3493

批量處理一般用在etl操作, etl代表提取(extract),轉換(transform),裝載(load), 是乙個資料倉儲的詞彙!

類似於下面的結構:

for x (select * from...)

loop

process data;

insert into table values(...);

end loop;

一般情況下, 我們處理大筆的資料插入動作, 有2種做法, 第一種就是一筆筆的迴圈插入

create table t1 as select * from user_tables where 1=0;

create table t2 as select * from user_tables where 1=0;

create table t3 as select table_name from user_tables where 1=0;

create or replace procedure nor_test

asbegin

for x in(select * from user_tables)

loop

insert into t1 values x;

end loop;

end;

第2種方法就是批量處理(insert全部字段):

create or replace procedure bulk_test1(p_array_size in number)

astype array is table of user_tables%rowtype;

l_data array;

cursor c is select * from user_tables;

begin

open c;

loop

fetch c bulk collect into l_data limit p_array_size;

forall i in 1..l_data.count

insert into t2 values l_data(i);

exit when c%notfound;

end loop;

end;

insert部分字段:

create or replace procedure bulk_test2(p_array_size in number)

asl_tablename dbms_sql.varchar2_table;

cursor c is select table_name from user_tables;

begin

open c;

loop

fetch c bulk collect into l_tablename limit p_array_size;

forall i in 1..l_tablename.count

insert into t3 values (l_tablename(i));

exit when c%notfound;

end loop;

end;

在效能方面批量處理有著很大的優勢, p_array_size一般預設都是100

大資料多執行緒高效批量處理

工作中遇到的場景,這裡寫個例子出來,實際應用比此處更為健壯和完善 應用場景 對一張表10萬條資料 或100萬或1億 進行更新操作或寫入操作 菜鳥是一條一條的執行吧,這顯然不行啊 我在實際專案中是這樣應用的,批量更新!當然這顯然是不夠的 要執行緒批量更新才對吧!怎麼做呢?舉例1 10萬條資料 我100...

PL SQL程式設計學習之異常處理

新增外來鍵關聯 alter table dept learn add constraint pk dept deptid primary key department id alter table emp learn add constraint fk emp dept deptid foreign...

PLSQL 的異常處理

任何一種程式語言中的異常處理部分都是比較重要的一部分,單獨學習一下。一 異常的種類及基本用法 1 預定義異常 總計21種,具體見文件 使用方法 begin select select select exception when no data found then 常用型別 no data foun...