Oracle虚拟索引
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle虚拟索引相关的知识,希望对你有一定的参考价值。
从9.2版本开始Oracle引入了虚拟索引的概念,虚拟索引是一个“伪造”的索引,它的定义只存在数据字典中并有存在相关的索引段。虚拟索引是为了在不真正创建索引的情况下,验证如果使用索引sql执行计划是否改变,执行效率是否能得到提高。
本文在11.2.0.4版本中测试使用虚拟索引
1、创建测试表
[email protected]> create table test_t as select * from dba_objects; Table created. [email protected]> select count(*) from test_t; COUNT(*) ---------- 86369
2、查看一个SQL的执行计划,由于没有创建索引,使用TABLE ACCESS FULL访问表
[email protected]> set autotrace traceonly explain [email protected]> select object_name from test_t where object_id=123; Execution Plan ---------------------------------------------------------- Plan hash value: 2946757696 ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 14 | 1106 | 344 (1)| 00:00:05 | |* 1 | TABLE ACCESS FULL| TEST_T | 14 | 1106 | 344 (1)| 00:00:05 | ---------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("OBJECT_ID"=123) Note ----- - dynamic sampling used for this statement (level=2)
3、创建虚拟索引,数据字典中有这个索引的定义但是并没有实际创建这个索引段
[email protected]> set autotrace off [email protected]> create index idx_virtual on test_t (object_id) nosegment; Index created. [email protected]> select object_name,object_type from user_objects where object_name=‘IDX_VIRTUAL‘; OBJECT_NAME OBJECT_TYPE -------------------------------------------------------------------------------------------------------------------------------- ------------------- IDX_VIRTUAL INDEX [email protected]> select segment_name,tablespace_name from user_segments where segment_name=‘IDX_VIRTUAL‘; no rows selected
4、再次查看执行计划
[email protected]> set autotrace traceonly explain [email protected]> select object_name from test_t where object_id=123; Execution Plan ---------------------------------------------------------- Plan hash value: 2946757696 ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 14 | 1106 | 344 (1)| 00:00:05 | |* 1 | TABLE ACCESS FULL| TEST_T | 14 | 1106 | 344 (1)| 00:00:05 | ----------------------------------------------------------------------------
5、我们看到执行计划并没有使用上面创建的索引,要使用虚拟索引需要设置参数
[email protected]> alter session set "_use_nosegment_indexes"=true; Session altered.
6、再次查看执行计划,可以看到执行计划选择了虚拟索引,而且时间也缩短了。
[email protected]> select object_name from test_t where object_id=123; Execution Plan ---------------------------------------------------------- Plan hash value: 1533029720 ------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 14 | 1106 | 5 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| TEST_T | 14 | 1106 | 5 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_VIRTUAL | 315 | | 1 (0)| 00:00:01 | ------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=123) Note ----- - dynamic sampling used for this statement (level=2)
从上面的执行计划可以看出创建这个索引会起到优化的效果,这个功能在大表建联合索引优化能起到很好的做作用,可以测试多个列组合哪个组合效果最好,而不需要实际每个组合都创建一个大索引。
7、删除虚拟索引
[email protected]> drop index idx_virtual; Index dropped.
MOS文档:Virtual Indexes (文档 ID 1401046.1)
本文出自 “DBA Fighting!” 博客,请务必保留此出处http://hbxztc.blog.51cto.com/1587495/1890730
以上是关于Oracle虚拟索引的主要内容,如果未能解决你的问题,请参考以下文章