<span style="font-size:14px;"><strong>UNUSABLE Clause</strong></span><br />
<span style="font-size:14px;">When you make an index unusable, it is ignored by the optimizer and is not maintained by DML. When you make one partition of a partitioned <br />
index unusable, the other partitions of the index remain valid.</span><br />
<span style="font-size:14px;">You must rebuild or drop and re-create an unusable index or index partition before using it.</span><br />
<br />
<span style="font-size:14px;"><strong>DISABLE Clause</strong></span><br />
<span style="font-size:14px;">DISABLE applies only to a function-based index. This clause lets you disable the use of a function-based index. You might want to do so,<br />
for example, while working on the body of the function. Afterward you can either rebuild the index or specify another ALTER INDEX statement <br />
with the ENABLE keyword.</span><br />
<br />
<span style="font-size:14px;"><strong>Invisible Index</strong></span><br />
<span style="font-size:14px;">An invisible index is an index that is ignored by the optimizer unless you explicitly set the OPTIMIZER_USE_INVISIBLE_INDEXES initialization parameter<br />
to TRUE at the session or system level.</span><br />
<br />
<span style="font-size:14px;"><strong>临时禁用一个索引时,发现唯一索引置为unusable时,会影响insert操作,但函数索引和非唯一索引不会.</strong></span><br />
<span style="font-size:14px;"><strong>以下测试:</strong></span><br />
<span style="font-size:14px;">--创建测试表</span><br />
<span style="font-size:14px;">SQL> create table scott.selectshen01</span><br />
<span style="font-size:14px;"> 2 as select * from dba_objects</span><br />
<span style="font-size:14px;"> 3 where object_id is not null;</span><br />
<span style="font-size:14px;">Table created</span><br />
<span style="font-size:14px;">--建唯一索引</span><br />
<span style="font-size:14px;">SQL> create unique index scott.idx_selectshen01_01 on scott.selectshen01(object_id);</span><br />
<span style="font-size:14px;">Index created</span><br />
<span style="font-size:14px;">--建非唯一索引</span><br />
<span style="font-size:14px;">SQL> create index scott.idx_selectshen01_02 on scott.selectshen01(data_object_id);</span><br />
<span style="font-size:14px;">Index created</span><br />
<span style="font-size:14px;">--建函数索引</span><br />
<span style="font-size:14px;">SQL> create index scott.idx_selectshen01_03 on scott.selectshen01(lower(owner));</span><br />
<span style="font-size:14px;">Index created</span><br />
<br />
<span style="font-size:14px;">--设置唯一索引unusable</span><br />
<span style="font-size:14px;">SQL> alter index scott.idx_selectshen01_01 unusable;</span><br />
<span style="font-size:14px;">Index altered</span><br />
<span style="font-size:14px;">--插入数据,报ORA-01502索引不可用.</span><br />
<span style="font-size:14px;">SQL> insert into scott.selectshen01(object_id)</span><br />
<span style="font-size:14px;"> 2 select object_id from scott.selectshen01 where rownum<2;</span><br />
<span style="font-size:14px;">insert into scott.selectshen01(object_id)</span><br />
<span style="font-size:14px;">select object_id from scott.selectshen01 where rownum<2</span><br />
<span style="font-size:14px;">ORA-01502: 索引 'SCOTT.IDX_SELECTSHEN01_01' 或这类索引的分区处于不可用状态</span><br />
<span style="font-size:14px;">--只能删除或重建</span><br />
<span style="font-size:14px;">SQL> drop index scott.idx_selectshen01_01;</span><br />
<span style="font-size:14px;">Index dropped</span><br />
<span style="font-size:14px;">--可以插入数据</span><br />
<span style="font-size:14px;">SQL> </span><br />
<span style="font-size:14px;">SQL> insert into scott.selectshen01(object_id)</span><br />
<span style="font-size:14px;"> 2 select object_id from scott.selectshen01 where rownum<2;</span><br />
<span style="font-size:14px;">1 row inserted</span><br />
<br />
<span style="font-size:14px;">--设置函数索引unusable</span><br />
<span style="font-size:14px;">SQL> alter index scott.idx_selectshen01_03 unusable;</span><br />
<span style="font-size:14px;">Index altered</span><br />
<span style="font-size:14px;">--可以插入数据</span><br />
<span style="font-size:14px;">SQL> insert into scott.selectshen01(owner)</span><br />
<span style="font-size:14px;"> 2 select owner from scott.selectshen01 where rownum<2;</span><br />
<span style="font-size:14px;">1 row inserted</span><br />
<span style="font-size:14px;">--设置非唯一索引unusable</span><br />
<span style="font-size:14px;">SQL> alter index scott.idx_selectshen01_02 unusable;</span><br />
<span style="font-size:14px;">Index altered</span><br />
<span style="font-size:14px;">--可以插入数据</span><br />
<span style="font-size:14px;">SQL> insert into scott.selectshen01(data_object_id)</span><br />
<span style="font-size:14px;"> 2 select data_object_id from scott.selectshen01 where rownum<2;</span><br />
<span style="font-size:14px;">1 row inserted</span><br />
<br />
<span style="font-size:14px;"><strong>顺便测试一下index的disable和invisible:</strong></span><br />
<span style="font-size:14px;"><strong>--只有函数索引才有disable/enable选项</strong></span><br />
<span style="font-size:14px;">SQL> alter index scott.idx_selectshen01_02 disable;</span><br />
<span style="font-size:14px;">alter index scott.idx_selectshen01_02 disable</span><br />
<span style="font-size:14px;">ORA-02243: ALTER INDEX 或 ALTER MATERIALIZED VIEW 选项无效</span><br />
<span style="font-size:14px;">--设置函数索引disable</span><br />
<span style="font-size:14px;">SQL> alter index scott.idx_selectshen01_03 disable;</span><br />
<span style="font-size:14px;">Index altered</span><br />
<span style="font-size:14px;">--插入数据,报30554索引被禁用</span><br />
<span style="font-size:14px;">SQL> insert into scott.selectshen01(owner)</span><br />
<span style="font-size:14px;"> 2 select owner from scott.selectshen01 where rownum<2;</span><br />
<span style="font-size:14px;">insert into scott.selectshen01(owner)</span><br />
<span style="font-size:14px;">select owner from scott.selectshen01 where rownum<2</span><br />
<span style="font-size:14px;">ORA-30554: 基于函数的索引SCOTT.IDX_SELECTSHEN01_03被禁用</span><br />
<span style="font-size:14px;">--enable或rebuild函数索引</span><br />
<span style="font-size:14px;">SQL> alter index scott.idx_selectshen01_03 enable;</span><br />
<span style="font-size:14px;">Index altered</span><br />
<span style="font-size:14px;">--可以插入数据</span><br />
<span style="font-size:14px;">SQL> insert into scott.selectshen01(owner)</span><br />
<span style="font-size:14px;"> 2 select owner from scott.selectshen01 where rownum<2;</span><br />
<span style="font-size:14px;">1 row inserted</span><br />
<br />
<br />
<span style="font-size:14px;"><strong>--重建索引,测试visible/invisilbe</strong></span><br />
<span style="font-size:14px;">SQL> alter index scott.idx_selectshen01_02 rebuild;</span><br />
<span style="font-size:14px;">Index altered</span><br />
<span style="font-size:14px;">--参数optimizer_use_invisible_indexes为false</span><br />
<span style="font-size:14px;">SQL> select name,value from v$parameter</span><br />
<span style="font-size:14px;"> 2 where name like '%visible%';</span><br />
<span style="font-size:14px;">NAME VALUE</span><br />
<span style="font-size:14px;">--------------------------------------- ------------------------------------------</span><br />
<span style="font-size:14px;">optimizer_use_invisible_indexes FALSE</span><br />
<br />
<span style="font-size:14px;">--设置索引invisible</span><br />
<span style="font-size:14px;">SQL> alter index scott.idx_selectshen01_02 invisible;</span><br />
<span style="font-size:14px;">Index altered</span><br />
<br />
<span style="font-size:14px;">--查看sql执行计划,发现不会走这个invisible的索引</span><br />
<span style="font-size:14px;">SQL> set autotrace traceonly</span><br />
<span style="font-size:14px;">SQL> select * from scott.selectshen01 where data_object_id=1;</span><br />
<br />
<span style="font-size:14px;">no rows selected</span><br />
<br />
<br />
<span style="font-size:14px;">Execution Plan</span><br />
<span style="font-size:14px;">----------------------------------------------------------</span><br />
<span style="font-size:14px;">Plan hash value: 3630500522</span><br />
<br />
<span style="font-size:14px;">--------------------------------------------------------------------------------</span><br />
<span style="font-size:14px;">--</span><br />
<br />
<span style="font-size:14px;">| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time</span><br />
<span style="font-size:14px;"> |</span><br />
<br />
<span style="font-size:14px;">--------------------------------------------------------------------------------</span><br />
<span style="font-size:14px;">--</span><br />
<br />
<span style="font-size:14px;">| 0 | SELECT STATEMENT | | 14 | 2898 | 349 (1)| 00:00:05</span><br />
<span style="font-size:14px;"> |</span><br />
<br />
<span style="font-size:14px;">|* 1 | TABLE ACCESS FULL| SELECTSHEN01 | 14 | 2898 | 349 (1)| 00:00:05</span><br />
<span style="font-size:14px;"> |</span><br />
<br />
<span style="font-size:14px;">--------------------------------------------------------------------------------</span><br />
<span style="font-size:14px;">...</span><br />
<span style="font-size:14px;">SQL> set autotrace off</span><br />
<br />
<br />
<span style="font-size:14px;">--设置索引visible</span><br />
<span style="font-size:14px;">SQL> alter index scott.idx_selectshen01_02 visible;</span><br />
<span style="font-size:14px;">Index altered</span><br />
<br />
<span style="font-size:14px;">--查看sql执行计划,发现已经走这个visible的索引</span><br />
<span style="font-size:14px;">SQL> set autotrace traceonly</span><br />
<br />
<span style="font-size:14px;">SQL> select * from scott.selectshen01 where data_object_id=1;</span><br />
<br />
<span style="font-size:14px;">no rows selected</span><br />
<br />
<br />
<span style="font-size:14px;">Execution Plan</span><br />
<span style="font-size:14px;">----------------------------------------------------------</span><br />
<span style="font-size:14px;">Plan hash value: 2125468280</span><br />
<br />
<span style="font-size:14px;">--------------------------------------------------------------------------------</span><br />
<span style="font-size:14px;">-------------------</span><br />
<br />
<span style="font-size:14px;">| Id | Operation | Name | Rows | Bytes | Cost</span><br />
<span style="font-size:14px;"> (%CPU)| Time |</span><br />
<br />
<span style="font-size:14px;">--------------------------------------------------------------------------------</span><br />
<span style="font-size:14px;">-------------------</span><br />
<br />
<span style="font-size:14px;">| 0 | SELECT STATEMENT | | 1 | 207 |</span><br />
<span style="font-size:14px;">1 (0)| 00:00:01 |</span><br />
<br />
<span style="font-size:14px;">| 1 | TABLE ACCESS BY INDEX ROWID| SELECTSHEN01 | 1 | 207 |</span><br />
<span style="font-size:14px;">1 (0)| 00:00:01 |</span><br />
<br />
<span style="font-size:14px;">|* 2 | INDEX RANGE SCAN | IDX_SELECTSHEN01_02 | 1 | |</span><br />
<span style="font-size:14px;">1 (0)| 00:00:01 |</span><br />
<br />
<span style="font-size:14px;">--------------------------------------------------------------------------------</span><br />
<br />
<span style="font-size:14px;">SQL> set autotrace off </span><br />
<br />
<span style="font-size:14px;"><strong>备注:</strong></span><br />
<span style="font-size:14px;">1.unusable的索引需要rebuild 或 drop,re-create才能回到valid.skip_unusable_indexes为true也是无法在insert时跳过不可用的唯一索引的.</span><br />
<span style="font-size:14px;">2.disable/enable只能用在函数索引中.从dba_indexes中这个值对应的列名funcidx_status也可以看出.</span><br />
<span style="font-size:14px;">3.invisible仍然会在表增删改数据时,更新索引.不可见索引在性能优化时测试索引的有效性很有用,不会影响其它会话的执行计划.可以通过设置系统或会话的<br />
optimizer_use_invisible_indexes为true,让不可见索引被使用.</span><br />
<br />
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30590922/viewspace-1972572/
,如需转载,请注明出处,否则将追究法律责任。