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

11g新特性_索引的可見與不可見

編輯:Oracle教程

11g新特性_索引的可見與不可見


當你想評估一個索引對你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 |   <strong>TABLE ACCESS FULL</strong>| 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 |   <strong>INDEX FULL SCAN</strong>| 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                       <span style="color:#ff0000;"><strong>VALID</strong></span>    <strong>INVISIBLE</strong></p><p>SQL> select count(id) from t;</p><p> COUNT(ID)
----------
  5</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 |   <strong>TABLE ACCESS FULL</strong>| T   | 5 |    15 | 2   (0)| 00:00:01 |      --此時執行計劃裡又是走全表掃描路徑
---------------------------------------------------------------------------</p><p>SQL> alter index idx_t_id visible;
 
Index altered</p>

索引的不可見可以避免了索引重建,尤其是大表的索引,這個新特性更有利於數據庫的優化 

-------------------------------------------------------------------------------------------------

本文來自於我的技術博客 http://blog.csdn.net/robo23

轉載請標注源文鏈接,否則追究法律責任!

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