
组合索引(concatenated index):由多个列构成的索引
创建组合索引例:
create index idx_detp on detp(col1,col2,col3,....)
idx_dept索引为组合索引。
在组合索引中有一个重要的概念:引导列(leading column),在上面的例子中,col1列为引导列。
当我们进行查询时where 限制条件必须有引导列。
通过案例说明
构建环境:
SQL> create table t as select * from dba_objects;
SQL> insert into t select * from t;
SQL>
SQL>
SQL> update t set object_id=rownum;
SQL> commit;
等值无范围查询时,组合索引顺序不影响性能的案例
建立不同顺序的组合索引
id+type组合:
SQL> create index idx_id_type on t(object_id,object_type);
type+id组合:
SQL> create index idx_type_id on t(object_type,object_id);
SQL> alter session set statistics_level=all;
SQL> set linesize 500
SQL> select *+index(t,idx_id_type)*/ * from t where object_id=20 and object_type='TABLE';
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
SQL> select *+index(t,idx_type_id)*/ * from t where object_id=20 and object_type='TABLE';
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
等值物无范围查询,性能一样。
组合索引最佳顺序 -- 将列等值查询条件的列置前
组合索引在条件不等的案例(条件经常是不等的,要放在后面,让等值的在前面)
不等值条件放前面的情况:
SQL> select *+index(t,idx_id_type)*/ * from t where object_id>=20 and object_id<2000 and object_type='TABLE';
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
等值的条件放前面的情况:
select /*+index(t,idx_type_id)*/ * from t where object_id>=20 and object_id<2000 and object_type='TABLE';
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
可见列等值的条件放前面的组合索引效率高。









