distinct用法和优化

来源:互联网 发布:域名后缀产生 编辑:程序博客网 时间:2024/05/14 06:28

A.distinct的使用

正确的语法

select distinct column1, column2 from table;

这样就可以查询出来column1和column2不重复的行

select column1,distinct column2 from table;

这样就可以查询出一个oracle错误信息,distinct 只能用在最左边。

那么如果有这样的需求改怎么办

例如

create table ssqtest
(
 name varchar2(30),
 age number,
 sex varchar2(2)
)

有四条记录


需求去掉name重复的行,age行不做去重。,也就是2,4只能保留一条。那么age就必须要知道需要哪一条,最大的还是最小的,用group by和聚合函数处理。

如果两行age你都想要保留,那么可以先group by name和wmsys.wm_concat()函数,至于这个函数用法,不做赘述

B.distinct 的优化

大家都知道distinct 很消耗性能,那么有什么办法呢

1.使用group 代替distinct

根据http://www.itpub.net/thread-1392256-1-1.html,经过验证基本没用

2.使用exists代替distinct

参看http://blog.itpub.net/10856805/viewspace-1000690/ 眼睛看花了,而且本人没有验证,而且感觉提升空间不大

3.其他

看过很多方法,仔细研究执行计划会发现和distinct没什么具体关系。


总结(相关算法研究):

distinct oracle 改进后使用hash unique算法以后效率已经有很大提高,但是仍然会对结果排序,所以性能有一定影响。所以在非必要情况下不建议使用

BUT,看下面一种情况:我和我的小伙伴都惊呆了,

http://tech.it168.com/a2009/1228/829/000000829878.shtml

http://blog.itpub.net/768134/viewspace-1007604/


0 0
原创粉丝点击