oracle bucket collect(批量查询)
2017-06-13 16:59
113 查看
采用bulk collect可以将查询结果一次性地加载到collections中,而不是通过cursor一条一条地处理。
可以在select into,fetch into,returning into语句使用bulk collect。
注意:在使用bulk collect时,所有的into变量都必须是collections。
create table t_test as
select object_id, object_name, object_type
from dba_objects
where wner = 'TEST';
1、在select into语句中使用bulk collect
declare
type object_list is table of t_test.object_name%type;
objs object_list;
begin
select object_name bulk collect
into objs
from t_test
where rownum <= 100;
for r in objs.first .. objs.last loop
dbms_output.put_line(' objs(r)=' || objs(r));
end loop;
end;
/
2、在fetch into中使用bulk collect
declare
type objecttab is table of t_test%rowtype;
objs objecttab;
cursor cob is
select object_id, object_name, object_type
from t_test
where rownum <= 10;
begin
open cob;
fetch cob bulk collect
into objs;
close cob;
for r in objs.first .. objs.last loop
dbms_output.put_line(' objs(r)=' || objs(r).object_name);
end loop;
end;
/
以上为把结果集一次fetch到collect中,我们还可以通过limit参数,来分批fetch数据,如下:
declare
type objecttab is table of t_test%rowtype;
objs objecttab;
cursor cob is
select object_id, object_name, object_type
from t_test
where rownum <= 10000;
begin
open cob;
loop
fetch cob bulk collect
into objs limit 1000;
exit when cob%notfound;
dbms_output.put_line('count:' || objs.count || ' first:' || objs.first ||
' last:' || objs.last);
for r in objs.first .. objs.last loop
dbms_output.put_line(' objs(r)=' || objs(r).object_name);
end loop;
end loop;
close cob;
end;
/
你可以根据实际来调整limit参数的大小,来达到最优的性能。limit参数会影响到PGA的使用率。
3、在returning into中使用bulk collect
declare
type id_list is table of t_test.object_id%type;
id
9408
s id_list;
type name_list is table of t_test.object_name%type;
names name_list;
begin
delete from t_test
where object_id <= 87510 returning object_id, object_name bulk collect into ids,
names;
dbms_output.put_line('deleted ' || sql%rowcount || ' rows:');
for i in ids.first .. ids.last loop
dbms_output.put_line('object #' || ids(i) || ': ' || names(i));
end loop;
end;
可以在select into,fetch into,returning into语句使用bulk collect。
注意:在使用bulk collect时,所有的into变量都必须是collections。
create table t_test as
select object_id, object_name, object_type
from dba_objects
where wner = 'TEST';
1、在select into语句中使用bulk collect
declare
type object_list is table of t_test.object_name%type;
objs object_list;
begin
select object_name bulk collect
into objs
from t_test
where rownum <= 100;
for r in objs.first .. objs.last loop
dbms_output.put_line(' objs(r)=' || objs(r));
end loop;
end;
/
2、在fetch into中使用bulk collect
declare
type objecttab is table of t_test%rowtype;
objs objecttab;
cursor cob is
select object_id, object_name, object_type
from t_test
where rownum <= 10;
begin
open cob;
fetch cob bulk collect
into objs;
close cob;
for r in objs.first .. objs.last loop
dbms_output.put_line(' objs(r)=' || objs(r).object_name);
end loop;
end;
/
以上为把结果集一次fetch到collect中,我们还可以通过limit参数,来分批fetch数据,如下:
declare
type objecttab is table of t_test%rowtype;
objs objecttab;
cursor cob is
select object_id, object_name, object_type
from t_test
where rownum <= 10000;
begin
open cob;
loop
fetch cob bulk collect
into objs limit 1000;
exit when cob%notfound;
dbms_output.put_line('count:' || objs.count || ' first:' || objs.first ||
' last:' || objs.last);
for r in objs.first .. objs.last loop
dbms_output.put_line(' objs(r)=' || objs(r).object_name);
end loop;
end loop;
close cob;
end;
/
你可以根据实际来调整limit参数的大小,来达到最优的性能。limit参数会影响到PGA的使用率。
3、在returning into中使用bulk collect
declare
type id_list is table of t_test.object_id%type;
id
9408
s id_list;
type name_list is table of t_test.object_name%type;
names name_list;
begin
delete from t_test
where object_id <= 87510 returning object_id, object_name bulk collect into ids,
names;
dbms_output.put_line('deleted ' || sql%rowcount || ' rows:');
for i in ids.first .. ids.last loop
dbms_output.put_line('object #' || ids(i) || ': ' || names(i));
end loop;
end;
相关文章推荐
- ORACLE游标、递归查询、子查询与批量更新示例
- oracle 批量查询DDL
- sqlserver移植为Oracle笔记(更新,新增字段名;批量新增记录;日期查询;截取字串函数)
- oracle创建定时job,job调用存储过程,批量查询,批量修改,自定义数据类型,
- Oracle批量插入和高效的查询分页
- OracleHelper(对增删改查分页查询操作进行了面向对象的封装,对批量增删改操作的事务封装)
- oracle查询树结构,根节点查询下边所有子节点,然后重新批量插入
- Oracle 的 bulk collect用法——批量查询
- oracle bucket collect(批量查询)
- Oracle批量查询、删除、更新使用BULK COLLECT提高效率
- Oracle中查询索引名称,批量修改索引名称语句
- ORACLE+Mybatis的一些批量操作及树查询
- ORACLE游标、递归查询、子查询与批量更新示例
- Oracle中查询索引名称,批量修改索引名称语句
- 『ORACLE』查询被锁的对象和批量杀会话(11g)
- Oracle 通过子查询批量添加、修改表数据
- oracle 数据分页查询
- [Oracle]ORA-19206: 用于查询或 REF CURSOR 参数的值无效
- Oracle临时表 优化查询速度
- oracle分页查询