hive中的四种排序类型

1、Order by 全局排序

ASC(ascend): 升序(默认)
DESC(descend): 降序

**注意 :**只有一个 Reducer,即使我们在设置set reducer的数量为多个,但是在执行了order by语句之后,当前此次的运算还是只有1个reducer,因为order by要做的是全局分区。(开窗函数内是分区内排序,不在此问题)

验证:

sql 复制代码
set mapreduce.job.reduces=3;
select * from stu_scores order by math;

**总结:**order by 语句的运行效率较低,一般要配合limit 使用。

2、Sort By 在每一个Reduce的job中进行排序

设置reduce 的job数为3

sql 复制代码
set mapreduce.job.reduces=3;

查看设置的reduce 的job数量

sql 复制代码
set mapreduce.job.reduces

测试sort by:

sql 复制代码
select * from stu_scores sort by math;

**备注:**在3个reduce job中分别进行排序。

3、Distribute By 分区(结合 sort by 使用)

有些场景我们需要控制某些特定行应该到同一reducer,做一些聚集操作。

distribute by 类似 MR 中 partition(自定义分区),进行分区,结合 sort by 使用。

设置reduce 的job数为3

sql 复制代码
set mapreduce.job.reduces=3;

查看设置的reduce 的job数量

sql 复制代码
set mapreduce.job.reduces

测试 distribute by....sort by

按照stu_id分区,分区内使用math排序

sql 复制代码
select * from stu_scores distribute by stu_id sort by math;

分区逻辑:根据distribute by 后的字段hash码与reduce 的个数进行模数后,决定分区路由。

4、cluster by

当 distribute by 和 sort by 字段相同时,可以使用 cluster by 方式。但是排序只能是升序排序,不能指定排序规则为 ASC 或者 DESC。

sql 复制代码
select * from stu_scores cluster by math;
select * from stu_scores distribute by math sort by math;

总结: cluster by 等价于distribute by 和 sort by 字段的升序排序。

相关推荐
向夏威夷 梦断明暄2 小时前
基于Tez引擎的 Hive SQL 性能优化
hive·sql·性能优化
txzrxz3 小时前
拓补排序讲解
c++·算法·图论·排序
这个DBA有点耶16 小时前
交易型数据库是什么?OLTP核心能力与2026选型指南
数据库·数据仓库·sql·database·数据库架构·olap·dba
RestCloud2 天前
Informatica迁移国产ETL完整实施指南:ETLCloud自动化平滑替换方案
数据仓库·etl·etlcloud·数据传输·数据集成工具·informatica·国产化替代
德昂信息dataondemand2 天前
全量还是增量?聊聊数仓ETL中的数据同步策略
数据仓库·etl
Database_Cool_3 天前
云数据仓库开通指南:阿里云 AnalyticDB MySQL 10 分钟从 0 到分析实战教程
数据仓库·mysql·阿里云
曾阿伦4 天前
Windows 下运行 Hadoop 并部署到 AWS EMR 指南
hadoop·windows·aws
罗政5 天前
基于AI工作流的多渠道销售数据仓库清洗统计实践
数据仓库
极光代码工作室5 天前
基于Spark的日志监控与分析平台
大数据·hadoop·python·spark·数据可视化