oracle优化distinct,oracle 索引优化之distinct

11G R2环境:

--DISTINCT测试前的准备

drop table t purge;

create table t as select * from dba_objects;

update t set object_id=rownum;

alter table T modify OBJECT_ID not null;

update t set object_id=2;

update t set object_id=3 where rownum<=25000;

commit;

/****

在oracle10g的R2环境之后,DISTINCT由于其 HASH UNIQUE的算法导致其不会产生排序,其调整的 ALTER SESSION SET "_gby_hash_aggregation_enabled" = FALSE

****/

set linesize 1000

set autotrace traceonly

select distinct object_id from t ;

执行计划:

8aa857bdbc90dfec2b4a073b4181e681.png

可以看出以上语句执行计划,占用内存1448k,逻辑读1276。

/*不过虽然没有排序,通过观察TempSpc可知distinct消耗PGA内存进行HASH UNIQUE运算,

接下来看看建了索引后的情况,TempSpc关键字立即消失,COST也立即下降许多,具体如下*/

--为T表的object_id列建索引

create index idx_t_object_id on t(object_id);

set linesize 1000

set autotrace traceonly

select /*+index(t)*/ distinct object_id from t ;

aea07974deb7c55a4f5c5754c394ccdd.png

可以看出cost降低到525,走的索引,没有进行排序,逻辑读也降低了到176。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值