当前位置:网站首页>HQL语句的调优
HQL语句的调优
2022-04-23 06:15:00 【山顶看数据】
1、去重语句(用group by 来代替distinct)
Group by 也有去重的功能,具体内容如下
select distinct customer_id
from test_join_order;
=>
select customer_id
from test_join_order
group by customer_id;
在极大的数据量(且很多重复值)时,可以先group by去重,在count()计数,效率高于count(distinct col)
create table if not exists test1(
id int
)
row format delimited
fields terminated by ',';
insert into test1 values(1);
insert into test1 values(2);
insert into test1 values(1);
insert into test1 values(3);
insert into test1 values(1);
insert into test1 values(2);
insert into test1 values(4);
select count(distinct(id))
from test1;
select count(t1.id)
from(
select id
from test1
group by id) t1;
2、聚合技巧——利用窗口函数grouping sets、cube、rollup
这个窗口函数是都是上一个的完善与封装
group by quantity,sex,age
grouping sets(quantity,sex,age)group by quantity,sex,age
with cube;group by quantity,sex
with rollup;
-- 数量分布
select quantity,count(*)
from test2
group by quantity;
2 1
3 2
4 1
5 1
7 2
-- 性别分布
select sex,count(*)
from test2
group by sex;
0 3
1 4
-- 年龄分布
select age,count(*)
from test2
group by age;
16 2
19 1
21 1
23 1
34 2
缺点要分别写三次SQL,需要执行三次,重复工作,且费事
-- 优化方法(聚合结果均在同一列,分类字段用不同列来进行分区)
select quantity,sex,age,count(*)
from test2
group by quantity,sex,age
grouping sets(quantity,sex,age)
NULL NULL 16 2
NULL NULL 19 1
NULL NULL 21 1
NULL NULL 23 1
NULL NULL 34 2
NULL 0 NULL 3
NULL 1 NULL 4
2 NULL NULL 1
3 NULL NULL 2
4 NULL NULL 1
5 NULL NULL 1
7 NULL NULL 2
就是把三列的结果放在一起
以下是测试数据
create table if not exists test2(
quantity int,
sex int,
age int
)
row format delimited
fields terminated by ',';
insert into test2 values(5,1,16);
insert into test2 values(3,0,23);
insert into test2 values(4,1,16);
insert into test2 values(7,0,34);
insert into test2 values(2,0,19);
insert into test2 values(7,1,21);
insert into test2 values(3,1,34);
cube:根据group by 维度的(所有)组合进行聚合
select quantity,sex,age,count(*)
from test2
group by quantity,sex,age
grouping sets(quantity,sex,age,(sex,age),(quantity,sex),(quantity,age),(quantity,sex,age));
(sex,age)是相同sex和age的数量有多少个
以上的写法可以写成
select quantity,sex,age,count(*)
from test2
group by quantity,sex,age
with cube;
cube
英 [kjuːb] 美 [kjuːb]
n.
立方体;立方形;立方形的东西(尤指食物);立方;三次幂
vt.
求…的立方;把(食物)切成小方块
rollup主要用于分层统计,相当于是达到每层汇总的效果
select quantity ,sex ,sum(age)
from test2
group by quantity,sex
with rollup;
2 NULL 19
2 0 19
3 NULL 57
3 0 23
3 1 34
4 NULL 16
4 1 16
5 NULL 16
5 1 16
7 NULL 55
7 0 34
7 1 21
3、表的连接优化
1、小表在前,大表在后
hive假定查询中最后一个是大表,它会将其他表缓存起来,然后扫描最后那个表
2、使用相同的连接建
当对3个或者多个表进行join连接时,如果每个on子句都使用相同的连接键的话,那么只会产生一个MR job
3、尽早的过滤数据
减少每个阶段的数据量,对于分区表要加分区,同时只选择需要使用到的字段
当逻辑过于复杂时,引入中间表
4、如何解决数据倾斜
1、数据倾斜的表现:
任务进度长时间维持在99%(100%),查看任务监控页面,发现只要少量(1个或几个)reduce子任务未完成。因为其处理的数据量和其他reduce差异过大
2、数据倾斜的原因与解决方法:
1、空值产生的数据倾斜
解决:如果两个表连接时,使用的连接条件有很多空值,建议在连接条件中增加过滤
eg:on a.user_id = b.user_id and a.user_id is not null
2、大小表连接(其中一张表很大,另一张表非常小)
解决:将小表放到内存里,在map端做join
select /*+mapjoin(a)*/ a.user_id,a.user_name
from user_info a
join join_order b
where b.customer_id = a.customer_id;
把数据提前放到内存中的方法
/*+mapjoin(a)*/
3、两个表连接条件的字段数据类型不一致
解决:将连接条件的字段数据类型转换成一致的
on a.user_id = cast(b.user_id as string)
版权声明
本文为[山顶看数据]所创,转载请带上原文链接,感谢
https://blog.csdn.net/li1579026891/article/details/121885097
边栏推荐
- GIS实战应用案例100篇(五十一)-ArcGIS中根据指定的范围计算nc文件逐时次空间平均值的方法
- 北峰油气田自组网无线通信对讲系统解决方案
- 美摄科技受邀LVSon2020大会 分享《AI合成虚拟人物的技术框架与挑战》
- Warning "force fallback to CPU execution for node: gather_191" in onnxruntime GPU 1.7
- x86架构初探之8086
- Device Tree 详解
- 基于Labview上位机的51单片机步进电机控制系统(上位机代码+下位机源码+ad原理图+51完整开发环境)
- 广西电网|应急空天一体化通信系统方案
- JDBC连接池
- 【点云系列】DeepMapping: Unsupervised Map Estimation From Multiple Point Clouds
猜你喜欢
随机推荐
网络层重要知识(面试、复试、期末)
初探智能指针之std::shared_ptr、std::unique_ptr
应急医疗通讯解决方案|MESH无线自组网系统
AUTOSAR从入门到精通100讲(八十三)-BootLoader自我刷新
Wechat applet uses wxml2canvas plug-in to generate some problem records of pictures
go语言:在函数间传递切片
【点云系列】 场景识别类导读
AUTOSAR从入门到精通100讲(八十七)-高级EEA的关键利器-AUTOSAR与DDS
基于51单片机的体脂检测系统设计(51+oled+hx711+us100)
重大安保事件应急通信系统解决方案
PyTorch 10. 学习率
江宁医院DMR系统解决方案
Paddleocr image text extraction
美摄科技推出桌面端专业视频编辑解决方案——美映PC版
PyTorch 19. PyTorch中相似操作的区别与联系
F. The wonderful use of pad
scons 搭建嵌入式arm编译
SHA512/384 原理及C语言实现(附源码)
美摄科技起诉天目传媒使用火山引擎侵权代码的声明
LPDDR4笔记









