批量插入,update

2022-06-11 19:42:12 字數 1943 閱讀 9326

#####setting 1

create table t as select * from all_objects where 1 =2;

###.模擬逐行提交的情況,注意觀察執行時間

declare

begin

for cur in (select * from t_ref) loop

insert into t values cur;

commit;

end loop;

end;

/###模擬批量提交

declare

v_count number;

begin

for cur in (select * from t_ref) loop

insert into t values cur;

v_count := v_count + 1;

if v_count >= 100 then

commit;

v_count :=0;

end if;

end loop;

commit;

end;

/更高階的方法,體驗一下極限速度。

declare

cursor cur is

select * from t_ref where column=; <-就按照條件篩選資料,

type rec is table of t_ref%rowtype;

recs rec;

begin

open cur;

while (true) loop

fetch cur bulk collect

into recs limit 100;

forall i in 1 .. recs.count

insert into t values recs (i);

commit;

exit when cur%notfound;

end loop;

close cur;

end;

/#####setting 2:

--用 rownum 來限定取出的記錄數來測試

cursor all_contacts_cur is    

selectsr_contact_id,contact_phone,remark from sr_contacts where rownum <= 100000;   

###seting 3:

要麼就按照條件篩選資料,分別提交

如:insert into table_a select * from table_b where type=1;

commit;

insert into table_a select * from table_b where type=2;

commit;

就是舉個例子,最後的type要是個全集

#####setting 3:

######update

declare

i int;--定義變數

v_count int;--定義變數

v_loop int;--定義變數

begin

select count(*) into v_count from test;--計算表內資料總數

select ceil(v_count/10) into v_loop from dual;--計算需要迴圈次數

i:=1;--為i賦值

while i<=v_loop loop--迴圈退出條件

update test set begintime=sysdate where begintime is null and rownum<=10;--執行更新

commit;--提交

i:=i+1;--i依次加1

end loop;--結束迴圈

end;

批量UPDATE的操作

有乙個簡單的業務需求,要根據另外乙個表的id去更新這張表的sys為scott 要更新 w記錄。sql create index t idx1 on t merge1 object id index created.sql create index idx t on t merge2 object i...

Oracle批量Update記錄

工作中經常用到oracle批量更新記錄,做為老手也怕出錯,總之要小心再小心,確保資料批量更新正確。下面舉乙個例子 1 建立兩張結構類似的表,建表語句如下 create table jayt1 id int,code varchar2 8 create table jayt2 id int,code ...

mysql批量update資料優化

有一張表goods中有20w條資料,現在需要把某個欄位的值做md5加密後更新。一條一條迴圈更新效能差不說,還容易造成資料庫阻塞。set time limit 0 ini set memory limit 1g mysqli new mysqli 127.0.0.1 root root wx 3306...