Oracle 存储过程 遍历 游标 数据集 效率比较

Oracle中游标使用效率比较

2013年09月26日 21:09:08 進擊的胖蛇 阅读数:4380

鸣谢:http://blog.163.com/gaoyutong122@126/blog/static/344697322012725344964/

扩展:http://www.cnblogs.com/rootq/archive/2008/11/17/1335491.html

批量SQL之 BULK COLLECT 子句http://blog.csdn.net/leshami/article/details/7545597

对300万一张表数据,用游标进行循环,不同写法的效率比较
 
1、显式游标
declare
    cursor cur_2 is select a.cust_name from ea_cust.cust_info a;
    cust_id varchar2(100);
    
begin
    open cur_2;
    loop
        fetch cur_2 into cust_id;
        exit when cur_2%notfound;
        NULL;
    end loop;
    close cur_2;
end;
 
--耗时48秒
 
2、隐式游标
declare
begin
    for cur_ in (select c.cust_name from ea_cust.cust_info c) loop
    
       NULL;  www.2cto.com 
    
    end loop;
 
end;
 
--耗时16秒
 
3、bulk collect into + cursor
 
declare
    cursor cur_3 is select a.cust_name from ea_cust.cust_info a;
    type t_table is table of varchar2(100);
    c_table t_table;
    to_cust_id varchar2(100); 
begin
    open cur_3;
    loop
         fetch cur_3 bulk collect into c_table limit 100;
         exit when c_table.count = 0;
         for i in c_table.first..c_table.last loop
             null;
             
         end loop;         
    end loop;
    commit;
end;
 
--耗时13秒,看样子这种最快

https://blog.csdn.net/Hollboy/article/details/12068045

Oracle中IS TABLE OF的使用

2017年04月23日 18:33:12 pan_junbiao 阅读数:9804

 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pan_junbiao/article/details/70537582

IS TABLE OF :指定是一个集合的表的数组类型,简单的来说就是一个可以存储一列多行的数据类型。

INDEX BY BINARY_INTEGER:指索引组织类型

BULK COLLECT :指是一个成批聚合类型,简单的来说 , 它可以存储一个多行多列存储类型,采用BULK COLLECT可以将查询结果一次性地加载到集合中。

【实例】在SCOTT模式下,使用IS TABLE OF获取所有员工的姓名,职务,工资信息。

 
  1. declare

  2. type type_ename is table of emp.ename%type;

  3. type type_job is table of emp.job%type;

  4. type type_sal is table of emp.sal%type;

  5.  
  6. var_ename type_ename:=type_ename();

  7. var_job type_job:=type_job();

  8. var_sal type_sal:=type_sal();

  9. begin

  10. select ename,job,sal

  11. bulk collect into var_ename,var_job,var_sal

  12. from emp;

  13.  
  14. /*输出雇员信息*/

  15. for v_index in var_ename.first .. var_ename.last loop

  16. dbms_output.put_line('雇员名称:'||var_ename(v_index)||' 职务:'||var_job(v_index)||' 工资:'||var_sal(v_index));

  17. end loop;

  18. end;

【实例】在SCOTT模式下,使用IS TABLE OF获取所有员工的所有信息。

 
  1. declare

  2. type emp_table_type is table of emp%rowtype index by binary_integer;

  3. var_emp_table emp_table_type;

  4. begin

  5. select *

  6. bulk collect into var_emp_table

  7. from emp;

  8.  
  9. /*输出雇员信息*/

  10. for i in 1..var_emp_table.COUNT loop

  11. dbms_output.put_line('雇员名称:'||var_emp_table(i).ename||' 职务:'||var_emp_table(i).job||' 工资:'||var_emp_table(i).sal);

  12. end loop;

  13. end;

https://blog.csdn.net/pan_junbiao/article/details/70537582

猜你喜欢

转载自blog.csdn.net/xuheng8600/article/details/85253689