對於大表批量操作建議用遊標操作.
如對以下大表的操作,直接update耗時約16min;運用遊標後耗時3min左右
注意:遊標打開後,服務器會專門爲遊標分配一定的內存空間存放遊標操作的數據結果集,同時使用遊標也會對某些數據進行封鎖。所以遊標一旦用過,應及時關閉,避免服務器資源浪費。
關於遊標的具體使用請移步參考:https://www.cnblogs.com/jdzhang/p/7576520.html
declare
po_success integer;
po_info varchar2(2000);
cursor cur_cdr is
--查詢目標數據
SELECT ID FROM LISTING_QUEUE_LAZ where SALE_SITE='SGAMZ';
type type_listing_queue_laz_id is table of LISTING_QUEUE_LAZ.ID%type;
table_listing_queue_laz_id type_listing_queue_laz_id;
ln_cnt number := 0;
begin
open cur_cdr;
loop
fetch cur_cdr bulk collect
into table_listing_queue_laz_id limit 10000;
ln_cnt := ln_cnt + table_listing_queue_laz_id.count;
-- forall i in 1 .. table_rowid.count
For i In 1 .. table_listing_queue_laz_id.Count loop
--執行目標操作
UPDATE LISTING_QUEUE_LAZ SET SALE_SITE='SG' WHERE ID=table_listing_queue_laz_id(i);
ln_cnt := ln_cnt - 1;
end loop;
commit;
exit when cur_cdr%notfound or cur_cdr%notfound is null;
end loop;
--使用結束應立即關閉
close cur_cdr;
commit;
--打印日誌
po_success := 0;
po_info := '執行成功';
-- dbms_output.put_line('執行結果: code:' || po_success || ' msg:' || po_info);
exception
when others then
if cur_cdr%isopen then
close cur_cdr;
end if;
rollback;
po_success := 1;
po_info := '執行失敗,失敗信息爲:' || sqlerrm;
-- dbms_output.put_line('執行結果: code:' || po_success || ' msg:' || po_info);
end;