默认值100意味着索引访问与全表扫描是完全等价的
sys@ORCL> show parameter optimizer_index NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ optimizer_index_caching integer 0 optimizer_index_cost_adj integer 100
--创建测试表
sys@ORCL> drop table t purge;
Table dropped.
sys@ORCL> create table t as select * from dba_objects;
Table created.
sys@ORCL> create index idx_t on t (owner);
Index created.
sys@ORCL> analyze table t compute statistics;
Table analyzed.
--分别观察全表扫和索引访问的成本
sys@ORCL> set autot trace exp
sys@ORCL> select /*+ full(t) */ * from t where owner=\'HR\';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2098 | 178K| 155 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| T | 2098 | 178K| 155 (2)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"=\'HR\')
sys@ORCL> select * from t where owner=\'HR\';
Execution Plan
----------------------------------------------------------
Plan hash value: 1594971208
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2098 | 178K| 61 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 2098 | 178K| 61 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_T | 2098 | | 5 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER"=\'HR\') optimizer_index_cost_adj=Full Table Scan Cost/Index Scan Cost
下面我们通过调整optimizer_index_cost_adj来看一下执行计划的变化
sys@ORCL> set autot trace exp
sys@ORCL> alter session set optimizer_index_cost_adj=80;
Session altered.
sys@ORCL> select * from t where owner=\'HR\';
Execution Plan
----------------------------------------------------------
Plan hash value: 1594971208
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2098 | 178K| 49 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 2098 | 178K| 49 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_T | 2098 | | 4 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER"=\'HR\')
sys@ORCL> select /*+ full(t) */ * from t where owner=\'HR\';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2098 | 178K| 155 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| T | 2098 | 178K| 155 (2)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"=\'HR\')sys@ORCL> alter session set optimizer_index_cost_adj=280;
Session altered.
sys@ORCL> select * from t where owner=\'HR\';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2098 | 178K| 155 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| T | 2098 | 178K| 155 (2)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"=\'HR\')
sys@ORCL> select /*+ index(t idx_t) */ * from t where owner=\'HR\';
Execution Plan
----------------------------------------------------------
Plan hash value: 1594971208
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2098 | 178K| 171 (0)| 00:00:03 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 2098 | 178K| 171 (0)| 00:00:03 |
|* 2 | INDEX RANGE SCAN | IDX_T | 2098 | | 14 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER"=\'HR\')sys@ORCL> ed
Wrote file afiedt.buf
1 select a.average_wait a1,
2 b.average_wait a2,
3 round( ((a.average_wait/b.average_wait)*100) ) a3
4 from
5 (select d.kslednam EVENT,
6 s.kslestim / (10000 * s.ksleswts) AVERAGE_WAIT
7 from x$kslei s, x$ksled d
8 where s.ksleswts != 0 and s.indx = d.indx) a,
9 (select d.kslednam EVENT,
10 s.kslestim / (10000 * s.ksleswts) AVERAGE_WAIT
11 from x$kslei s, x$ksled d
12 where s.ksleswts != 0 and s.indx = d.indx) b
13 where a.event = \'db file sequential read\'
14* and b.event = \'db file scattered read\'
15
sys@ORCL> /
A1 A2 A3
---------- ---------- ----------
1.35507461 2.1632742 63来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26515977/viewspace-1207987/ ,如需转载,请注明出处,否则将追究法律责任。