一、查詢索引的高度
select index_name,
blevel,
leaf_blocks,
num_rows,
distinct_keys,
clustering_factor
from user_ind_statistics
where table_name in( 'T1','T2','T3');
2. 索引存儲列值(可優化聚合)
2.1索引特性之存列值優化count
drop table t purge;
create table t as select * from dba_objects;
update t set object_id=rownum;
commit;
create index idx1_object_id on t(object_id);
set autotrace on
select count(*) from t;
--count無法用到
修改代碼讓count用到索引
select count(*) from t where object_id is not null;
修改代碼讓count用到索引
修改代碼讓count用到索引
alter table t modify OBJECT_ID not null;
select count(*) from t;
2.2主鍵讓count用到索引
drop table t purge;
create table t as select * from dba_objects;
update t set object_id=rownum;
alter table t add constraint pk1_object_id primary key (OBJECT_ID);
set autotrace on
select count(*) from t;
2.3索引特性之存列值優化sum avg
drop table t purge;
create table t as select * from dba_objects;
create index idx1_object_id on t(object_id);
set autotrace on
set linesize 1000
set timing on
select sum(object_id) from t;
2.4sum avg不走索引的代價
select /*+full(t)*/ sum(object_id) from t;
3 索引本身有序(可優化排序)
3.1索引特性之有序優化order by
set autotrace traceonly
set linesize 1000
drop table t purge;
create table t as select * from dba_objects;
select * from t where object_id>2 order by object_id;
--無索引的order by 語句必然會排序
索引讓order by 語句排序消失
create index idx_t_object_id on t(object_id);
set autotrace traceonly
select * from t where object_id>2 order by object_id;
3.2 索引特性之有序優化Max/Min
--MAX/MIN 的索引優化
drop table t purge;
create table t as select * from dba_objects;
update t set object_id=rownum;
alter table t add constraint pk_object_id primary key (OBJECT_ID);
set autotrace on
set linesize 1000
select max(object_id) from t;
MAX/MIN 語句用不到索引性能低下
select /*+full(t)*/ max(object_id) from t;
3.3 MAX/MIN 用索引與數據量增加的影響
set autotrace off
drop table t_max purge;
create table t_max as select * from dba_objects;
insert into t_max select * from t_max;
insert into t_max select * from t_max;
insert into t_max select * from t_max;
insert into t_max select * from t_max;
insert into t_max select * from t_max;
select count(*) from t_max;
create index idx_t_max_obj on t_max(object_id);
set autotrace on
select max(object_id) from t_max;
4 組合索引選用
4.1 僅等值無范圍查詢時,組合的順序不影響性能
drop table t purge;
create table t as select * from dba_objects;
insert into t select * from t;
insert into t select * from t;
insert into t select * from t;
update t set object_id=rownum ;
commit;
create index idx_id_type on t(object_id,object_type);
create index idx_type_id on t(object_type,object_id);
set autotrace off
alter session set statistics_level=all ;
set linesize 366
type_id,id順序組合索引
select /*+index(t,idx_id_type)*/ * from t where object_id=20 and object_type='TABLE';
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
--id、type_id順序組合索引
select /*+index(t,idx_type_id)*/ * from t where object_id=20 and object_type='TABLE';
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
4.2 組合索引最佳順序一般是將等值查詢的列置前
將等值查詢的列置前
select /*+index(t,idx_id_type)*/ * from t where object_id>=20 and object_id<2000 and object_type='TABLE';
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';