加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle 索引优化之distinct

发布时间:2020-12-12 16:45:46 所属栏目:百科 来源:网络整理
导读: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 /****在o

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 1000set autotrace traceonly

select distinct object_id from t ;执行计划:

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

/*不过虽然没有排序,通过观察TempSpc可知distinct消耗PGA内存进行HASH UNIQUE运算,接下来看看建了索引后的情况,TempSpc关键字立即消失,COST也立即下降许多,具体如下*/

--为T表的object_id列建索引create index idx_t_object_id on t(object_id);set linesize 1000set autotrace traceonly

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读