这是一个极端的例子,为了使用索引,我修改了optimizer_index_cost_adj 与optimizer_index_caching
参数,我建立deptno+dname的索引并分析了表。
scott@ORCL> CREATE UNIQUE INDEX SCOTT.I_DEPT_DEPTNO_DNAME ON SCOTT.DEPT
2 (DEPTNO, DNAME);
Index created.
scott@ORCL> Analyze Table SCOTT.DEPT Compute Statistics ;
Table analyzed.
scott@ORCL> ALTER SESSION SET optimizer_index_cost_adj = 1;
Session altered.
scott@ORCL> ALTER SESSION SET optimizer_index_caching = 100 ;
Session altered.
scott@ORCL> SELECT *
2 FROM scott.dept
3 WHERE deptno IN (SELECT deptno
4 FROM scott.dept
5 WHERE dname LIKE '%A%');
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=24)
1 0 NESTED LOOPS (Cost=2 Card=1 Bytes=24)
2 1 INDEX (FULL SCAN) OF 'I_DEPT_DEPTNO_DNAME' (UNIQUE) (Cost=26 Card=1 Bytes=9)
3 1 TABLE ACCESS (BY INDEX ROWID) OF 'DEPT' (Cost=1 Card=5 Bytes=75)
4 3 INDEX (UNIQUE SCAN) OF 'PK_DEPT' (UNIQUE)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
10 consistent gets
0 physical reads
0 redo size
678 bytes sent via SQL*Net to client
425 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
4 rows processed
scott@ORCL> select * from scott.dept where
2 rowid in (
3 select /*+ index_ffs(a i_dept_dname) */
4 rowid from scott.dept a where dname like '%A%')
5 ;
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=36)
1 0 NESTED LOOPS (Cost=2 Card=1 Bytes=36)
2 1 INDEX (FULL SCAN) OF 'I_DEPT_NAME' (NON-UNIQUE) (Cost=1 Card=1 Bytes=15)
3 1 TABLE ACCESS (BY USER ROWID) OF 'DEPT' (Cost=1 Card=5 Bytes=105)
Statistics
----------------------------------------------------------
48 recursive calls
0 db block gets
27 consistent gets
0 physical reads
0 redo size
678 bytes sent via SQL*Net to client
425 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
4 rows processed