示例如下(请自己动手实验):
create table test1 as select * from dba_objects;
create table test2 as select * from dba_objects;
create index idx1 on test1(object_id);
create index idx2 on test1(owner);
create index idx3 on test2(object_id);
create index idx4 on test2(owner);
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(ownname => 'SCOTT',
tabname => 'TEST1',
estimate_percent => 100,
method_opt => 'for columns owner size 200',
no_invalidate => FALSE,
degree => 1,
cascade => TRUE);
END;
/
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(ownname => 'SCOTT',
tabname => 'TEST2',
estimate_percent => 100,
method_opt => 'for columns owner size 200',
no_invalidate => FALSE,
degree => 1,
cascade => TRUE);
END;
/
比如这个SQL:
select * from test1 where owner='SCOTT' or object_id in(select object_id from test2 where owner='SCOTT');
执行计划如下:
select * from test1 where owner='SCOTT' or object_id in(select object_id from test2 where owner='SCOTT');
1859 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 4136318878
------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5493 | 520K| 296 (1)| 00:00:04 |
|* 1 | FILTER | | | | | |
| 2 | TABLE ACCESS FULL | TEST1 | 74533 | 7060K| 296 (1)| 00:00:04 |
| 3 | BITMAP CONVERSION TO ROWIDS | | 1 | 19 | 2 (0)| 00:00:01 |
| 4 | BITMAP AND | | | | | |
| 5 | BITMAP CONVERSION FROM ROWIDS| | | | | |
|* 6 | INDEX RANGE SCAN | IDX3 | 1860 | | 1 (0)| 00:00:01 |
| 7 | BITMAP CONVERSION FROM ROWIDS| | | | | |
|* 8 | INDEX RANGE SCAN | IDX4 | 1860 | | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"='SCOTT' OR EXISTS (SELECT 0 FROM "TEST2" "TEST2" WHERE
"OBJECT_ID"=:B1 AND "OWNER"='SCOTT'))
6 - access("OBJECT_ID"=:B1)
8 - access("OWNER"='SCOTT')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
475201 consistent gets
0 physical reads
0 redo size
70860 bytes sent via SQL*Net to client
1772 bytes received via SQL*Net from client
125 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1859 rows processed
由于在test2上创建的都是单列索引:
create index idx3 on test2(object_id);
create index idx4 on test2(owner);
在test2上创建组合索引
create index idx5 on test2(object_id,owner);
SQL> select * from test1 where owner='SCOTT' or object_id in(select object_id from test2 where owner='SCOTT');
1859 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 4133849820
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5493 | 520K| 296 (1)| 00:00:04 |
|* 1 | FILTER | | | | | |
| 2 | TABLE ACCESS FULL | TEST1 | 74533 | 7060K| 296 (1)| 00:00:04 |
|* 3 | TABLE ACCESS BY INDEX ROWID| TEST2 | 1 | 19 | 4 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | IDX4 | 1860 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"='SCOTT' OR EXISTS (SELECT 0 FROM "TEST2" "TEST2" WHERE
"OWNER"='SCOTT' AND "OBJECT_ID"=:B1))
3 - filter("OBJECT_ID"=:B1)
4 - access("OWNER"='SCOTT')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2399432 consistent gets
0 physical reads
0 redo size
70860 bytes sent via SQL*Net to client
1772 bytes received via SQL*Net from client
125 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1859 rows processed