標籤:
假設一張表上有十幾個索引,你有什麼感受?顯然會拖慢增、刪、改的速度。不要指望開發人員能建好索引。我的處理方法是先監控非常長的一段時間。看哪些索引沒實用到,然後刪除。
但刪除以後,假設發現某一天實用,那又要又一次建,假設是大表。那就有些麻煩。如今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
Oracle 11g新特性invisible index(不可見的索引)