ORACLE不可见索引(Invisible Indexes)

时间:2022-11-07 16:39:17

不可见索引概念

不可见索引(Invisible Index)是ORACLE 11g引入的新特性。不可见索引是会被优化器忽略的不可见索引,除非在会话或系统级别上将OPTIMIZER_USE_INVISIBLE_INDEXES初始化参数显式设置为TRUE。此参数的默认值是FALSE。如果是虚拟索引是为了合理、科学新增索引而设计的,那么不可见索引就是为了合理、科学的删除索引而设计的。为什么这样说呢? 因为DBA在维护索引时,我们经常会找出无用或低效的索引,并删除这些索引,在生产环境下,删除索引还是有一定风险的,即使ORACLE提供了监控索引使用情况的技术。例如,某些索引可能只是在一些周期的作业中被使用到,而如果监控周期没有覆盖到这些作业的触发点,就会认为索引是无用的而被删除。当作业启动后,可能就会对系统性能造成冲击。这时,可能就会手忙脚乱的去找回索引定义语句、重建索引。11G之前,我们可以先不删除索引,而将其修改为unusable。这样的话,索引的定义并未删除,只是索引不能再被使用也不会随着表数据的更新而更新。当需要重新使用该索引时,需要用rebuild语句重建、然后更新统计信息。对于一些大表来说,这个时间可能就非常长。在ORACLE 11g里提供了一个新的特性来降低直接删除索引或者禁用索引的风险,那就是索引不可见(Index Invisible)。我们可以将无用或低效的索引设置为不可见索引,当观察一段时间后,发现其对系统性能并无任何影响,那么就可以彻底删除索引了。

Beginning with Release 11g, you can create invisible indexes. An invisible index is an index that is ignored by the optimizer and the user unless you explicitly set the OPTIMIZER_USE_INVISIBLE_INDEXES initialization parameter to TRUE at the session or system level.The default value for this parameter is FALSE.

Using invisible indexes, you can do the following:

1.Test the removal of an index before dropping it.

2.Use temporary index structures for certain operations or modules of an application without affecting the overall application.

Unlike unusable indexes, an invisible index is maintained during DML statements.

不可见索引测试

创建测试表TEST,在表上新建不可见索引IDX_TEST_ID,不可见索引可以从字段VISIBILITY这个字段来区别,如下所示:

SQL> create table test

  2  as

  3  select * from dba_objects;

 

Table created.

 

SQL> create index idx_test_id on test(object_id) invisible;

 

Index created.

 

SQL> select index_name, status,visibility from dba_indexes where index_name=upper('idx_test_id');

 

INDEX_NAME                     STATUS   VISIBILIT

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

IDX_TEST_ID                    VALID    INVISIBLE

 

 

 

SQL> show parameter optimizer_use_invisible_indexes;

 

NAME                                 TYPE        VALUE

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

optimizer_use_invisible_indexes      boolean     FALSE

SQL>                                                                                    

SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS(OWNNAME =>USER,TABNAME=>'TEST',CASCADE => TRUE);

 

PL/SQL procedure successfully completed.

如下所示,优化器是“看不见”这个索引的,即使使用提示hint强制其走这个索引。优化器还是不会走索引扫描

SQL> set autotrace traceonly;

SQL> select * from test where object_id=12;

 

 

Execution Plan

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

Plan hash value: 1357081020

 

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

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

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

|   0 | SELECT STATEMENT  |      |     1 |    97 |   282   (1)| 00:00:04 |

|*  1 |  TABLE ACCESS FULL| TEST |     1 |    97 |   282   (1)| 00:00:04 |

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

Predicate Information (identified by operation id):

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

   1 - filter("OBJECT_ID"=12)

 

 

Statistics

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

        424  recursive calls

          0  db block gets

       1094  consistent gets

          0  physical reads

          0  redo size

       1604  bytes sent via SQL*Net to client

        523  bytes received via SQL*Net from client

          2  SQL*Net roundtrips to/from client

          4  sorts (memory)

          0  sorts (disk)

          1  rows processed

 

 

 

SQL> select /*+ index(text, idx_test_id) */ * from test where object_id=12;

 

 

Execution Plan

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

Plan hash value: 1357081020

 

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

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

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

|   0 | SELECT STATEMENT  |      |     1 |    97 |   282   (1)| 00:00:04 |

|*  1 |  TABLE ACCESS FULL| TEST |     1 |    97 |   282   (1)| 00:00:04 |

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

Predicate Information (identified by operation id):

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

   1 - filter("OBJECT_ID"=12)

 

 

Statistics

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

          1  recursive calls

          0  db block gets

       1036  consistent gets

          0  physical reads

          0  redo size

       1604  bytes sent via SQL*Net to client

        523  bytes received via SQL*Net from client

          2  SQL*Net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          1  rows processed

设置参数optimizer_use_invisible_indexes为true后,此时优化器就会走索引范围扫描了。

SQL> alter session set optimizer_use_invisible_indexes=true;

 

Session altered.

 

SQL> select * from test where object_id=12;

 

 

Execution Plan

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

Plan hash value: 1345973065

 

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

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

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

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

|   1 |  TABLE ACCESS BY INDEX ROWID| TEST        |     1 |    97 |     2   (0)| 00:00:01 |

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

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

Predicate Information (identified by operation id):

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

   2 - access("OBJECT_ID"=12)

 

 

Statistics

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

          0  recursive calls

          0  db block gets

          4  consistent gets

          0  physical reads

          0  redo size

       1607  bytes sent via SQL*Net to client

        523  bytes received via SQL*Net from client

          2  SQL*Net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          1  rows processed

早期版本中,不可见索引有一些Bug,下面整理了一些这方面的Bug,应用相关的补丁或最新的版本都可避免碰到这些Bug。

1:使用DBMS_METADATA.GET_DDL获取不可见索引的定义不正确,少了关键字invisible

 

详情参考:DBMS_METADATA.GET_DDL Returns Incorrect DDL For Invisible Index (文档 ID 1965563.1)

 

ORACLE不可见索引(Invisible Indexes)

 

2:Bug 9336476 - Optimizer 10053 trace shows wrong status for INVISIBLE index(文档 ID 9336476.8)

 

3:Bug 16544878 - Drop invisible index affects SQL execution plan (文档 ID 16544878.8)

 

4:Wrong Result With Invisible Index (文档 ID 1266380.1)

 

 

UPDATE statement fails with below error when all indexes are marked invisible.

ORA-14406: updated partition key is beyond highest legal partition key

The execution plan of the UPDATE is parallel plan.

 

5: Bug 9347681 - OERI [12863] with invisible indexing(文档 ID 9347681.8)

参考资料

http://blog.itpub.net/26736162/viewspace-2124044