以往在优化的时候如果要禁用一个索引做测试时只能drop,而后重建(funcation based index除外)。
从11g索引可以有invisible和visible了,避免了重新创建索引的麻烦,并且当索引为invisible是对table的dml同样会更新index
当索引被设为不可见后,实际上就是指该索引对于优化器不可见,而索引的正常更新并不受影响——即表在增、删、改时,索引也会被更新。只是当优化器在选择查询计划时会“无视”该索引(无论是CBO或RBO):
SQL> show parameter OPTIMIZER_USE_INVISIBLE_INDEXES
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_use_invisible_indexes boolean FALSE
SQL> select *From v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> conn scott/tiger
已连接。
SQL> create table t1 as select *From emp;
表已创建。
SQL> create index ii on t1(empno);
索引已创建。
SQL> set autot trace
SQL> select ename from t1 where empno=7788;
执行计划
----------------------------------------------------------
Plan hash value: 2627886109
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 20 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 1 | 20 | 1 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | II | 1 | | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------
SQL> alter index ii invisible;
索引已更改。
SQL> SELECT INDEX_NAME, VISIBILITY
2 FROM USER_INDEXES
3 WHERE INDEX_NAME ='II';
INDEX_NAME VISIBILIT
------------------------------ ---------
II INVISIBLE
SQL> select ename from t1 where empno=7788;
未选定行
执行计划
----------------------------------------------------------
Plan hash value: 3617692013
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 20 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T1 | 1 | 20 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
SQL> alter index ii visible;
索引已更改。
SQL> SELECT INDEX_NAME, VISIBILITY
2 FROM USER_INDEXES
3 WHERE INDEX_NAME ='II';
INDEX_NAME VISIBILIT
------------------------------ ---------
II VISIBLE
SQL> show parameter OPTIMIZER_USE_INVISIBLE_INDEXES
NAME TYPE VALUE
------------------------------------ ----------- -----------------------------
optimizer_use_invisible_indexes boolean FALSE
SQL> alter system set optimizer_use_invisible_indexes=true;
系统已更改。
SQL> show parameter OPTIMIZER_USE_INVISIBLE_INDEXES
NAME TYPE VALUE
------------------------------------ ----------- -----------------------------
optimizer_use_invisible_indexes boolean TRUE
SQL> alter index ii invisible ;
SQL> select ename from t1 where empno=7788;
未选定行
执行计划
----------------------------------------------------------
Plan hash value: 2627886109
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 20 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 1 | 20 | 1 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | II | 1 | | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------