fetch bulk collect into 批量效率的读取游标数据 【转载】

来源:互联网 发布:湘北vs山王数据 编辑:程序博客网 时间:2024/06/06 04:30
sql] view plaincopy
  1. 添加自己测试的一个选项,判断collect是否为空,否则打开的时候汇报06502的异常。  
  2.   
  3. 在9i的版本中说是报0331的错误,地址如下:“BULK COLLECT读取含空日期字段的BUG”,我是在10.2.0.4的版本中测试的,没有出现这种情况。  
  4. http://dev.21tx.com/2005/05/20/12202.html  
  5.   
  6. create table T_TEST(  ID    INTEGER,  TNAME VARCHAR2(20));  
  7. insert into t_test(id,tname) values(1,'aa');  
  8. insert into t_test(id,tname) values(2,'aa');  
  9. insert into t_test(id,tname) values(3,'aa');  
  10. insert into t_test(id,tname) values(4,'aa');  
  11. insert into t_test(id,tname) values(null,'aa1');  
  12. insert into t_test(id,tname) values(6,null);  
  13. insert into t_test(id,tname) values(null,null);  
  14. commit;  
  15.   
  16.   
  17. declare   
  18.   -- Local variables here  
  19.     TYPE TYPE_ID IS TABLE OF t_test.id%TYPE INDEX BY BINARY_INTEGER;  
  20.     TYPE TYPE_NAME IS TABLE OF t_test.tname%TYPE INDEX BY BINARY_INTEGER;  
  21.     c_id TYPE_ID;  
  22.     c_name TYPE_name;  
  23.       
  24. begin  
  25.   -- Test statements here  
  26.   SELECT * BULK COLLECT  
  27.   into c_id,c_name from t_test where rownum<1;  
  28.     
  29.   if c_id.count>=1 then  
  30.   FOR N IN c_id.FIRST .. c_id.LAST LOOP   
  31.    
  32.    dbms_output.put_line('id='||c_id(n)||',name='||c_name(n));  
  33.   end loop;  
  34.   end if;  
  35. end;  
  36.   
  37.   
  38.   
  39. -- Created on 2013/8/13 by ZHOUXX   
  40. declare   
  41.   -- Local variables here  
  42.   i integer;  
  43.     TYPE TYPE_md5msg IS TABLE OF number INDEX BY varchar2(64);  
  44.   col_md5msg TYPE_md5msg;  
  45. begin  
  46.   -- Test statements here  
  47.                 for cur in (select md5(alertname || ',' || alertkey || ',' || msg) idx,  
  48.                                  cnt  
  49.                             from (select alertname,  
  50.                                          alertkey,  
  51.                                          msg,  
  52.                                          trunc(sysdate) dt,  
  53.                                          0 cnt  
  54.                                     from t_rover_alert  
  55.                                    where datetime >= trunc(sysdate - 3)  
  56.                                      and alertlevel in (1, 2, 3, 4, 5, 7) and rownum=1  
  57.                                    group by alertname, alertkey, msg)) loop  
  58.               col_md5msg(cur.idx) :=cur.cnt;  
  59.               end loop;  
  60.               dbms_output.put_line('111='||col_md5msg.count);  
  61.               if col_md5msg.exists('00'then                
  62.               dbms_output.put_line('222='||col_md5msg('00'));  
  63.               end if;  
  64.                 
  65.               col_md5msg('00') :=10;  
  66.               if col_md5msg.exists('00'then                
  67.               dbms_output.put_line('333='||col_md5msg('00'));  
  68.               end if;  
  69.                 
  70.               col_md5msg('00') :=col_md5msg('00')+1;  
  71.               if col_md5msg.exists('00'then                
  72.               dbms_output.put_line('333-1='||col_md5msg('00'));  
  73.               end if;  
  74.                 
  75.                 
  76.               dbms_output.put_line('444='||col_md5msg.count);  
  77.               col_md5msg.delete;  
  78.               dbms_output.put_line('555='||col_md5msg.count);  
  79.                              
  80. end;  
  81.   
  82.   
  83. drop table tttt purge;  
  84. create table tttt(idx number,id number(1),id1 number(1,0),id2 number(1,1));  
  85.   
  86. insert into tttt(idx,id,id1,id2) values(1,1,null,null);  
  87. insert into tttt(idx,id,id1,id2) values(2,null,1,null);  
  88. insert into tttt(idx,id,id1,id2) values(3,null,null,1);  
  89.   
  90.   
  91. insert into tttt(idx,id,id1,id2) values(4,1.1,null,null);  
  92. insert into tttt(idx,id,id1,id2) values(5,null,1.1,null);  
  93. insert into tttt(idx,id,id1,id2) values(6,null,null,1.1);  
  94.   
  95.   
  96. insert into tttt(idx,id,id1,id2) values(7,0.1,null,null);  
  97. insert into tttt(idx,id,id1,id2) values(8,null,0.1,null);  
  98. insert into tttt(idx,id,id1,id2) values(9,null,null,0.1);  
  99.   
  100.   
  101. insert into tttt(idx,id,id1,id2) values(10,10,null,null);  
  102. insert into tttt(idx,id,id1,id2) values(11,null,10,null);  
  103. insert into tttt(idx,id,id1,id2) values(12,null,null,10);  
  104.   
  105.   
  106. select * from tttt;  
  107.   
  108.   
  109. fetch bulk collect into 批量效率的读取游标数据 【转载】  
  110. 通常我们获取游标数据是用 fetch some_cursor into var1, var2 的形式,当游标中的记录数不多时不打紧。然而自 Oracle 8i 起,Oracle 为我们提供了 fetch bulk collect 来批量取游标中的数据。它能在读取游标中大量数据的时候提高效率,就像 SNMP 协议中,V2 版比 V1 版新加了 GET-BULK PDU 一样,也是用来更高效的批量取设备上的节点值。  
  111.   fetch bulk collect into 的使用格式是:fetch some_cursor collect into col1, col2 limit xxx。col1、col2 是声明的集合类型变量,xxx 为每次取数据块的大小(记录数),相当于缓冲区的大小,可以不指定 limit xxx 大小。下面以实际的例子来说明它的使用,并与逐条取记录的 fetch into 执行效率上进行比较。测试环境是 Oracle 10g  10.2.1.0,查询的联系人表 sr_contacts 中有记录数 1802983 条,游标中以 rownum 限定返回的记录数。  
  112.   
  113.   使用 fetch bulk collect into 获取游标数据  
  114.   
  115. Java代码  
  116.   
  117.     declare         
  118.          --声明需要集合类型及变量,参照字段的 type 来声明类型          
  119.          type id_type is table of sr_contacts.sr_contact_id%type;        
  120.          v_id id_type;                  
  121.          type phone_type is table of sr_contacts.contact_phone%type;       
  122.          v_phone phone_type;                  
  123.          type remark_type is table of sr_contacts.remark%type;          
  124.          v_remark remark_type;       
  125.         
  126.        cursor all_contacts_cur is select sr_contact_id,contact_phone,remark from sr_contacts where rownum <= 100000;           
  127.         
  128.     begin         
  129.         open all_contacts_cur;          
  130.         loop          
  131.             fetch all_contacts_cur bulk collect into v_id,v_phone,v_remark limit 256;          
  132.             for i in 1..v_id.count loop --遍历集合          
  133.          --用 v_id(i)/v_phone(i)/v_remark(i) 取出字段值来执行你的业务逻辑             
  134.             end loop;          
  135.             exit when all_contacts_cur%notfound; --exit 不能紧接 fetch 了,不然会漏记录          
  136.         end loop;          
  137.         close all_contacts_cur;          
  138.     end;        
  139.   
  140.   
  141.    
  142.   
  143.   使用 fetch into 逐行获取游标数据  
  144.   
  145.     declare      
  146.         
  147.         --声明变量,参照字段的 type 来声明类型       
  148.          v_id sr_contacts.sr_contact_id%type;       
  149.          v_phone sr_contacts.contact_phone%type;       
  150.          v_remark sr_contacts.remark%type;        
  151.         
  152.        cursor all_contacts_cur is select sr_contact_id ,contact_phone,remark from sr_contacts where rownum <= 100000;       
  153.         
  154.     begin      
  155.                
  156.         open all_contacts_cur;       
  157.         loop       
  158.             fetch all_contacts_cur into v_id,v_phone,v_remark;       
  159.             exit when all_contacts_cur%notfound;           
  160.             --用 v_id/v_phone/v_remark 取出字段值来执行你的业务逻辑       
  161.             null--这里只放置一个空操作,只为测试循环取数的效率       
  162.         end loop;       
  163.         close all_contacts_cur;       
  164.     end;    
  165.   
  166.    
  167.   
  168.   执行性能比较  
  169.   
  170.   看看测试的结果,分别执行五次所耗费的秒数:  
  171.   
  172.   当 rownum <= 100000 时:  
  173.   
  174.   fetch bulk collect into 耗时:0.125秒, 0.125秒, 0.125秒, 0.125秒, 0.141秒  
  175.   
  176.   fetch into 耗时:      1.266秒, 1.250秒, 1.250秒, 1.250秒, 1.250秒  
  177.   
  178.   当 rownum <= 1000000 时:  
  179.   
  180.   fetch bulk collect into 耗时:1.157秒, 1.157秒, 1.156秒, 1.156秒, 1.171秒  
  181.   
  182.   fetch into 耗时:      12.128秒, 12.125秒, 12.125秒, 12.109秒, 12.141秒  
  183.   
  184.   当 rownum <= 10000 时:  
  185.   
  186.   fetch bulk collect into 耗时:0.031秒, 0.031秒, 0.016秒, 0.015秒, 0.015秒  
  187.   
  188. fetch into 耗时:                 0.141秒, 0.140秒, 0.125秒, 0.141秒, 0.125秒  
  189.   
  190.   当 rownum <= 1000 时:  
  191.   
  192.   fetch bulk collect into 耗时:0.016秒, 0.015秒, 0.016秒, 0.016秒, 0.015秒  
  193.   
  194.   fetch into 耗时:      0.016秒, 0.031秒, 0.031秒, 0.032秒, 0.015秒  
  195.   
  196.   从测试结果来看游标的记录数越大时,用 fetch bulk collect into 的效率很明显示,趋于很小时就差不多了。  
  197.   
  198.   注意了没有,前面使用 fetch bulk collect into 时前为每一个查询列都定义了一个集合,这样有些繁琐。我们之前也许用过表的 %rowtype 类型,同样的我们也可以定义表的 %rowtype 的集合类型。看下面的例子,同时在这个例子中,我们借助于集合的 firstlast 属性来代替使用 count  属性来进行遍历。  
  199.   
  200.     declare      
  201.        --声明需要集合类型及变量,参照字段的 type 来声明类型       
  202.       type contacts_type is table of sr_contacts%rowtype;       
  203.       v_contacts contacts_type;       
  204.        cursor all_contacts_cur is --用 rownum 来限定取出的记录数来测试       
  205.          select * from sr_contacts where rownum <= 10000;       
  206.         
  207.     begin      
  208.                
  209.         open all_contacts_cur;       
  210.         loop       
  211.             fetch all_contacts_cur bulk collect into v_contacts limit 256;       
  212.             for i in v_contacts.first .. v_contacts.last loop --遍历集合       
  213.                 --用 v_contacts(i).sr_contact_id/v_contacts(i).contact_phone/v_contacts(i).remark       
  214.                 --的形式来取出各字段值来执行你的业务逻辑         
  215.             end loop;       
  216.             exit when all_contacts_cur%notfound;       
  217.         end loop;       
  218.         close all_contacts_cur;       
  219.     end;      
  220.   
  221.   
  222.     
  223. 关于 limit 参数  
  224.   
  225.   你可以根据你的实际来调整 limit 参数的大小,来达到你最优的性能。limit 参数会影响到 pga 的使用率。而且也可以在 fetch bulk 中省略 limit 参数,写成  
  226.   
  227. fetch all_contacts_cur bulk collect into v_contacts;  
  228.   
  229.   有些资料中是说,如果不写 limit 参数,将会以数据库的 arraysize  参数值作为默认值。在 sqlplus 中用 show arraysize  可以看到该值默认为 15,set arraysize 256 可以更改该值。而实际上我测试不带 limit 参数时,外层循环只执行了一轮,好像不是 limit 15,所以不写 limit 参数时,可以去除外层循环,begin-end 部分可写成:  
  230.   
  231.   
  232.   
  233.     begin      
  234.         open all_contacts_cur;       
  235.         fetch all_contacts_cur bulk collect into v_contacts;       
  236.         for i in v_contacts.first .. v_contacts.last loop --遍历集合       
  237.             --用 v_contacts(i).sr_contact_id/v_contacts(i).contact_phone/v_contacts(i).remark       
  238.             --的形式来取出各字段值来执行你的业务逻辑       
  239.             null--这里只放置一个空操作,只为测试循环取数的效率       
  240.             dbms_output.put_line(2000);       
  241.         end loop;       
  242.         close all_contacts_cur;       
  243.     end;     
  244.   
  245.   
  246.   
  247.   
  248. bulk collect 的其他用法(总是针对集合)  
  249.   
  250.   select into 语句中,如:  
  251.   
  252. SELECT sr_contact_id,contact_phone BULK COLLECT INTO v_id,v_phone  
  253.      FROM sr_contacts WHERE ROWNUM <= 100;  
  254. dbms_output.put_line('Count:'||v_id.count||', First:'||v_id(1)||'|'||v_phone(1));  
  255.   
  256.   returning into 语句中,如:  
  257.   
  258. DELETE FROM sr_contacts WHERE sr_contact_id < 30  
  259.     RETURNING sr_contact_id, contact_phone BULK COLLECT INTO v_id, v_phone;  
  260. dbms_output.put_line('Count:'||v_id.count||', First:'||v_id(1)||'|'||v_phone(1));  
  261.   
  262.   forall 的 bulk dml 操作,它大大优于 for 集合后的操作  
  263.   
  264. fetch all_contacts_cur bulk collect into v_contacts;  
  265. forall i in 1 .. v_contacts.count  
  266. --forall i in v_contacts.first .. v_contacts.last    
  267. --forall i in indices of v_contacts --10g以上,可以是非连续的集合    
  268. insert into sr_contacts(sr_contact_id,contact_phone,remark)  
  269.     values(v_contacts(i).sr_contact_id,v_contacts(i).contact_phone,v_contacts(i).remark);   
  270.   
  271.    --或者是单条的 delete/update 操作 
0 0