oracle索引的5種使用模式
索引的使用對數據庫的性能有巨大的影響。共有五類不同的使用模式。
1。INDEX UNIQUE SCAN??? 效率最高,主鍵或唯一索引
2。INDEX FULL SCAN????? 有順序的輸出,不能并行讀索引
3。INDEX FAST FULL SCAN? 讀的最塊,可以并行訪問索引,但輸出不按順序
4。INDEX RANGE SCAN????? 給定的區間查詢
5。INDEX SKIP SCAN?????? 聯合索引,不同值越少的列,越要放在前面
--實驗后的總論。
能用唯一索引,一定用唯一索引
能加非空,就加非空約束
一定要統計表的信息,索引的信息,柱狀圖的信息。
聯合索引的順序不同,影響索引的選擇,盡量將值少的放在前面
只有做到以上四點,數據庫才會正確的選擇執行計劃。
conn system/manager
grant select any dictionary to scott;
conn scott/tiger
drop table t1 purge;
create table t1 as select * from dba_objects;
analyze table t1 compute statistics;
create index it1 on t1(object_type);
set autot traceonly
select distinct object_type from t1;
將是全表掃描,為什么不使用索引呢?因為索引中不能含有null值,
如果使用索引就可能產生不正確的結果。
--增加非空約束
alter table t1 modify (object_type not null);
select distinct object_type from t1? ;
使用INDEX FAST FULL SCAN方式查找數據
--
select? object_type from t1;
使用INDEX FAST FULL SCAN,因為不需要排序
select? object_type from t1 order by 1;
使用INDEX FULL SCAN,因為要按照順序輸出
select? object_type from t1 where object_type='TABLE';
使用INDEX RANGE SCAN
--使用非唯一索引
create index i2t1 on t1(object_id);
select * from t1 where object_id=3762;
使用INDEX RANGE SCAN,因為數據庫不知道是否唯一
--使用唯一索引
drop index i2t1;
create unique index i2t1 on t1(object_id);
使用INDEX UNIQUE SCAN,因為數據庫知道是唯一的
--跳躍的掃描索引
create index i3t1 on t1(object_type,object_name);
select * from t1 where object_name='EMP';
select object_name from t1 where object_name='EMP';
使用INDEX SKIP SCAN,因為數據庫知道可以跳過object_type,雖然object_name在第二個列。
--聯合索引的順序不同,影響索引的選擇,盡量將值少的放在前面
drop index i3t1;
drop index it1;
create index i3t1 on t1(object_name,object_type);
select * from t1 where object_type='TABLE';
計劃為全表掃描。
posted on 2010-07-26 00:28 飛熊 閱讀(519) 評論(0) 編輯 收藏 所屬分類: ORACLE