一个测试例子及说明
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