oracle 索引的几种方式

本文详细介绍了Oracle数据库中索引的使用及优化方法,包括如何通过索引提高COUNT、SUM、AVG等聚合函数的效率,利用索引的有序特性优化ORDER BY、MAX和MIN查询,以及探讨组合索引的最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、查询索引的高度

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';

 

 

 

 

 

转载于:https://www.cnblogs.com/ss-33/p/9077327.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值