ORACLE不可見索引(Invisible Indexes)


 

不可見索引概念

 

不可見索引(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)

 

clip_image001[4]

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM