ITPub博客

首页 > 数据库 > Oracle > oracle小知识点7--索引的unusable,disable,invisible

oracle小知识点7--索引的unusable,disable,invisible

Oracle 作者:childishwanghe 时间:2016-01-06 14:42:34 0 删除 编辑
<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&gt; create table scott.selectshen01</span><br /> <span style="font-size:14px;">&nbsp; 2&nbsp; as select * from dba_objects</span><br /> <span style="font-size:14px;">&nbsp; 3&nbsp; 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&gt; 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&gt; 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&gt; 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&gt; 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&gt; insert into scott.selectshen01(object_id)</span><br /> <span style="font-size:14px;">&nbsp; 2&nbsp; select object_id from scott.selectshen01 where rownum&lt;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&lt;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&gt; 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&gt; </span><br /> <span style="font-size:14px;">SQL&gt; insert into scott.selectshen01(object_id)</span><br /> <span style="font-size:14px;">&nbsp; 2&nbsp; select object_id from scott.selectshen01 where rownum&lt;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&gt; 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&gt; insert into scott.selectshen01(owner)</span><br /> <span style="font-size:14px;">&nbsp; 2&nbsp; select owner from scott.selectshen01 where rownum&lt;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&gt; 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&gt; insert into scott.selectshen01(data_object_id)</span><br /> <span style="font-size:14px;">&nbsp; 2&nbsp; select data_object_id from scott.selectshen01 where rownum&lt;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&gt; 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&gt; 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&gt; insert into scott.selectshen01(owner)</span><br /> <span style="font-size:14px;">&nbsp; 2&nbsp; select owner from scott.selectshen01 where rownum&lt;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&lt;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&gt; 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&gt; insert into scott.selectshen01(owner)</span><br /> <span style="font-size:14px;">&nbsp; 2&nbsp; select owner from scott.selectshen01 where rownum&lt;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&gt; 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&gt; select name,value from v$parameter</span><br /> <span style="font-size:14px;">&nbsp; 2&nbsp; where name like '%visible%';</span><br /> <span style="font-size:14px;">NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VALUE</span><br /> <span style="font-size:14px;">--------------------------------------- ------------------------------------------</span><br /> <span style="font-size:14px;">optimizer_use_invisible_indexes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FALSE</span><br /> <br /> <span style="font-size:14px;">--设置索引invisible</span><br /> <span style="font-size:14px;">SQL&gt; 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&gt; set autotrace traceonly</span><br /> <span style="font-size:14px;">SQL&gt; 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&nbsp; | Operation&nbsp;&nbsp; &nbsp;&nbsp; | Name&nbsp;&nbsp; &nbsp; | Rows&nbsp; | Bytes | Cost (%CPU)| Time</span><br /> <span style="font-size:14px;">&nbsp;|</span><br /> <br /> <span style="font-size:14px;">--------------------------------------------------------------------------------</span><br /> <span style="font-size:14px;">--</span><br /> <br /> <span style="font-size:14px;">|&nbsp;&nbsp; 0 | SELECT STATEMENT&nbsp; |&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; |&nbsp;&nbsp;&nbsp; 14 |&nbsp; 2898 |&nbsp;&nbsp; 349&nbsp;&nbsp; (1)| 00:00:05</span><br /> <span style="font-size:14px;">&nbsp;|</span><br /> <br /> <span style="font-size:14px;">|*&nbsp; 1 |&nbsp; TABLE ACCESS FULL| SELECTSHEN01 |&nbsp;&nbsp;&nbsp; 14 |&nbsp; 2898 |&nbsp;&nbsp; 349&nbsp;&nbsp; (1)| 00:00:05</span><br /> <span style="font-size:14px;">&nbsp;|</span><br /> <br /> <span style="font-size:14px;">--------------------------------------------------------------------------------</span><br /> <span style="font-size:14px;">...</span><br /> <span style="font-size:14px;">SQL&gt; set autotrace off</span><br /> <br /> <br /> <span style="font-size:14px;">--设置索引visible</span><br /> <span style="font-size:14px;">SQL&gt; 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&gt; set autotrace traceonly</span><br /> <br /> <span style="font-size:14px;">SQL&gt; 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&nbsp; | Operation&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; | Name&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; | Rows&nbsp; | Bytes | Cost</span><br /> <span style="font-size:14px;">&nbsp;(%CPU)| Time&nbsp;&nbsp; &nbsp;&nbsp; |</span><br /> <br /> <span style="font-size:14px;">--------------------------------------------------------------------------------</span><br /> <span style="font-size:14px;">-------------------</span><br /> <br /> <span style="font-size:14px;">|&nbsp;&nbsp; 0 | SELECT STATEMENT&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; |&nbsp;&nbsp; &nbsp;1 |&nbsp;&nbsp; 207 |</span><br /> <span style="font-size:14px;">1&nbsp;&nbsp; (0)| 00:00:01 |</span><br /> <br /> <span style="font-size:14px;">|&nbsp;&nbsp; 1 |&nbsp; TABLE ACCESS BY INDEX ROWID| SELECTSHEN01&nbsp;&nbsp; &nbsp;&nbsp; |&nbsp;&nbsp; &nbsp;1 |&nbsp;&nbsp; 207 |</span><br /> <span style="font-size:14px;">1&nbsp;&nbsp; (0)| 00:00:01 |</span><br /> <br /> <span style="font-size:14px;">|*&nbsp; 2 |&nbsp;&nbsp; INDEX RANGE SCAN&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; | IDX_SELECTSHEN01_02 |&nbsp;&nbsp; &nbsp;1 |&nbsp;&nbsp; &nbsp;&nbsp; |</span><br /> <span style="font-size:14px;">1&nbsp;&nbsp; (0)| 00:00:01 |</span><br /> <br /> <span style="font-size:14px;">--------------------------------------------------------------------------------</span><br /> <br /> <span style="font-size:14px;">SQL&gt; 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/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: oracle直方图
请登录后发表评论 登录
全部评论

注册时间:2015-11-26

  • 博文量
    11
  • 访问量
    15184