從9.2版本開始Oracle引入了虛擬索引的概念,虛擬索引是一個“偽造”的索引,它的定義只存在數(shù)據(jù)字典中并有存在相關(guān)的索引段。虛擬索引是為了在不真正創(chuàng)建索引的情況下,驗證如果使用索引sql執(zhí)行計劃是否改變,執(zhí)行效率是否能得到提高。
創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)承留,十載網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
本文在11.2.0.4版本中測試使用虛擬索引
1、創(chuàng)建測試表
ZX@orcl> create table test_t as select * from dba_objects; Table created. ZX@orcl> select count(*) from test_t; COUNT(*) ---------- 86369
2、查看一個SQL的執(zhí)行計劃,由于沒有創(chuàng)建索引,使用TABLE ACCESS FULL訪問表
ZX@orcl> set autotrace traceonly explain ZX@orcl> select object_name from test_t where object_id=123; Execution Plan ---------------------------------------------------------- Plan hash value: 2946757696 ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 14 | 1106 | 344 (1)| 00:00:05 | |* 1 | TABLE ACCESS FULL| TEST_T | 14 | 1106 | 344 (1)| 00:00:05 | ---------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("OBJECT_ID"=123) Note ----- - dynamic sampling used for this statement (level=2)
3、創(chuàng)建虛擬索引,數(shù)據(jù)字典中有這個索引的定義但是并沒有實際創(chuàng)建這個索引段
ZX@orcl> set autotrace off ZX@orcl> create index idx_virtual on test_t (object_id) nosegment; Index created. ZX@orcl> select object_name,object_type from user_objects where object_name='IDX_VIRTUAL'; OBJECT_NAME OBJECT_TYPE -------------------------------------------------------------------------------------------------------------------------------- ------------------- IDX_VIRTUAL INDEX ZX@orcl> select segment_name,tablespace_name from user_segments where segment_name='IDX_VIRTUAL'; no rows selected
4、再次查看執(zhí)行計劃
ZX@orcl> set autotrace traceonly explain ZX@orcl> select object_name from test_t where object_id=123; Execution Plan ---------------------------------------------------------- Plan hash value: 2946757696 ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 14 | 1106 | 344 (1)| 00:00:05 | |* 1 | TABLE ACCESS FULL| TEST_T | 14 | 1106 | 344 (1)| 00:00:05 | ----------------------------------------------------------------------------
5、我們看到執(zhí)行計劃并沒有使用上面創(chuàng)建的索引,要使用虛擬索引需要設(shè)置參數(shù)
ZX@orcl> alter session set "_use_nosegment_indexes"=true; Session altered.
6、再次查看執(zhí)行計劃,可以看到執(zhí)行計劃選擇了虛擬索引,而且時間也縮短了。
ZX@orcl> select object_name from test_t where object_id=123; Execution Plan ---------------------------------------------------------- Plan hash value: 1533029720 ------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 14 | 1106 | 5 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| TEST_T | 14 | 1106 | 5 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_VIRTUAL | 315 | | 1 (0)| 00:00:01 | ------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=123) Note ----- - dynamic sampling used for this statement (level=2)
從上面的執(zhí)行計劃可以看出創(chuàng)建這個索引會起到優(yōu)化的效果,這個功能在大表建聯(lián)合索引優(yōu)化能起到很好的做作用,可以測試多個列組合哪個組合效果最好,而不需要實際每個組合都創(chuàng)建一個大索引。
7、刪除虛擬索引
ZX@orcl> drop index idx_virtual; Index dropped.
MOS文檔:Virtual Indexes (文檔 ID 1401046.1)