首页 > 代码库 > Oracle 11g新特性invisible index(不可见的索引)

Oracle 11g新特性invisible index(不可见的索引)

    如果一张表上有十几个索引,你有什么感受?显然会拖慢增、删、改的速度,不要指望开发人员能建好索引。我的处理方法是先监控很长的一段时间,看哪些索引没有用到,然后删除。但删除以后,如果发现某一天有用,那又要重新建,如果是大表,那就有些麻烦。现在11g提供一个新特性,不可见索引,可以建索引设置为不可见索引,CBO在评估执行计划的时候会忽略它,如果需要的时候,设置回来即可。

    还有一种用途,你在调试一条SQL语句,要建一个索引测试,而你不想影响其他的会话,用不可见索引正是时候。

SQL> drop table test purge;

SQL> create table test as select * from dba_objects;
SQL> create index ind_t_object_id on test(object_id);
SQL> exec dbms_stats.gather_table_stats(user,‘test‘,cascade => true);
SQL> set autotrace traceonly
SQL> select * from test where object_id = 10;
执行计划
----------------------------------------------------------
Plan hash value: 255872589
-----------------------------------------------------------------------------------------------
| Id  | Operation                   | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                 |     1 |   100 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST            |     1 |   100 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IND_T_OBJECT_ID |     1 |       |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("OBJECT_ID"=10)
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          4  consistent gets
          0  physical reads
          0  redo size
       1195  bytes sent via SQL*Net to client
        337  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed


SQL> alter index ind_t_object_id invisible;
SQL> select * from test where object_id = 10;
执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |   100 |   209   (1)| 00:00:03 |
|*  1 |  TABLE ACCESS FULL| TEST |     1 |   100 |   209   (1)| 00:00:03 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("OBJECT_ID"=10)
统计信息
----------------------------------------------------------
        196  recursive calls
          0  db block gets
        567  consistent gets
          0  physical reads
          0  redo size
       1195  bytes sent via SQL*Net to client
        337  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          6  sorts (memory)
          0  sorts (disk)
          1  rows processed


SQL> select /*+ index(test ind_t_object_id)*/ * from test where object_id = 10;
执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |   100 |   209   (1)| 00:00:03 |
|*  1 |  TABLE ACCESS FULL| TEST |     1 |   100 |   209   (1)| 00:00:03 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("OBJECT_ID"=10)
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        544  consistent gets
          0  physical reads
          0  redo size
       1195  bytes sent via SQL*Net to client
        337  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

--让数据库看到不可见索引,可以通过改变一个参数
SQL> alter session set optimizer_use_invisible_indexes = true;
SQL> select * from test where object_id = 10;
执行计划
----------------------------------------------------------
Plan hash value: 255872589
-----------------------------------------------------------------------------------------------
| Id  | Operation                   | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                 |     1 |   100 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST            |     1 |   100 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IND_T_OBJECT_ID |     1 |       |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("OBJECT_ID"=10)
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          4  consistent gets
          0  physical reads
          0  redo size
       1195  bytes sent via SQL*Net to client
        337  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed