下面统计一下这张表中的记录数:
SQL> select count(*) from customers;
COUNT(*)
----------
55500
已用时间: 00: 00: 00.01
执行计划
----------------------------------------------------------------------------------------------
Plan hash value: 237477902
----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | BITMAP CONVERSION COUNT | | 55500 | 3 (0)| 00:00:01 |
| 3 | BITMAP INDEX FAST FULL SCAN| CUSTOMERS_GENDER_BIX | | | |
----------------------------------------------------------------------------------------------
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
6 consistent gets
0 physical reads
0 redo size
413 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
首先比较两种分页方法的区别:
SQL> SET AUTOT ON
SQL> set timing on
SQL> col CUST_LAST_NAME format a16;
SQL> col CUST_FIRST_NAME format a16;
以SYS用户执行下面的SQL Plus命令(注意不要在任何重要的生产环境中执行这两条命令):
SQL> show user;
USER 为 "SYS"
SQL> alter system flush shared_pool;
系统已更改。
SQL> alter system flush buffer_cache;
系统已更改。
SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, 'CUSTOMERS')
PL/SQL 过程已成功完成。
下面比较两种分页算法的效率:
SQL> select CUST_ID,
2 CUST_FIRST_NAME,
3 CUST_LAST_NAME,
4 CUST_GENDER,
5 CUST_YEAR_OF_BIRTH,
6 CUST_CITY,
7 CUST_CITY_ID,
8 COUNTRY_ID
9 from (select rownum rn,
10 CUST_ID,
11 CUST_FIRST_NAME,
12 CUST_LAST_NAME,
13 CUST_GENDER,
14 CUST_YEAR_OF_BIRTH,
15 CUST_CITY,
16 CUST_CITY_ID,
17 COUNTRY_ID
18 from (select CUST_ID,
19 CUST_FIRST_NAME,
20 CUST_LAST_NAME,
21 CUST_GENDER,
22 CUST_YEAR_OF_BIRTH,
23 CUST_CITY,
24 CUST_CITY_ID,
25 COUNTRY_ID
26 from customers))
27 where rn between 11 and 20;
CUST_ID CUST_FIRST_NAME CUST_LAST_NAME C CUST_YEAR_OF_BIRTH CUST_CITY CUST_CITY_ID COUNTRY_ID
---------- ---------------- ---------------- - ------------------ ------------------------------ ------------ ----------
35227 Abigail Ruddy M 1948 Lelystad 51793 52770
36117 Abner Everett M 1954 Wolverhampton 52514 52789
39672 Abner Everett M 1975 Murnau 51934 52776
43228 Abner Everett M 1957 Los Angeles 51806 52790
25470 Abner Everett M 1966 Stuttgart 52331 52776
47006 Abner Everett M 1985 Montara 51919 52790
50561 Abner Everett M 1960 Neuss 51975 52776
4117 Abner Everett M 1972 Clermont-l'Herault 51329 52779
7673 Abner Everett M 1988 Schwaebisch Gmuend 52300 52776
11228 Abner Everett M 1956 Ingolstadt 51680 52776
已选择10行。
已用时间: 00: 00: 00.22
执行计划
----------------------------------------------------------
Plan hash value: 4059343527
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 55500 | 6449K| 333 (2)| 00:00:04 |
|* 1 | VIEW | | 55500 | 6449K| 333 (2)| 00:00:04 |
| 2 | COUNT | | | | | |
| 3 | TABLE ACCESS FULL| CUSTOMERS | 55500 | 2493K| 333 (2)| 00:00:04 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("RN"<=20 AND "RN">=11)
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
1460 consistent gets
0 physical reads
0 redo size
1250 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10 rows processed
SQL>
SQL> select
2 CUST_ID,
3 CUST_FIRST_NAME,
4 CUST_LAST_NAME,
5 CUST_GENDER,
6 CUST_YEAR_OF_BIRTH,
7 CUST_CITY,
8 CUST_CITY_ID,
9 COUNTRY_ID
10 from (select rownum rn,
11 CUST_ID,
12 CUST_FIRST_NAME,
13 CUST_LAST_NAME,
14 CUST_GENDER,
15 CUST_YEAR_OF_BIRTH,
16 CUST_CITY,
17 CUST_CITY_ID,
18 COUNTRY_ID
19 from (select CUST_ID,
20 CUST_FIRST_NAME,
21 CUST_LAST_NAME,
22 CUST_GENDER,
23 CUST_YEAR_OF_BIRTH,
24 CUST_CITY,
25 CUST_CITY_ID,
26 COUNTRY_ID
27 from customers)
28 where rownum <= 20)
29 where rn >= 11;
CUST_ID CUST_FIRST_NAME CUST_LAST_NAME C CUST_YEAR_OF_BIRTH CUST_CITY CUST_CITY_ID COUNTRY_I
---------- ---------------- ---------------- - ------------------ ------------------------------ ------------ ---------
35227 Abigail Ruddy M 1948 Lelystad 51793 5277
36117 Abner Everett M 1954 Wolverhampton 52514 5278
39672 Abner Everett M 1975 Murnau 51934 5277
43228 Abner Everett M 1957 Los Angeles 51806 5279
25470 Abner Everett M 1966 Stuttgart 52331 5277
47006 Abner Everett M 1985 Montara 51919 5279
50561 Abner Everett M 1960 Neuss 51975 5277
4117 Abner Everett M 1972 Clermont-l'Herault 51329 5277
7673 Abner Everett M 1988 Schwaebisch Gmuend 52300 5277
11228 Abner Everett M 1956 Ingolstadt 51680 5277
已选择10行。
已用时间: 00: 00: 00.10
执行计划
----------------------------------------------------------
Plan hash value: 4287989439
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 20 | 2380 | 2 (0)| 00:00:01 |
|* 1 | VIEW | | 20 | 2380 | 2 (0)| 00:00:01 |
|* 2 | COUNT STOPKEY | | | | | |
| 3 | TABLE ACCESS FULL| CUSTOMERS | 20 | 920 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("RN">=11)
2 - filter(ROWNUM<=20)
统计信息
----------------------------------------------------------
373 recursive calls
0 db block gets
141 consistent gets
9 physical reads
0 redo size
1250 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
10 rows processed
二者执行效率相差很大,一个需要1460多逻辑读,而另一个只需要141个逻辑读。观察二者的执行计划可以发现,两个执行计划唯一的区别就是第二
个查询在COUNT这步使用了STOPKEY,也就是说,Oracle将ROWNUM <= 20推入到查询内层,当符合查询的条件的记录达到STOPKEY的值,则Oracle结
束查询。
因此,可以预见,采用第二种方式,在翻页的开始部分查询速度很快,越到后面,效率越低,当翻到最后一页,效率应该和第一种方式接近。
SQL> select
2 CUST_ID,
3 CUST_FIRST_NAME,
4 CUST_LAST_NAME,
5 CUST_GENDER,
6 CUST_YEAR_OF_BIRTH,
7 CUST_CITY,
8 CUST_CITY_ID,
9 COUNTRY_ID
10 from (select rownum rn,
11 CUST_ID,
12 CUST_FIRST_NAME,
13 CUST_LAST_NAME,
14 CUST_GENDER,
15 CUST_YEAR_OF_BIRTH,
16 CUST_CITY,
17 CUST_CITY_ID,
18 COUNTRY_ID
19 from (select CUST_ID,
20 CUST_FIRST_NAME,
21 CUST_LAST_NAME,
22 CUST_GENDER,
23 CUST_YEAR_OF_BIRTH,
24 CUST_CITY,
25 CUST_CITY_ID,
26 COUNTRY_ID
27 from customers)
28 where rownum <= 55500)
29 where rn >= 55490;
CUST_ID CUST_FIRST_NAME CUST_LAST_NAME C CUST_YEAR_OF_BIRTH CUST_CITY CUST_CITY_ID COUNTR
---------- ---------------- ---------------- - ------------------ ------------------------------ ------------ ------
101789 Pavani Krishnan M 1947 Lowndesville 51807 5
100115 Grace Carbery M 1981 Frederick 51519 5
104365 Payton Stengard F 1981 Plains 52111 5
101876 Margaret Dadashev F 1977 Hiseville 51663 5
103405 Abbie Anderson M 1975 Chieflake 51325 5
100364 Jordan Zwolinsky F 1970 Molino 51917 5
104496 Sydney Tang F 1947 Evinston 51459 5
102169 Boriana Cay M 1944 Orlinda 52038 5
104252 Jade Taft M 1976 Norman 51986 5
100680 Joshua Prabu M 1954 Cleveland 51330 5
100055 Andrew Clark F 1978 Duncan 51402 5
已选择11行。
已用时间: 00: 00: 00.28
执行计划
----------------------------------------------------------
Plan hash value: 4287989439
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 55500 | 6449K| 333 (2)| 00:00:04 |
|* 1 | VIEW | | 55500 | 6449K| 333 (2)| 00:00:04 |
|* 2 | COUNT STOPKEY | | | | | |
| 3 | TABLE ACCESS FULL| CUSTOMERS | 55500 | 2493K| 333 (2)| 00:00:04 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("RN">=55490)
2 - filter(ROWNUM<=55500)
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
1460 consistent gets
1449 physical reads
0 redo size
1349 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
11 rows processed
SQL>
注意在执行计划的统计信息中出现了 1460 consistent gets
更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/103259.htm
