zt http://www.xifenfei.com/1290.htmlhtml
一、查看數據庫版本
SQL> select * from v$version where rownum<2;sql
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production數據庫
二、建立表和索引
create table t_a as select * from dba_objects;
create index t_a_ind on t_a(object_id);ide
三、查詢最大值
SQL> select max(object_id) from t_a;
執行計劃
———————————————————-
Plan hash value: 3226265922this
————————————————————————————–orm
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |htm
————————————————————————————–索引
| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |ip
| 1 | SORT AGGREGATE | | 1 | 13 | | |get
| 2 | INDEX FULL SCAN (MIN/MAX)| T_A_IND | 1 | 13 | 2 (0)| 00:00:01 |
————————————————————————————–
Note—— dynamic sampling used for this statement (level=2)
統計信息
———————————————————-
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
431 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
四、查詢最小值
SQL> select min(object_id) from t_a;
執行計劃
———————————————————-
Plan hash value: 3226265922
————————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————————–
| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 13 | |
|
| 2 | INDEX FULL SCAN (MIN/MAX)| T_A_IND | 1 | 13 | 2 (0)| 00:00:01 |
————————————————————————————–
Note—— dynamic sampling used for this statement (level=2)
統計信息
———————————————————-
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
429 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
五、查詢最大值和最小值
SQL> select max(object_id),min(object_id) from t_a;
執行計劃
———————————————————-
Plan hash value: 2127980459
—————————————————————————
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
—————————————————————————
| 0 | SELECT STATEMENT | | 1 | 13 | 293 (1)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
| 2 | TABLE ACCESS FULL| T_A | 78093 | 991K| 293 (1)| 00:00:04 |
—————————————————————————
Note—— dynamic sampling used for this statement (level=2)
統計信息
———————————————————-
4 recursive calls
0 db block gets
1119 consistent gets
1044 physical reads
0 redo size
502 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
六、查詢最大值和最小值(排除null)
SQL> select max(object_id),min(object_id) from t_a where object_id is not null;
執行計劃
———————————————————-
Plan hash value: 1214261695
———————————————————————————
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
———————————————————————————
| 0 | SELECT STATEMENT | | 1 | 13 | 50 (2)| 00:00:01
|
| 1 | SORT AGGREGATE | | 1 | 13 | |
|
|* 2 | INDEX FAST FULL SCAN| T_A_IND | 78093 | 991K| 50 (2)| 00:00:01
|
———————————————————————————
Predicate Information (identified by operation id):
—————————————————
2 – filter(「OBJECT_ID」 IS NOT NULL)
Note—— dynamic sampling used for this statement (level=2)
統計信息
———————————————————-
4 recursive calls
0 db block gets
242 consistent gets
0 physical reads
0 redo size
502 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
七、分別查詢最大值和最小值
SQL> select (select max(object_id) from t_a) max,(select min(object_id) from t_a) min from dual;
執行計劃
———————————————————-
Plan hash value: 312201770
————————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————————–
| 0 | SELECT STATEMENT | | 1 | | 2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 13 | |
| 2 | INDEX FULL SCAN (MIN/MAX)| T_A_IND | 1 | 13 | 2 (0)| 00:00:01 |
| 3 | SORT AGGREGATE | | 1 | 13 | |
| 4 | INDEX FULL SCAN (MIN/MAX)| T_A_IND | 1 | 13 | 2 (0)| 00:00:01 |
| 5 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
————————————————————————————–
Note—— dynamic sampling used for this statement (level=2)
統計信息
———————————————————-
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
480 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
說明:
一、單查詢最大值或者最小值,sql會自動走index
二、若是同時查詢最大值和最小值,sql會使用全表掃描,而不是咱們想象的索引快速掃描
三、加上where 排除掉null的狀況,sql使用索引快速掃描,緣由是:在不能肯定索引列不爲null(或者沒有排除掉null)的狀況下,不會使用索引快速掃描,而sql爲了保證正確而採用了全表掃描
四、INDEX FULL SCAN (MIN/MAX)掃描效率很搞,因此把最大值,最小值分開查詢,提升執行效率
五、其餘寫法
SQL> select (select /*+ index_asc(t_a t_a_ind) */ object_id from t_a where rownu
m=1) min ,(select /*+ index_desc(t_a t_a_ind) */ object_id from t_a where rownum=1)
max from dual;
執行計劃
———————————————————-
Plan hash value: 674626822
—————————————————————————————
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
—————————————————————————————
| 0 | SELECT STATEMENT | | 1 | | 2 (0)| 00:
00:01 |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | INDEX FULL SCAN | T_A_IND | 78093 | 991K| 2 (0)| 00:
00:01 |
|* 3 | COUNT STOPKEY | | | | |
| 4 | INDEX FULL SCAN DESCENDING| T_A_IND | 78093 | 991K| 2 (0)| 00:
00:01 |
| 5 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
—————————————————————————————
Predicate Information (identified by operation id):
—————————————————
1 – filter(ROWNUM=1)
3 – filter(ROWNUM=1)
Note—— dynamic sampling used for this statement (level=2)
統計信息———————————————————-0 recursive calls0 db block gets4 consistent gets0 physical reads0 redo size480 bytes sent via SQL*Net to client416 bytes received via SQL*Net from client2 SQL*Net roundtrips to/from client0 sorts (memory)0 sorts (disk)1 rows processed注意:1)這個要正確執行,須要一個前提條件object_id這列要爲not null限制條件,否則會hint提示無效2)從執行計劃的統計信息上看,這個和INDEX FULL SCAN (MIN/MAX)方式的執行效率同樣