當你想評估一個索引對你SQL查詢語句的影響時,恰巧你的數據庫是11g時候,可以快速將索引設置成VISIBLE或INVISIBLE。值得稱讚的時,當索引被設置成INVISIBLE時候,DML語句發生時候照常會維護索引,也就是說處於INVISIBLE狀態下的索引並不失效,只是優化器不選擇索引路徑而已。
下面通過實驗來驗證一下:
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE 11.2.0.4.0 Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
SQL> select * from t;
ID
----------
2
11
22
1
33
SQL> select count(id) from t;
COUNT(ID)
----------
5
Execution Plan
----------------------------------------------------------
Plan hash value: 2966233522
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
| 2 | TABLE ACCESS FULL| T | 5 | 15 | 2 (0)| 00:00:01 | --可以看到此時是全表掃描
在T表ID列創建唯一索引
<p>SQL> create unique index idx_t_id on t(id) invisible;</p><p>Index created.</p>SQL> select count(id) from t;
COUNT(ID)
----------
5
Execution Plan
----------------------------------------------------------
Plan hash value: 4168287108
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
| 2 | INDEX FULL SCAN| IDX_T_ID | 5 | 15 | 1 (0)| 00:00:01 |
----------------------------------------------------------------------------- --發生索引全掃描
將索引設置成INVISIBLE
SQL> alter index idx_t_id invisible;
Index altered<p>SQL> select index_name,status,visibility from dba_indexes where table_name='T';
INDEX_NAME STATUS VISIBILITY
------------------------------ -------- ----------
IDX_T_ID VALID INVISIBLE<p>SQL> select count(id) from t;
COUNT(ID)
----------
5</p><p> </p>Execution Plan
----------------------------------------------------------
Plan hash value: 2966233522</p><p>---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
| 2 | TABLE ACCESS FULL| T | 5 |15 | 2 (0)| 00:00:01 | --此時執行計劃裏又是走全表掃描路徑
---------------------------------------------------------------------------
SQL> alter index idx_t_id visible;
Index altered
索引的不可見可以避免了索引重建,尤其是大表的索引,這個新特性更有利於數據庫的優化
-------------------------------------------------------------------------------------------------
本文來自於我的技術博客 http://blog.csdn.net/robo23
轉載請標註源文鏈接,否則追究法律責任!