Table created.
SQL> create index t_idx on t(rn);
Index created.
SQL> BEGIN
2
3 DBMS_STATS.GATHER_TABLE_STATS(OWNNAME=>'SYS',
4 TABNAME=>'T',
5 ESTIMATE_PERCENT=>30,
6 METHOD_OPT=>'FOR ALL COLUMNS SIZE 1',
7 NO_INVALIDATE=>FALSE,
8 CASCADE=>TRUE,
9 DEGREE => 4);
10 END;
11 /
PL/SQL procedure successfully completed.
SQL> explain plan for select * from t where rn<200;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 470836197
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 199 | 4179 | 3 (0)| 00:00:35 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 199 | 4179 | 3 (0)| 00:00:35 |
|* 2 | INDEX RANGE SCAN | T_IDX | 199 | | 2 (0)| 00:00:24 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("RN"<200)
14 rows selected.
SQL> alter session set optimizer_index_cost_adj=50;
Session altered.
SQL> explain plan for select * from t where rn<200;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 470836197
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 199 | 4179 | 2 (0)| 00:00:18 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 199 | 4179 | 2 (0)| 00:00:18 |
|* 2 | INDEX RANGE SCAN | T_IDX | 199 | | 1 (0)| 00:00:12 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("RN"<200)
14 rows selected.
SQL> alter session set optimizer_index_cost_adj=500;
Session altered.
SQL> explain plan for select * from t where rn<200;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1601196873
-------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 199 | 4179 | 6 (0)| 00:01:10 |
|* 1 | TABLE ACCESS FULL| T | 199 | 4179 | 6 (0)| 00:01:10 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("RN"<200)
13 rows selected.
SQL> explain plan for select * from t where rn in (1,2,3,4,5,6,7,100,130,200,240,2000);
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3506647781
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 252 | 4 (0)| 00:00:43 |
| 1 | INLIST ITERATOR | | | | | |
| 2 | TABLE ACCESS BY INDEX ROWID| T | 12 | 252 | 4 (0)| 00:00:43 |
|* 3 | INDEX RANGE SCAN | T_IDX | 12 | | 3 (0)| 00:00:31 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("RN"=1 OR "RN"=2 OR "RN"=3 OR "RN"=4 OR "RN"=5 OR "RN"=6 OR
"RN"=7 OR "RN"=100 OR "RN"=130 OR "RN"=200 OR "RN"=240 OR "RN"=2000)
16 rows selected.
SQL> alter session set optimizer_index_caching=10;
Session altered.
SQL> explain plan for select * from t where rn in (1,2,3,4,5,6,7,100,130,200,240,2000);
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3506647781
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 252 | 3 (0)| 00:00:31 |
| 1 | INLIST ITERATOR | | | | | |
| 2 | TABLE ACCESS BY INDEX ROWID| T | 12 | 252 | 3 (0)| 00:00:31 |
|* 3 | INDEX RANGE SCAN | T_IDX | 12 | | 2 (0)| 00:00:19 | --可以看到cost确实降低了
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("RN"=1 OR "RN"=2 OR "RN"=3 OR "RN"=4 OR "RN"=5 OR "RN"=6 OR
"RN"=7 OR "RN"=100 OR "RN"=130 OR "RN"=200 OR "RN"=240 OR "RN"=2000)
16 rows selected
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 752965310
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 42 | 8 (0)| 00:01:34 |
| 1 | NESTED LOOPS | | | | | |
| 2 | NESTED LOOPS | | 1 | 42 | 8 (0)| 00:01:34 |
|* 3 | TABLE ACCESS FULL | T | 1 | 21 | 6 (0)| 00:01:10 |
|* 4 | INDEX RANGE SCAN | T_IDX | 1 | | 1 (0)| 00:00:12 |
| 5 | TABLE ACCESS BY INDEX ROWID| T | 1 | 21 | 2 (0)| 00:00:24 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("B"."NAME"='sss')
4 - access("A"."RN"="B"."RN")
18 rows selected.
SQL> alter session set optimizer_index_caching=10;
Session altered.
SQL> explain plan for select /*+ use_nl(a b) */ * from t a,t b where a.rn=b.rn and b.name='sss';
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 752965310
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 42 | 7 (0)| 00:01:22 |
| 1 | NESTED LOOPS | | | | | |
| 2 | NESTED LOOPS | | 1 | 42 | 7 (0)| 00:01:22 | --可以看到cost确实降低了
|* 3 | TABLE ACCESS FULL | T | 1 | 21 | 6 (0)| 00:01:10 |
|* 4 | INDEX RANGE SCAN | T_IDX | 1 | | 0 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| T | 1 | 21 | 1 (0)| 00:00:12 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("B"."NAME"='sss')
4 - access("A"."RN"="B"."RN")
18 rows selected.
非in-list和嵌套循环操作,调整这个参数时,不会影响oracle优化器成本的运算,如下
SQL> set linesize 200 pagesize 999
SQL> explain plan for select * from t where rn<200;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 470836197
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 199 | 4179 | 3 (0)| 00:00:35 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 199 | 4179 | 3 (0)| 00:00:35 |
|* 2 | INDEX RANGE SCAN | T_IDX | 199 | | 2 (0)| 00:00:24 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("RN"<200)
14 rows selected.
SQL> alter session set optimizer_index_caching=1;
Session altered.
SQL> explain plan for select * from t where rn<200;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 470836197
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 199 | 4179 | 3 (0)| 00:00:35 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 199 | 4179 | 3 (0)| 00:00:35 |
|* 2 | INDEX RANGE SCAN | T_IDX | 199 | | 2 (0)| 00:00:24 | --可以看到cost确实没有改变
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("RN"<200)
14 rows selected.
747




被折叠的 条评论
为什么被折叠?



