程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> Oracle11g新特性invisibleindex(不可見的索引)

Oracle11g新特性invisibleindex(不可見的索引)

編輯:Oracle教程

Oracle11g新特性invisibleindex(不可見的索引)


如果一張表上有十幾個索引,你有什麼感受?顯然會拖慢增、刪、改的速度,不要指望開發人員能建好索引。我的處理方法是先監控很長的一段時間,看哪些索引沒有用到,然後刪除。但刪除以後,如果發現某一天有用,那又要重新建,如果是大表,那就有些麻煩。現在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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved