日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Oracle 中使用 fetch bulk collect into 批量效率的读取游标数据

發(fā)布時(shí)間:2023/12/13 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Oracle 中使用 fetch bulk collect into 批量效率的读取游标数据 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
通常我們獲取游標(biāo)數(shù)據(jù)是用 fetch some_cursor into var1, var2 的形式,當(dāng)游標(biāo)中的記錄數(shù)不多時(shí)不打緊。然而自 Oracle 8i 起,Oracle 為我們提供了 fetch bulk collect 來(lái)批量取游標(biāo)中的數(shù)據(jù)。它能在讀取游標(biāo)中大量數(shù)據(jù)的時(shí)候提高效率,就像 SNMP 協(xié)議中,V2 版比 V1 版新加了 GET-BULK PDU 一樣,也是用來(lái)更高效的批量取設(shè)備上的節(jié)點(diǎn)值。

  fetch bulk collect into 的使用格式是:fetch some_cursor collect into col1, col2 limit xxx。col1、col2 是聲明的集合類型變量,xxx 為每次取數(shù)據(jù)塊的大小(記錄數(shù)),相當(dāng)于緩沖區(qū)的大小,可以不指定 limit xxx 大小。下面以實(shí)際的例子來(lái)說(shuō)明它的使用,并與逐條取記錄的 fetch into 執(zhí)行效率上進(jìn)行比較。測(cè)試環(huán)境是 Oracle 10g  10.2.1.0,查詢的聯(lián)系人表 sr_contacts 中有記錄數(shù) 1802983 條,游標(biāo)中以 rownum 限定返回的記錄數(shù)。

  使用 fetch bulk collect into 獲取游標(biāo)數(shù)據(jù)

  declare    

   --聲明需要集合類型及變量,參照字段的 type 來(lái)聲明類型      

  type id_type is table of sr_contacts.sr_contact_id%type;     
  v_id id_type;     
       
  type phone_type is table of sr_contacts.contact_phone%type;     
  v_phone phone_type;     
       
  type remark_type is table of sr_contacts.remark%type;     
  v_remark remark_type;   

?

?

   cursor all_contacts_cur is --用 rownum 來(lái)限定取出的記錄數(shù)來(lái)測(cè)試

?select sr_contact_id,contact_phone,remark from sr_contacts where rownum <= 100000;      

begin    
         
    open all_contacts_cur;     
    loop     
        fetch all_contacts_cur bulk collect into v_id,v_phone,v_remark limit 256;     
        for i in 1..v_id.count loop --遍歷集合     
            --用 v_id(i)/v_phone(i)/v_remark(i) 取出字段值來(lái)執(zhí)行你的業(yè)務(wù)邏輯        
        end loop;     
        exit when all_contacts_cur%notfound; --exit 不能緊接 fetch 了,不然會(huì)漏記錄     
    end loop;     
    close all_contacts_cur;     
end;   
declare  

?

   --聲明需要集合類型及變量,參照字段的 type 來(lái)聲明類型   

  type id_type is table of sr_contacts.sr_contact_id%type;  
  v_id id_type;  
    
  type phone_type is table of sr_contacts.contact_phone%type;  
  v_phone phone_type;  
    
  type remark_type is table of sr_contacts.remark%type;  
  v_remark remark_type; 

?

?

   cursor all_contacts_cur is --用 rownum 來(lái)限定取出的記錄數(shù)來(lái)測(cè)試

?select sr_contact_id,contact_phone,remark from sr_contacts where rownum <= 100000;   

begin 
    open all_contacts_cur;  
    loop  
        fetch all_contacts_cur bulk collect into v_id,v_phone,v_remark limit 256;  
        for i in 1..v_id.count loop --遍歷集合  
            --用 v_id(i)/v_phone(i)/v_remark(i) 取出字段值來(lái)執(zhí)行你的業(yè)務(wù)邏輯  
        end loop;  
        exit when all_contacts_cur%notfound; --exit 不能緊接 fetch 了,不然會(huì)漏記錄  
    end loop;  
    close all_contacts_cur;  
end; 

  使用 fetch into 逐行獲取游標(biāo)數(shù)據(jù)

  declare 

   --聲明變量,參照字段的 type 來(lái)聲明類型   

  v_id sr_contacts.sr_contact_id%type;  
  v_phone sr_contacts.contact_phone%type;  
  v_remark sr_contacts.remark%type;   

?

   cursor all_contacts_cur is  --用 rownum 來(lái)限定取出的記錄數(shù)來(lái)測(cè)試   

     select sr_contact_id,contact_phone,remark from sr_contacts where rownum <= 100000;   

begin 
      
    open all_contacts_cur;  
    loop  
        fetch all_contacts_cur into v_id,v_phone,v_remark;  
        exit when all_contacts_cur%notfound;      
        --用 v_id/v_phone/v_remark 取出字段值來(lái)執(zhí)行你的業(yè)務(wù)邏輯  
        null; --這里只放置一個(gè)空操作,只為測(cè)試循環(huán)取數(shù)的效率  
    end loop;  
    close all_contacts_cur;  
end; 
declare

?v_id sr_contacts.sr_contact_id%type;
  v_phone sr_contacts.contact_phone%type;
  v_remark sr_contacts.remark%type;

   cursor all_contacts_cur is  --用 rownum 來(lái)限定取出的記錄數(shù)來(lái)測(cè)試

     select sr_contact_id,contact_phone,remark from sr_contacts where rownum <= 100000;

begin
   
    open all_contacts_cur;
    loop
        fetch all_contacts_cur into v_id,v_phone,v_remark;
        exit when all_contacts_cur%notfound;   
        --用 v_id/v_phone/v_remark 取出字段值來(lái)執(zhí)行你的業(yè)務(wù)邏輯
        null; --這里只放置一個(gè)空操作,只為測(cè)試循環(huán)取數(shù)的效率
    end loop;
    close all_contacts_cur;
end;

  執(zhí)行性能比較

  看看測(cè)試的結(jié)果,分別執(zhí)行五次所耗費(fèi)的秒數(shù):

  當(dāng) rownum <= 100000 時(shí):

  fetch bulk collect into 耗時(shí):0.125秒, 0.125秒, 0.125秒, 0.125秒, 0.141秒

  fetch into 耗時(shí):      1.266秒, 1.250秒, 1.250秒, 1.250秒, 1.250秒

  當(dāng) rownum <= 1000000 時(shí):

  fetch bulk collect into 耗時(shí):1.157秒, 1.157秒, 1.156秒, 1.156秒, 1.171秒

  fetch into 耗時(shí):      12.128秒, 12.125秒, 12.125秒, 12.109秒, 12.141秒

  當(dāng) rownum <= 10000 時(shí):

  fetch bulk collect into 耗時(shí):0.031秒, 0.031秒, 0.016秒, 0.015秒, 0.015秒

fetch into 耗時(shí):                 0.141秒, 0.140秒, 0.125秒, 0.141秒, 0.125秒

  當(dāng) rownum <= 1000 時(shí):

  fetch bulk collect into 耗時(shí):0.016秒, 0.015秒, 0.016秒, 0.016秒, 0.015秒

  fetch into 耗時(shí):      0.016秒, 0.031秒, 0.031秒, 0.032秒, 0.015秒

  從測(cè)試結(jié)果來(lái)看游標(biāo)的記錄數(shù)越大時(shí),用 fetch bulk collect into 的效率很明顯示,趨于很小時(shí)就差不多了。

  注意了沒(méi)有,前面使用 fetch bulk collect into 時(shí)前為每一個(gè)查詢列都定義了一個(gè)集合,這樣有些繁瑣。我們之前也許用過(guò)表的 %rowtype 類型,同樣的我們也可以定義表的 %rowtype 的集合類型。看下面的例子,同時(shí)在這個(gè)例子中,我們借助于集合的 first、last 屬性來(lái)代替使用 count  屬性來(lái)進(jìn)行遍歷。

  declare 

   --聲明需要集合類型及變量,參照字段的 type 來(lái)聲明類型   

  type contacts_type is table of sr_contacts%rowtype;  
  v_contacts contacts_type;   

?

   cursor all_contacts_cur is --用 rownum 來(lái)限定取出的記錄數(shù)來(lái)測(cè)試   

     select * from sr_contacts where rownum <= 10000;   

begin 
      
    open all_contacts_cur;  
    loop  
        fetch all_contacts_cur bulk collect into v_contacts limit 256;  
        for i in v_contacts.first .. v_contacts.last loop --遍歷集合  
            --用 v_contacts(i).sr_contact_id/v_contacts(i).contact_phone/v_contacts(i).remark  
            --的形式來(lái)取出各字段值來(lái)執(zhí)行你的業(yè)務(wù)邏輯    
        end loop;  
        exit when all_contacts_cur%notfound;  
    end loop;  
    close all_contacts_cur;  
end; 
declare

--聲明需要集合類型及變量,參照字段的 type 來(lái)聲明類型

  type contacts_type is table of sr_contacts%rowtype;
  v_contacts contacts_type;

?

   cursor all_contacts_cur is --用 rownum 來(lái)限定取出的記錄數(shù)來(lái)測(cè)試

     select * from sr_contacts where rownum <= 10000;

begin
   
    open all_contacts_cur;
    loop
        fetch all_contacts_cur bulk collect into v_contacts limit 256;
        for i in v_contacts.first .. v_contacts.last loop --遍歷集合
            --用 v_contacts(i).sr_contact_id/v_contacts(i).contact_phone/v_contacts(i).remark
            --的形式來(lái)取出各字段值來(lái)執(zhí)行你的業(yè)務(wù)邏輯
        end loop;
        exit when all_contacts_cur%notfound;
    end loop;
    close all_contacts_cur;
end;

  關(guān)于 limit 參數(shù)

  你可以根據(jù)你的實(shí)際來(lái)調(diào)整 limit 參數(shù)的大小,來(lái)達(dá)到你最優(yōu)的性能。limit 參數(shù)會(huì)影響到 pga 的使用率。而且也可以在 fetch bulk 中省略 limit 參數(shù),寫(xiě)成

fetch all_contacts_cur bulk collect into v_contacts;

  有些資料中是說(shuō),如果不寫(xiě) limit 參數(shù),將會(huì)以數(shù)據(jù)庫(kù)的 arraysize  參數(shù)值作為默認(rèn)值。在 sqlplus 中用 show arraysize  可以看到該值默認(rèn)為 15,set arraysize 256 可以更改該值。而實(shí)際上我測(cè)試不帶 limit 參數(shù)時(shí),外層循環(huán)只執(zhí)行了一輪,好像不是 limit 15,所以不寫(xiě) limit 參數(shù)時(shí),可以去除外層循環(huán),begin-end 部分可寫(xiě)成:

?

begin 
    open all_contacts_cur;  
    fetch all_contacts_cur bulk collect into v_contacts;  
    for i in v_contacts.first .. v_contacts.last loop --遍歷集合  
        --用 v_contacts(i).sr_contact_id/v_contacts(i).contact_phone/v_contacts(i).remark  
        --的形式來(lái)取出各字段值來(lái)執(zhí)行你的業(yè)務(wù)邏輯  
        null; --這里只放置一個(gè)空操作,只為測(cè)試循環(huán)取數(shù)的效率  
        dbms_output.put_line(2000);  
    end loop;  
    close all_contacts_cur;  
end; 
begin
    open all_contacts_cur;
    fetch all_contacts_cur bulk collect into v_contacts;
    for i in v_contacts.first .. v_contacts.last loop --遍歷集合
        --用 v_contacts(i).sr_contact_id/v_contacts(i).contact_phone/v_contacts(i).remark
        --的形式來(lái)取出各字段值來(lái)執(zhí)行你的業(yè)務(wù)邏輯
        null; --這里只放置一個(gè)空操作,只為測(cè)試循環(huán)取數(shù)的效率
        dbms_output.put_line(2000);
    end loop;
    close all_contacts_cur;
end;

  bulk collect 的其他用法(總是針對(duì)集合)

  select into 語(yǔ)句中,如:

SELECT sr_contact_id,contact_phone BULK COLLECT INTO v_id,v_phone
     FROM sr_contacts WHERE ROWNUM <= 100;
dbms_output.put_line('Count:'||v_id.count||', First:'||v_id(1)||'|'||v_phone(1));

?

  returning into 語(yǔ)句中,如:

DELETE FROM sr_contacts WHERE sr_contact_id < 30
    RETURNING sr_contact_id, contact_phone BULK COLLECT INTO v_id, v_phone;
dbms_output.put_line('Count:'||v_id.count||', First:'||v_id(1)||'|'||v_phone(1));

  forall 的 bulk dml 操作,它大大優(yōu)于 for 集合后的操作

fetch all_contacts_cur bulk collect into v_contacts;
forall i in 1 .. v_contacts.count
--forall i in v_contacts.first .. v_contacts.last  
--forall i in indices of v_contacts --10g以上,可以是非連續(xù)的集合  
insert into sr_contacts(sr_contact_id,contact_phone,remark)
    values(v_contacts(i).sr_contact_id,v_contacts(i).contact_phone,v_contacts(i).remark); 

   --或者是單條的 delete/update 操作

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/zndavid/archive/2009/06/10/1500323.html

總結(jié)

以上是生活随笔為你收集整理的Oracle 中使用 fetch bulk collect into 批量效率的读取游标数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。