Oracle 11g之不可见的索引(二)
ible_indexes = true;
如果您无法修改代码(如第三方应用程序中的代码),该特性将十分有用。创建索引时,可以在末尾追加子句 INVISIBLE,将索引构建为对优化器不可见。您还可以使用字典视图 USER_INDEXES 查看索引的当前设置。
SQL> select visibility
2 from user_indexes
3 where index_name = 'IN_RES_GUEST';
VISIBILITY
---------
INVISIBLE
注意,如果您重新构建该索引,则该索引将变为可见。您必须再次将其显式设为不可见。
那么,该索引到底对“什么”不可见?当然,它不会对用户不可见。它只是对优化器不可见。常规数据库操作(如插入、更新和删除)将继续更新索引。创建不可见索引时应注意这一点;由于该索引,您将不会再次查看性能,但同时您在 DML 操作期间可能会付出一些代价。
-------------------------------------------------
以下是我自己做的例子,仅供参考:
------------------------------------------------
[sql]
SCOTT@chan> create table test (id number, name varchar2(20));
表已创建。
SCOTT@chan> insert into test values (1, 'JACK');
已创建 1 行。
SCOTT@chan> insert into test values (2, 'DYLAN');
已创建 1 行。
SCOTT@chan> commit;
提交完成。
SCOTT@chan> create index test_idx on test(name);
索引已创建。
SCOTT@chan> set autotrace on explain
SCOTT@chan> select * from test where name = 'DYLAN';
ID NAME
---------- --------------------
2 DYLAN
执行计划
----------------------------------------------------------
Plan hash value: 1389866015
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 1 | TABLE ACCESS BY INDEX ROWID| TEST | 1 | 25 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TEST_IDX | 1 | | 1 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("NAME"='DYLAN')
Note
-----
- dynamic sampling used for this statement (level=2)
SCOTT@chan> alter index test_idx invisible;
索引已更改。
SCOTT@chan> select * from test where name = 'DYLAN';
ID NAME
---------- --------------------
2 DYLAN
执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 25 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST | 1 | 25 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("NAME"='DYLAN')
Note
-----
- dynamic sampling used for this statement (level=2)
[sql]
SCOTT@chan> alter session set optimizer_use_invisible_indexes = true;
会话已更改。
[sql]
SCOTT@chan> select * from test where name = 'DYLAN';
ID NAME
---------- ------