索引的使用對數據庫的性能有巨大的影響。
共有五類不同的使用模式。
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';
計劃爲全表掃描。
oracle index的5種使用模式
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.