hive SQL优化之distribute by和sort by
近期在优化hiveSQL。
以下是一段排序,分组后取每组第一行记录的SQL
- INSERT OVERWRITE TABLE t_wa_funnel_distinct_temp PARTITION (pt='${SRCTIME}')
- SELECT
- bussiness_id,
- cookie_id,
- session_id,
- funnel_id,
- group_first(funnel_name) funnel_name,
- step_id,
- group_first(step_name) step_name,
- group_first(log_type) log_type,
- group_first(url_pattern) url_pattern,
- group_first(url) url,
- group_first(refer) refer,
- group_first(log_time) log_time,
- group_first(is_new_visitor) is_new_visitor,
- group_first(is_mobile_traffic) is_mobile_traffic,
- group_first(is_bounce) is_bounce,
- group_first(campaign_name) campaign_name,
- group_first(group_name) group_name,
- group_first(slot_name) slot_name,
- group_first(source_type) source_type,
- group_first(next_page) next_page,
- group_first(continent) continent,
- group_first(sub_continent_region) sub_continent_region,
- group_first(country) country,
- group_first(region) region,
- group_first(city) city,
- group_first(language) language,
- group_first(browser) browser,
- group_first(os) os,
- group_first(screen_color) screen_color,
- group_first(screen_resolution) screen_resolution,
- group_first(flash_version) flash_version,
- group_first(java) java,
- group_first(host) host
- FROM
- ( SELECT *
- FROM r_wa_funnel
- WHERE pt='${SRCTIME}'
- ORDER BY bussiness_id, cookie_id, session_id, funnel_id, step_id, log_time ASC
- ) t1
- GROUP BY pt, bussiness_id, cookie_id, session_id, funnel_id, step_id;
group_first: 自己定义函数。用户取每组第一个字段
${SRCTIME}:
由外部oozie调度传入, 作为时间分区,精确到小时.eg: 2011.11.01.21
以下在hive上以SRCTIME = 2011.11.01.21
运行以上SQL. 2011.11.01.21小时分区记录数有10435486
运行时间:
从上面能够看出,reduce阶段仅仅有一个reduce, 这是由于ORDER BY是全局排序,hive仅仅能通过一个reduce进行排序
从业务需求来看, 仅仅要按bussiness_id, cookie_id, session_id, funnel_id, step_id分组,组内按
log_time升序排序就可以.
OK, 这样能够採用hive提供的distribute by 和 sort by,这样能够充分利用hadoop资源, 在多个
reduce中局部按log_time 排序
优化有的hive代码:
- INSERT OVERWRITE TABLE t_wa_funnel_distinct PARTITION (pt='2011.11.01.21')
- SELECT
- bussiness_id,
- cookie_id,
- session_id,
- funnel_id,
- group_first(funnel_name) funnel_name,
- step_id,
- group_first(step_name) step_name,
- group_first(log_type) log_type,
- group_first(url_pattern) url_pattern,
- group_first(url) url,
- group_first(refer) refer,
- group_first(log_time) log_time,
- group_first(is_new_visitor) is_new_visitor,
- group_first(is_mobile_traffic) is_mobile_traffic,
- group_first(is_bounce) is_bounce,
- group_first(campaign_name) campaign_name,
- group_first(group_name) group_name,
- group_first(slot_name) slot_name,
- group_first(source_type) source_type,
- group_first(next_page) next_page,
- group_first(continent) continent,
- group_first(sub_continent_region) sub_continent_region,
- group_first(country) country,
- group_first(region) region,
- group_first(city) city,
- group_first(language) language,
- group_first(browser) browser,
- group_first(os) os,
- group_first(screen_color) screen_color,
- group_first(screen_resolution) screen_resolution,
- group_first(flash_version) flash_version,
- group_first(java) java,
- group_first(host) host
- FROM
- ( SELECT *
- FROM r_wa_funnel
- WHERE pt='2011.11.01.21'
- distribute by bussiness_id, cookie_id, session_id, funnel_id, step_id sort by log_time ASC
- ) t1
- GROUP BY bussiness_id, cookie_id, session_id, funnel_id, step_id;
运行时间:
第一个须要运行6:43, 而优化有仅仅要运行0:35秒。性能得到大幅提升
hive SQL优化之distribute by和sort by的更多相关文章
- Hive SQL 优化面试题整理
Hive优化目标 在有限的资源下,执行效率更高 常见问题: 数据倾斜 map数设置 reduce数设置 其他 Hive执行 HQL --> Job --> Map/Reduce 执行计划 ...
- 深入浅出Hive企业级架构优化、Hive Sql优化、压缩和分布式缓存(企业Hadoop应用核心产品)
一.本课程是怎么样的一门课程(全面介绍) 1.1.课程的背景 作为企业Hadoop应用的核心产品,Hive承载着FaceBook.淘宝等大佬 95%以上的离线统计,很多企业里的离线统 ...
- Hive SQL优化思路
Hive的优化主要分为:配置优化.SQL语句优化.任务优化等方案.其中在开发过程中主要涉及到的可能是SQL优化这块. 优化的核心思想是: 减少数据量(例如分区.列剪裁) 避免数据倾斜(例如加参数.Ke ...
- hive的高级查询(group by、 order by、 join 、 distribute by、sort by、 clusrer by、 union all等)
查询操作 group by. order by. join . distribute by. sort by. clusrer by. union all 底层的实现 mapreduce 常见的聚合操 ...
- [转]hive中order by,distribute by,sort by,cluster by
转至http://my.oschina.net/repine/blog/296562 order by,distribute by,sort by,cluster by 查询使用说明 1 2 3 4 ...
- hive中order by、distribute by、sort by和cluster by的区别和联系
hive中order by.distribute by.sort by和cluster by的区别和联系 order by order by 会对数据进行全局排序,和oracle和mysql等数据库中 ...
- Hive使用Calcite CBO优化流程及SQL优化实战
目录 Hive SQL执行流程 Hive debug简单介绍 Hive SQL执行流程 Hive 使用Calcite优化 Hive Calcite优化流程 Hive Calcite使用细则 Hive向 ...
- 016-Hadoop Hive sql语法详解6-job输入输出优化、数据剪裁、减少job数、动态分区
一.job输入输出优化 善用muti-insert.union all,不同表的union all相当于multiple inputs,同一个表的union all,相当map一次输出多条 示例 二. ...
- Hive篇---Hive使用优化
一.前述 本节主要描述Hive的优化使用,Hive的优化着重强调一个 把Hive SQL 当做Mapreduce程序去优化 二.主要优化点 1.Hive运行方式:本地模式集群模式 本地模式开启本地模式 ...
随机推荐
- javafx style and cssFile
public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...
- JOISC 2017 Day1 T3 烟花棒
JOISC 2017 Day1 T3 烟花棒 题意: 数轴上有\(N\)人在放烟花,一开始只有第\(K\)个人的烟花是点燃的,烟花燃烧的时间为\(T\)秒,求让所有人的烟花都可以点燃的速度的最小值 ...
- PHP温习之二
1.php包含的超全局变量 (1)$GLOBALS超全局变量组,在PHP脚本所有的作用域均可以访问到. <?php $x = 23; $y = 17; function addAction(){ ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(9)-入口分析
AndroidManifest.xml是Android应用程序最重要的配置文件. 入口文件和intent-filter <application android:icon="@draw ...
- HDFS简单介绍及用C语言訪问HDFS接口操作实践
一.概述 近年来,大数据技术如火如荼,怎样存储海量数据也成了当今的热点和难点问题,而HDFS分布式文件系统作为Hadoop项目的分布式存储基础,也为HBASE提供数据持久化功能,它在大数据项目中有很广 ...
- VMware虚拟机XP系统安装
转载:http://jingyan.baidu.com/article/54b6b9c00e2f452d593b4762.html
- 1.3 Quick Start中 Step 3: Create a topic官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 3: Create a topic Step 3: 创建一个主题(topi ...
- BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings
继上一节.BeautifulSoup的高级应用 之 find findAll,这一节,主要解说BeautifulSoup有关的其它几个重要应用函数. 本篇中,所使用的html为: html_doc = ...
- 40.【IntelliJ IDEA】使用idea解决新建jsp文件而找不到jsp文件模版的新建选项
转自:https://www.cnblogs.com/sxdcgaq8080/p/7676294.html 使用idea解决新建jsp文件而找不到jsp文件模版的新建选项,这样每次创建一个新的jsp文 ...
- Redis笔记教程
一.redis简介 1.1.1.什么是redis? REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. 读 ...