Oracle 索引的可见与隐藏(visible/invisible)

php中文网
发布: 2016-06-07 15:53:47
原创
2134人浏览过

一个不可见的索引在优化器中被忽视,除非你主动地在会话或系统级别中设置OPTIMIZER_USE_INVISIBLE_INDEXES初始化参数为TRUE。标记

官方文档:making an index invisible

An invisible index is ignored by the optimizer unless you explicitly set the OPTIMIZER_USE_INVISIBLE_INDEXES initialization parameter to TRUE at the session or system level. Making an index invisible is an alternative to making it unusable or dropping it. You cannot make an individual index partition invisible. Attempting to do so produces an error.

解释:一个不可见的索引在优化器中被忽视,除非你主动地在会话或系统级别中设置OPTIMIZER_USE_INVISIBLE_INDEXES初始化参数为TRUE。标记不可见索引可以替代不可用索引或删除索引。你不能让分区索引不可见。试图这么做会产生一个错误。

测试:

1. 创建测试表ti,根据dba_objects表。

scott@ORCL>create table ti as select * from dba_objects;

Table created.

scott@ORCL>select count(*) from ti;

  COUNT(*)

----------

    72799

2. 根据object_id列创建索引ind_ti

scott@ORCL>create index ind_ti on ti(object_id);

Index created.

3. 调整为查看执行计划

scott@ORCL>set autot trace exp
 

4. 测试索引是否生效被使用,结果索引被正常应用

scott@ORCL>select * from ti where object_id=20;

Execution Plan

----------------------------------------------------------

Plan hash value: 1655810896

--------------------------------------------------------------------------------------

| Id  | Operation                  | Name  | Rows  | Bytes | Cost (%CPU)| Time    |

--------------------------------------------------------------------------------------

|  0 | SELECT STATEMENT            |        |    1 |  207 |    2  (0)| 00:00:01 |

|  1 |  TABLE ACCESS BY INDEX ROWID| TI    |    1 |  207 |    2  (0)| 00:00:01 |

|*  2 |  INDEX RANGE SCAN          | IND_TI |    1 |      |    1  (0)| 00:00:01 |

--------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

  2 - access("OBJECT_ID"=20)

Note

-----

  - dynamic sampling used for this statement (level=2)

 

5. 将ind_ti索引改为不可见

scott@ORCL>alter index ind_ti invisible;
 

6. 再次测试时,索引没有被使用

scott@ORCL>select * from ti where object_id=20;

Execution Plan

----------------------------------------------------------

Plan hash value: 798420002

--------------------------------------------------------------------------

Designify
Designify

拖入图片便可自动去除背景✨

Designify 90
查看详情 Designify

| Id  | Operation        | Name | Rows  | Bytes | Cost (%CPU)| Time    |

--------------------------------------------------------------------------

|  0 | SELECT STATEMENT  |      |    12 |  2484 |  291  (1)| 00:00:04 |

|*  1 |  TABLE ACCESS FULL| TI  |    12 |  2484 |  291  (1)| 00:00:04 |

--------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

  1 - filter("OBJECT_ID"=20)

Note

-----

  - dynamic sampling used for this statement (level=2)

7. 数据修改

scott@ORCL>insert into ti select * from ti;

72799 rows created.

scott@ORCL>insert into ti select * from ti;

145598 rows created.

scott@ORCL>select count(*) from ti;

  COUNT(*)

----------

    291196

8. 将索引改为可见状态

scott@ORCL>alter index ind_ti visible;

Index altered.

9. 测试索引是否生效被使用,,结果索引被正常应用

scott@ORCL>set autot trace exp

scott@ORCL>select * from ti where object_id=20;

Execution Plan

----------------------------------------------------------

Plan hash value: 1655810896

--------------------------------------------------------------------------------------

| Id  | Operation                  | Name  | Rows  | Bytes | Cost (%CPU)| Time    |

--------------------------------------------------------------------------------------

|  0 | SELECT STATEMENT            |        |    4 |  828 |    2  (0)| 00:00:01 |

|  1 |  TABLE ACCESS BY INDEX ROWID| TI    |    4 |  828 |    2  (0)| 00:00:01 |

|*  2 |  INDEX RANGE SCAN          | IND_TI |    4 |      |    1  (0)| 00:00:01 |

--------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

  2 - access("OBJECT_ID"=20)

Note

-----

  - dynamic sampling used for this statement (level=2)

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号