您好,登錄后才能下訂單哦!
從9.2版本開始Oracle引入了虛擬索引的概念,虛擬索引是一個(gè)“偽造”的索引,它的定義只存在數(shù)據(jù)字典中并有存在相關(guān)的索引段。虛擬索引是為了在不真正創(chuàng)建索引的情況下,驗(yàn)證如果使用索引sql執(zhí)行計(jì)劃是否改變,執(zhí)行效率是否能得到提高。
本文在11.2.0.4版本中測(cè)試使用虛擬索引
1、創(chuàng)建測(cè)試表
ZX@orcl> create table test_t as select * from dba_objects; Table created. ZX@orcl> select count(*) from test_t; COUNT(*) ---------- 86369
2、查看一個(gè)SQL的執(zhí)行計(jì)劃,由于沒有創(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ù)字典中有這個(gè)索引的定義但是并沒有實(shí)際創(chuàng)建這個(gè)索引段
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í)行計(jì)劃
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í)行計(jì)劃并沒有使用上面創(chuàng)建的索引,要使用虛擬索引需要設(shè)置參數(shù)
ZX@orcl> alter session set "_use_nosegment_indexes"=true; Session altered.
6、再次查看執(zhí)行計(jì)劃,可以看到執(zhí)行計(jì)劃選擇了虛擬索引,而且時(shí)間也縮短了。
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í)行計(jì)劃可以看出創(chuàng)建這個(gè)索引會(huì)起到優(yōu)化的效果,這個(gè)功能在大表建聯(lián)合索引優(yōu)化能起到很好的做作用,可以測(cè)試多個(gè)列組合哪個(gè)組合效果最好,而不需要實(shí)際每個(gè)組合都創(chuàng)建一個(gè)大索引。
7、刪除虛擬索引
ZX@orcl> drop index idx_virtual; Index dropped.
MOS文檔:Virtual Indexes (文檔 ID 1401046.1)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。