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/
- distinct用法和优化
- distinct用法注意事项和q''
- mysql distinct 用法详解及优化
- mysql distinct 用法详解及优化
- distinct用法
- distinct用法
- distinct用法
- distinct用法
- DISTINCT 用法
- 数据库top和distinct的用法
- mysql的order by,group by和distinct优化
- mysql的order by,group by和distinct优化
- mysql的order by,group by和distinct优化
- mysql order by,group by和distinct优化
- mysql的order by,group by和distinct优化
- mysql的order by,group by和distinct优化
- oracle中的distinct关键字的用法和效果
- SQL中distinct和row_number() over() 的区别及用法
- scrapy配置过程遇到的问题以及解决--002
- C#、ASP.NET 将PDF转换成Swf
- 爱的来如同去一般
- 武汉理工大学计算机学院隧道项目组的公共博客使用办法
- 递推——Tiling
- distinct用法和优化
- 【C++】基类和派生类构造函数的调用顺序
- 2-0. 整数四则运算(10)
- 如何在oracle中导入dmp数据库文件
- PAT Advanced Level 1057
- webStroe 快捷键
- 2-1. 厘米换算英尺英寸(15)
- eclipse4.2集成github
- 阿士大夫阿士大夫