日期:2014-05-16 浏览次数:20529 次
索引的使用对数据库的性能有巨大的影响。 
共有五类不同的使用模式。 
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'; 
计划为全表扫描。