oracle fetch循环,关于fetch记录的速度

一个测试例子及说明

SQL> create table test_cursor as select a.* from all_objects a,all_objects b where rownum<200001;

表已创建。

SQL> select count(*) from test_cursor;

COUNT(*)

----------

200000

SQL> set timing on

SQL> select count(*) from test_cursor;

COUNT(*)

----------

200000

已用时间: 00: 00: 01.03

SQL> declare

2 n_time1 number;

3 n_time2 number;

4 n_time3 number;

5 n_time4 number;

6 r_test_cursor test_cursor%rowtype;

7 n_count number;

8 cursor c1 is select count(*) from test_cursor;

9 cursor c2 is select * from test_cursor;

10 cursor c3 is select * from test_cursor;

11 begin

12 n_time1 := dbms_utility.get_time;

13 for c11 in c1 loop

14 null;

15 end loop;

16 n_time2 := dbms_utility.get_time;

17

18 open c2;

19 fetch c2 into r_test_cursor;

20 close c2;

21

22 n_time3 := dbms_utility.get_time;

23

24 for c33 in c3 loop

25 null;

26 end loop;

27

28 n_time4 := dbms_utility.get_time;

29

30 dbms_output.put_line(to_char(n_time2 - n_time1));

31 dbms_output.put_line(to_char(n_time3 - n_time2));

32 dbms_output.put_line(to_char(n_time4 - n_time3));

33

34 end ;

35 /

175 ----- 获取结果总和消耗时间,做了个full table scan

4 ------ 获取满足条件的第一条记录消耗时间

1903 ------- 获取满足条件的所有记录消耗的时间

PL/SQL 过程已成功完成。

SQL>

当游标打开的时候,数据库并没有获取整个记录集合

当我们开始fetch的时候才取 data buffer 获取所需要的数据,也就是说打开游标的时候记录本身未必已经存在于data buffer中,或者就算已经存在但并没有为游标 查询出这么一个集合

实际上,数据库在处理的时候

当我们获取第一条记录的时候,数据库会去 data buffer中将 满足条件的记录获取一批到 PGA 中

这样一批一批的获取到PGA中,即使我们一条一条的fetch

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值