虚拟索引是定义在数据字典中的伪索引,但没有相关的索引段。虚拟索引的目的是模拟索引的存--而不用真实的创建一个完整索引。这允许开发者创建虚拟索引来查看相关执行计划而不用等到真实创建完索引才能查看索引对执行计划的影响,并且不会增加存储空间的使用。如果我们观察到优化器生成了一个昂贵的执行计划并且SQL调整指导建议我们对某些的某列创建索引,但在生产数据库环境中创建索引与测试并不总是可以操作。我们需要确保创建的索引将不会对数据库中的其它查询产生负面影响,因此可以使用虚拟索引。
下面举例进行说明:
1.创建一个测试表test
- SQL> create table test as select * from dba_objects;
- Table created.
2.从表test查询object_name等于standard的记录
- SQL> select * from test where object_name='STANDARD';
- OWNER
- ------------------------------
- OBJECT_NAME
- --------------------------------------------------------------------
- SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE
- ------------------------------ ---------- -------------- -----------
- CREATED LAST_DDL_TIM TIMESTAMP STATUS T G S
- ------------ ------------ ------------------- ------- - - -
- SYS
- STANDARD
- 888 PACKAGE
- 19-APR-10 19-APR-10 2003-04-18:00:00:00 VALID N N N
- OWNER
- ------------------------------
- OBJECT_NAME
- --------------------------------------------------------------------
- SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE
- ------------------------------ ---------- -------------- -------------------
- CREATED LAST_DDL_TIM TIMESTAMP STATUS T G S
- ------------ ------------ ------------------- ------- - - -
- SYS
- STANDARD
- 889 PACKAGE BODY
- 19-APR-10 19-APR-10 2010-04-19:10:22:58 VALID N N N
3.查询上面查询的执行计划
- SQL> set autotrace traceonly explain
- SQL> select * from test where object_name='STANDARD';
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 1357081020
- --------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 8 | 1416 | 155 (1)| 00:00:02 |
- |* 1 | TABLE ACCESS FULL| TEST | 8 | 1416 | 155 (1)| 00:00:02 |
- --------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("OBJECT_NAME"='STANDARD')
- Note
- -----
- - dynamic sampling used for this statement
4.在表test的object_name列上创建一个虚拟索引
- SQL> create index test_index on test(object_name) nosegment;Index created.
为了创建虚拟索引必须在create index语句中指定nosegment子句,并且不会创建索引段。
5.来验证虚拟索引不会创建索引段
- SQL> set autotrace off
- SQL> select index_name from dba_indexes where table_name = 'TEST' and index_name = 'TEST_INDEX';
- no rows selected
- SQL> col OBJECT_NAME format a20;
- SQL> select object_name, object_type from dba_objects where object_name = 'TEST_INDEX';
- OBJECT_NAME OBJECT_TYPE
- -------------------- -------------------
- TEST_INDEX INDEX
从上面的结果可以看到索引对象已经创建,但没有创建索引段。
6.重新执行sql查看创建的虚拟索引是否被使用
- SQL> set autotrace traceonly explainSQL> select * from test where object_name='STANDARD';
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 1357081020
- --------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 8 | 1416 | 155 (1)| 00:00:02 |
- |* 1 | TABLE ACCESS FULL| TEST | 8 | 1416 | 155 (1)| 00:00:02 |
- --------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("OBJECT_NAME"='STANDARD')
- Note
- -----
- - dynamic sampling used for this statement
从上面的执行计划可以清楚地看到创建的虚拟索引并没有被使用。
7.为了能使用所创建的虚拟索引,需要将_USE_NOSEGMENT_INDEXES设置为true
- SQL> alter session set "_USE_NOSEGMENT_INDEXES" = true;Session altered.
8.重新执行sql查看创建的虚拟索引是否被使用
- SQL> set long 900SQL> set linesize 900
- SQL> select * from test where object_name='STANDARD';
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 2627321457
- --------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 8 | 1416 | 5 (0)| 00:00:01 |
- | 1 | TABLE ACCESS BY INDEX ROWID| TEST | 8 | 1416 | 5 (0)| 00:00:01 |
- |* 2 | INDEX RANGE SCAN | TEST_INDEX | 238 | | 1 (0)| 00:00:01 |
- --------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 2 - access("OBJECT_NAME"='STANDARD')
- Note
- -----
- - dynamic sampling used for this statement
从上面的执行计划可以看到当设置隐含参数_USE_NOSEGMENT_INDEXES后,优化器将会使用创建的虚拟索引。在使用虚拟索引需要注意,我们可以分析虚拟索引,但不能重建虚拟索引,如果重建虚拟索引会收到ORA-8114: "User attempted to alter a fake index"错误提示,可以删除虚拟索引。