近期在优化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. Android ijkplayer详解使用教程

    1.认识ijkplayer 最近公司准备开发一款视频播放及直播的应用,找了许多开源的框架,大部分都是基于ffmpeg开发的.最开始准备用Vitamio框架开发的,相关的文章也比较丰富,结果对于非个人移 ...

  2. Oracle 练习

    --简单的select语句select deptno,dname,loc from DEPT where deptno='40';--描述表结构 部门表desc dept;--雇员表desc emp; ...

  3. ES6第二节:新的声明方式

    通过上一节的环境搭建完成,接下来我们就可以愉快的探索ES6的新世界了!下面我们从新的声明方式开始: 在ES6里新加了两种声明方式:let 和 const,以前我们都是用var去作声明,接下来我们一一比 ...

  4. 【例题 8-12 UVA-12627】Erratic Expansion

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 规律+递归题 f[k][i] k时刻前i行的红气球个数 i<=2^(k-1) f[k][i] = 2*f[k-1][i]; i ...

  5. 【2017 Multi-University Training Contest - Team 3】RXD's date

    [Link]: [Description] [Solution] [NumberOf WA] 1 [Reviw] [Code] #include <bits/stdc++.h> using ...

  6. java JDK设置环境变量

    1.右键"我的电脑"图标.在弹出菜单中依次选择"属性"-"高级"-"环境变量". 2.在"环境变量" ...

  7. android关键组件service服务(一)

    一. Service简单介绍 Service是android 系统中的四大组件之中的一个(Activity.Service.BroadcastReceiver.ContentProvider),它跟A ...

  8. C. Arthur and Table(Codeforces Round #311 (Div. 2) 贪心)

    C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. 教你win7解除阻止程序运行怎么操作

    教你win7解除阻止程序运行怎么操作 来源:http://www.windows7en.com/jiaocheng/27594.html 有时候我下载的软件,被win7系统禁止了运行了时软件不能使用, ...

  10. excel表如何实现多if选择结构多分支判断

    excel表如何实现多if选择结构多分支判断 一.总结 一句话总结:把多if分支转换成单if分支相加. 也可以if分支,也可以lookup函数. 1.CHOICE: +2 if band A; +1 ...