近期在优化hiveSQL。

以下是一段排序,分组后取每组第一行记录的SQL

  1. INSERT OVERWRITE TABLE t_wa_funnel_distinct_temp PARTITION (pt='${SRCTIME}')
  2. SELECT
  3. bussiness_id,
  4. cookie_id,
  5. session_id,
  6. funnel_id,
  7. group_first(funnel_name) funnel_name,
  8. step_id,
  9. group_first(step_name) step_name,
  10. group_first(log_type) log_type,
  11. group_first(url_pattern) url_pattern,
  12. group_first(url) url,
  13. group_first(refer) refer,
  14. group_first(log_time) log_time,
  15. group_first(is_new_visitor) is_new_visitor,
  16. group_first(is_mobile_traffic) is_mobile_traffic,
  17. group_first(is_bounce) is_bounce,
  18. group_first(campaign_name) campaign_name,
  19. group_first(group_name) group_name,
  20. group_first(slot_name) slot_name,
  21. group_first(source_type) source_type,
  22. group_first(next_page) next_page,
  23. group_first(continent) continent,
  24. group_first(sub_continent_region) sub_continent_region,
  25. group_first(country) country,
  26. group_first(region) region,
  27. group_first(city) city,
  28. group_first(language) language,
  29. group_first(browser) browser,
  30. group_first(os) os,
  31. group_first(screen_color) screen_color,
  32. group_first(screen_resolution) screen_resolution,
  33. group_first(flash_version) flash_version,
  34. group_first(java) java,
  35. group_first(host) host
  36. FROM
  37. (   SELECT *
  38. FROM r_wa_funnel
  39. WHERE pt='${SRCTIME}'
  40. ORDER BY bussiness_id, cookie_id, session_id, funnel_id, step_id, log_time ASC
  41. ) t1
  42. 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代码:

  1. INSERT OVERWRITE TABLE t_wa_funnel_distinct PARTITION (pt='2011.11.01.21')
  2. SELECT
  3. bussiness_id,
  4. cookie_id,
  5. session_id,
  6. funnel_id,
  7. group_first(funnel_name) funnel_name,
  8. step_id,
  9. group_first(step_name) step_name,
  10. group_first(log_type) log_type,
  11. group_first(url_pattern) url_pattern,
  12. group_first(url) url,
  13. group_first(refer) refer,
  14. group_first(log_time) log_time,
  15. group_first(is_new_visitor) is_new_visitor,
  16. group_first(is_mobile_traffic) is_mobile_traffic,
  17. group_first(is_bounce) is_bounce,
  18. group_first(campaign_name) campaign_name,
  19. group_first(group_name) group_name,
  20. group_first(slot_name) slot_name,
  21. group_first(source_type) source_type,
  22. group_first(next_page) next_page,
  23. group_first(continent) continent,
  24. group_first(sub_continent_region) sub_continent_region,
  25. group_first(country) country,
  26. group_first(region) region,
  27. group_first(city) city,
  28. group_first(language) language,
  29. group_first(browser) browser,
  30. group_first(os) os,
  31. group_first(screen_color) screen_color,
  32. group_first(screen_resolution) screen_resolution,
  33. group_first(flash_version) flash_version,
  34. group_first(java) java,
  35. group_first(host) host
  36. FROM
  37. (   SELECT *
  38. FROM r_wa_funnel
  39. WHERE pt='2011.11.01.21'
  40. distribute by bussiness_id, cookie_id, session_id, funnel_id, step_id sort by log_time ASC
  41. ) t1
  42. GROUP BY bussiness_id, cookie_id, session_id, funnel_id, step_id;

运行时间:

第一个须要运行6:43, 而优化有仅仅要运行0:35秒。性能得到大幅提升

hive SQL优化之distribute by和sort by的更多相关文章

  1. Hive SQL 优化面试题整理

    Hive优化目标 在有限的资源下,执行效率更高 常见问题: 数据倾斜 map数设置 reduce数设置 其他 Hive执行 HQL --> Job --> Map/Reduce 执行计划 ...

  2. 深入浅出Hive企业级架构优化、Hive Sql优化、压缩和分布式缓存(企业Hadoop应用核心产品)

    一.本课程是怎么样的一门课程(全面介绍)    1.1.课程的背景       作为企业Hadoop应用的核心产品,Hive承载着FaceBook.淘宝等大佬 95%以上的离线统计,很多企业里的离线统 ...

  3. Hive SQL优化思路

    Hive的优化主要分为:配置优化.SQL语句优化.任务优化等方案.其中在开发过程中主要涉及到的可能是SQL优化这块. 优化的核心思想是: 减少数据量(例如分区.列剪裁) 避免数据倾斜(例如加参数.Ke ...

  4. 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 常见的聚合操 ...

  5. [转]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 ...

  6. hive中order by、distribute by、sort by和cluster by的区别和联系

    hive中order by.distribute by.sort by和cluster by的区别和联系 order by order by 会对数据进行全局排序,和oracle和mysql等数据库中 ...

  7. Hive使用Calcite CBO优化流程及SQL优化实战

    目录 Hive SQL执行流程 Hive debug简单介绍 Hive SQL执行流程 Hive 使用Calcite优化 Hive Calcite优化流程 Hive Calcite使用细则 Hive向 ...

  8. 016-Hadoop Hive sql语法详解6-job输入输出优化、数据剪裁、减少job数、动态分区

    一.job输入输出优化 善用muti-insert.union all,不同表的union all相当于multiple inputs,同一个表的union all,相当map一次输出多条 示例 二. ...

  9. Hive篇---Hive使用优化

    一.前述 本节主要描述Hive的优化使用,Hive的优化着重强调一个 把Hive SQL 当做Mapreduce程序去优化 二.主要优化点 1.Hive运行方式:本地模式集群模式 本地模式开启本地模式 ...

随机推荐

  1. javafx style and cssFile

    public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...

  2. JOISC 2017 Day1 T3 烟花棒

    JOISC 2017 Day1 T3 烟花棒 题意: ​ 数轴上有\(N\)人在放烟花,一开始只有第\(K\)个人的烟花是点燃的,烟花燃烧的时间为\(T\)秒,求让所有人的烟花都可以点燃的速度的最小值 ...

  3. PHP温习之二

    1.php包含的超全局变量 (1)$GLOBALS超全局变量组,在PHP脚本所有的作用域均可以访问到. <?php $x = 23; $y = 17; function addAction(){ ...

  4. 小米开源文件管理器MiCodeFileExplorer-源码研究(9)-入口分析

    AndroidManifest.xml是Android应用程序最重要的配置文件. 入口文件和intent-filter <application android:icon="@draw ...

  5. HDFS简单介绍及用C语言訪问HDFS接口操作实践

    一.概述 近年来,大数据技术如火如荼,怎样存储海量数据也成了当今的热点和难点问题,而HDFS分布式文件系统作为Hadoop项目的分布式存储基础,也为HBASE提供数据持久化功能,它在大数据项目中有很广 ...

  6. VMware虚拟机XP系统安装

    转载:http://jingyan.baidu.com/article/54b6b9c00e2f452d593b4762.html

  7. 1.3 Quick Start中 Step 3: Create a topic官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 3: Create a topic Step 3: 创建一个主题(topi ...

  8. BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings

    继上一节.BeautifulSoup的高级应用 之 find findAll,这一节,主要解说BeautifulSoup有关的其它几个重要应用函数. 本篇中,所使用的html为: html_doc = ...

  9. 40.【IntelliJ IDEA】使用idea解决新建jsp文件而找不到jsp文件模版的新建选项

    转自:https://www.cnblogs.com/sxdcgaq8080/p/7676294.html 使用idea解决新建jsp文件而找不到jsp文件模版的新建选项,这样每次创建一个新的jsp文 ...

  10. Redis笔记教程

    一.redis简介 1.1.1.什么是redis? REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. 读 ...