链接地址:http://blog.csdn.net/launch_225/article/details/25509089
SQL> create table t1 as select * from dba_objects;
Table created.
SQL> create index idx_z on t1(object_id,owner);
Index created.
SQL> create index idx_f on t1(owner,object_id);
Index created.
SELECT /*+ INDEX(T1,IDX_Z) */status from t1 where owner='PUBLIC' AND OBJECT_ID=26122;
Execution Plan
----------------------------------------------------------
Plan hash value: 2939511897
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 17 | 595 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 17 | 595 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_Z | 17 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_ID"=26122 AND "OWNER"='PUBLIC')
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets --4个逻辑读
0 physical reads
0 redo size
527 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SELECT /*+ INDEX(T1,IDX_f) */status from t1 where owner='PUBLIC' AND OBJECT_ID=26122
Execution Plan
----------------------------------------------------------
Plan hash value: 2808883862
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 17 | 595 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 17 | 595 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_F | 17 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER"='PUBLIC' AND "OBJECT_ID"=26122)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets --与上面的相等,4;
0 physical reads
0 redo size
527 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SELECT /*+ INDEX(T1,IDX_f) */ STATUS FROM T1 WHERE OBJECT_ID>20000 AND OWNER='PUBLIC';
Execution Plan
----------------------------------------------------------
Plan hash value: 2808883862
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14657 | 500K| 489 (1)| 00:00:06 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 14657 | 500K| 489 (1)| 00:00:06 |
|* 2 | INDEX RANGE SCAN | IDX_F | 14657 | | 52 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER"='PUBLIC' AND "OBJECT_ID">20000 AND "OBJECT_ID" IS NOT
NULL)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3535 consistent gets ---此索引owner前置,逻辑读为3535
0 physical reads
0 redo size
372119 bytes sent via SQL*Net to client
16363 bytes received via SQL*Net from client
1442 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
21605 rows processed
select /*+ index(t1,idx_z) */ STATUS FROM T1 WHERE OBJECT_ID>20000 AND OWNER='PUBLIC'
Execution Plan
----------------------------------------------------------
Plan hash value: 2939511897
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14657 | 500K| 148 (0)| 00:00:02 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 14657 | 500K| 148 (0)| 00:00:02 |
|* 2 | INDEX RANGE SCAN | IDX_Z | 167 | | 145 (0)| 00:00:02 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_ID">20000 AND "OWNER"='PUBLIC' AND "OBJECT_ID" IS NOT
NULL)
filter("OWNER"='PUBLIC')
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3637 consistent gets ---object_id前置的逻辑读3637比上面的值3535更多
0 physical reads
0 redo size
372119 bytes sent via SQL*Net to client
16363 bytes received via SQL*Net from client
1442 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
21605 rows processed
--可以根据上面得出:等值扫描时,所消耗的逻辑读是相等的;
--范围扫描时,=的列前置的索引所消耗的逻辑读更少;
将条件=和>的位置对调下;可以看到得到结论以上面相符;
select /*+ index(t1,idx_z) */ STATUS FROM T1 WHERE OBJECT_ID=20000 AND OWNER>'PUBLIC'
Execution Plan
----------------------------------------------------------
Plan hash value: 2939511897
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 35 | 1225 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 35 | 1225 | 3 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_Z | 3 | | 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_ID"=20000 AND "OWNER">'PUBLIC' AND "OWNER" IS NOT NULL)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
527 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
select /*+ index(t1,idx_F) */ STATUS FROM T1 WHERE OBJECT_ID=20000 AND OWNER>'PUBLIC'
Execution Plan
----------------------------------------------------------
Plan hash value: 2808883862
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 35 | 1225 | 135 (0)| 00:00:02 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 35 | 1225 | 135 (0)| 00:00:02 |
|* 2 | INDEX RANGE SCAN | IDX_F | 151 | | 130 (0)| 00:00:02 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER">'PUBLIC' AND "OBJECT_ID"=20000 AND "OWNER" IS NOT NULL)
filter("OBJECT_ID"=20000)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
111 consistent gets
0 physical reads
0 redo size
527 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed