distinct用group by优化
比如以下的SQL语句(a, b, c没有相关性):
select distinct(a), distinct(b), distinct(c) from tableName;
改进方案:把计算的distinct先group by,然后再计算:
select distinct(t.a), distinct(t.b), distinct(t.c)
from(
select a, b, c from tableName
group by a, b, c;
)t;
原理:distinct是在reduce里面计算的,再进行merge,而group by的字段会在map这里分N路hash。group by越多,hash分的越随机。
group by中avg以及sum的处理方式:
select
count(distinct a) as a, count(distinct b) as b,
count(distinct c) as c, count(distinct d) as d,
avg(e) as e,
sum(f) as f
from tablename;
处理方式如下:
select
count(distinct a) as a, count(distinct b) as b,
count(distinct c) as c, count(distinct d) as d,
sum(e)/sum(e_count) as e, // avg的处理
sum(f) as f // sum的处理
from (
select v.a, v.b, v.c, v.d,
sum(v.e) as e,
sum(v.f) as f,
sum(v.e_count) as e_count
from(
select
a, b, c, d, e, f,
case when e=null then 0 else 1 end as e_count
from tablename
)v
group by v.a, v.b, v.c, v.d
)t;
distinct用group by优化的更多相关文章
- (转)数据库 distinct 和 group by 的区别
这两者本质上应该没有可比性,distinct 取出唯一列,group by 是分组,但有时候在优化的时候,在没有聚合函数的时候,他们查出来的结果也一样. 举例来说可能方便一点. A表 id num a ...
- mysql DISTINCT 的实现与优化
DISTINCT实际上和GROUP BY的操作非常相似,只不过是在GROUP BY之后的每组中只取出一条记录而已.所以,DISTINCT的实现和GROUP BY的实现也基本差不多,没有太大的区别.同样 ...
- 44 答疑(三)--join的写法/Simple nested loop join的性能问题/Distinct和group by的性能/备库自增主键问题
44 答疑(三) Join的写法 35节介绍了join执行顺序,加了straight_join,两个问题: --1 如果用left join,左边的表一定是驱动表吗 --2 如果两个表的join包含多 ...
- 总结distinct、group by 、row_number()over函数用法及区别
distinct和group by 是一样的,查询去重,只能是全部重复的,也可以理解为针对单例,因为一行有一个字段不一样,他们就会认为这两行内容是不重复的.但是使用row_number()over这个 ...
- Mysql查询优化汇总 order by优化例子,group by优化例子,limit优化例子,优化建议
Mysql查询优化汇总 order by优化例子,group by优化例子,limit优化例子,优化建议 索引 索引是一种存储引擎快速查询记录的一种数据结构. 注意 MYSQL一次查询只能使用一个索引 ...
- MySQL中distinct和group by性能比较[转]
MySQL中distinct和group by性能比较[转] 之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论(仅在个人计算机上测试,可能不全面,仅供参考) 测试过程: 准备一张 ...
- mysql distinct跟group by性能
mysql distinct和group by性能 1,测试前的准备 //准备一张测试表 mysql> CREATE TABLE `test_test` ( -> `id` int ...
- DISTINCT 与 GROUP BY 的比较
看了很多文章,这两个SQL语句在不同的数据库上面的实现上可能有相同或有不同,但是应当要明确它们在功能概念上的区别,最终得出结论: GROUP BY 用来使用聚集函数获得值,比如 AVG, MAX, M ...
- distinct和group by的性能比较
distinct和group by的性能比较 当去重复的字段 的个数比较多的时候,group by 比distinct要快很多 当去重复的字符 的个数比较少的时候,distinct 比group by ...
随机推荐
- Atitit。D&D drag&drop拖拽功能c#.net java swing的对比与实现总结
Atitit.D&D drag&drop拖拽功能c#.net java swing的对比与实现总结 1. 实现一个D&D操作一般包括三个步骤: 1 2. .net黑头的拖曳机制 ...
- [svc]visio绘制模具
visio2016狮子XL自定义运维模具下载: https://github.com/lannyMa/scripts/blob/master/%E7%BE%8E%E5%8C%96%E5%AE%9A%E ...
- 快速创建各种类型的NSAttributeString和NSMutableParagraphStyle
NSDictionary *attributes = @{ NSForegroundColorAttributeName : [ UIColorredColor ], NSFontAttribut ...
- C++之string的底层真的是用char数组来实现的么?
一.引言 遇到一个问题:使用加密库对数据进行加密,得到密文,使用string进行保存并传输,然后可以正确解密出来,但是使用string.c_str()进行参数传递则无法正确解密出明文. 原因是:密文中 ...
- lo4j 日志级别
日志记录器(Logger)的行为是分等级的.如下表所示: 分为OFF.FATAL.ERROR.WARN.INFO.DEBUG.TRACE.ALL或者您定义的级别.Log4j建议只使用四个级别,优先级从 ...
- hdu1331(记忆化搜索)
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; typed ...
- androidStudio简便安装
最近在公司由eclipse换为androidstudio,说句实话,androidstudio还是蛮好用的,但是自己刚刚安装的时候遇到很多的问题,问了度娘,各种说法都有,还是捣鼓不得,于是自己尝试,弄 ...
- php html 转义
html_entity_decode($string);htmlentities($string);htmlspecialchars($string);htmlspecialchars_decode( ...
- spring boot初步尝试
第一次知道spring boot这个项目是为在学习spring的时候,在官网上看到的,那个时候不知道这个项目是干嘛的,也就没再继续关注了 这些天实习没什么事做,一直没用spring框架了,就想着去官网 ...
- [android] AndroidManifest.xml - 【 manifest -> application】
语法: <application android:allowTaskReparenting=["true" | "false"] android:back ...