Oracle 11g: Invisible Indexes

來源:互聯網
上載者:User

Oracle 11g 允許將索引標記為invisible. oracle像維護其他索引一樣維護 invisible index ,但是預設invisible index不能被最佳化器使用,除非顯式設定 OPTIMIZER_USE_INVISIBLE_INDEXES=TRUE(可以alter system/session).可以在建立索引的時候指定 INVISIBLE關鍵字或 ALTER INDEX命令來invisible一個索引。
 
CREATE INDEX idx_name on table_name(column_name) INVISIBLE;
ALTER INDEX idx_name INVISIBLE;
ALTER INDEX idx_name VISIBLE;

demo:

SQL> create table ii_tab( id number);

Table created.

SQL> begin     
  for i in 1 .. 10000 loop
  insert into ii_tab values (i);
  end loop;
  commit;
  end;
  /

PL/SQL procedure successfully completed.

SQL> create index ii_tab_id on ii_tab(id) invisible;

Index created.

SQL> exec dbms_stats.gather_table_stats(USER,'ii_tab',cascade=>TRUE);

PL/SQL procedure successfully completed.

SQL> set autotrace on
SQL> select * from ii_tab where id=9999;

        ID
----------

 

Execution Plan
----------------------------------------------------------
Plan hash value: 2057286804

----------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time    |
----------------------------------------------------------------------------
|  0 | SELECT STATEMENT  |        |    1 |    4 |    7  (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| II_TAB |    1 |    4 |    7  (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
 - filter("ID"=9999)


Statistics
----------------------------------------------------------
  recursive calls
  db block gets
  consistent gets
  physical reads
  redo size
  bytes sent via SQL*Net to client
  bytes received via SQL*Net from client
  SQL*Net roundtrips to/from client
  sorts (memory)
  sorts (disk)
  rows processed

SQL> alter session set optimizer_use_invisible_indexes=true;

Session altered.

SQL> select * from ii_tab where id=9999;

        ID
----------

 

Execution Plan
----------------------------------------------------------
Plan hash value: 81730945

------------------------------------------------------------------------------
| Id  | Operation        | Name      | Rows  | Bytes | Cost (%CPU)| Time    |
------------------------------------------------------------------------------
|  0 | SELECT STATEMENT |          |    1 |    4 |    1  (0)| 00:00:01 |
|*  1 |  INDEX RANGE SCAN| II_TAB_ID |    1 |    4 |    1  (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
 - access("ID"=9999)


Statistics
----------------------------------------------------------
  recursive calls
  db block gets
  consistent gets
  physical reads
  redo size
  bytes sent via SQL*Net to client
  bytes received via SQL*Net from client
  SQL*Net roundtrips to/from client
  sorts (memory)
  sorts (disk)
  rows processed

SQL> alter session set optimizer_use_invisible_indexes=false;

Session altered.

可以看到即使設定索引為invisible,當optimizer_use_invisible_indexes為true的時候 最佳化器仍然會走索引而非全表。
 
對比一下 ALTER INDEX ... UNUSABLE, 最佳化器不會使用UNUSABLE索引,需要 REBUILD

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.