近期在优化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. HDU——T 4738 Caocao's Bridges

    http://acm.hdu.edu.cn/showproblem.php?pid=4738 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  3. cocos2dx——lua自己主动和手动绑定

    [自己主动绑定] 參考:http://my.oschina.net/skyhacker2/blog/298397 主要是通过引擎自带的tools/tolua,主要过程例如以下: 1.编写好要导出的c+ ...

  4. BOM 请给javascript一个说法-------Day33

    楼市低迷,业主是不是该要个说法.黄金暴跌,谁来给大妈们一个说法.中国足球,敢不敢给大家一个说法. 给个说法,谁给,给谁,这该是哲学的范畴了吧. 可是,在这里.BOM是真真切切的给javascript一 ...

  5. searchView-风格调整

    5.1以后的searchView 风格调整属性相比于4.4有了些更改.我们先看代码 <style name="DeskClock.Theme" parent="an ...

  6. UDP 打洞示例 包含 服务器 客户端

    客户端示例: #include "Net.h" #include "../p2pInfo.h" int main() { CUdp  udp; if (0!=u ...

  7. Logstash整合Elasticsearch

    1:启动Elasticsearch2:bin/logstash -e 'input { stdin { } } output { elasticsearch { host => localhos ...

  8. 深入了解Linux远程桌面

    本文转载于:http://www.linux521.com/2009/system/201004/11001.html 已发表在<网管员世界>2010年3月杂志             本 ...

  9. js的数据类型和typeof数据类型

    js的数据类型:number,string,null,undefined,Boolean,object typeof数据类型:number,string,object,function,undefin ...

  10. Hadoop学习小结

    还在学校的时候,就知道Hadoop的存在了. 2012年在公司实习的时候,买了<Hadoop权威指南第2版>,大致看了下. 今年,抽空也大致喵了几眼. 最大的感悟就是:光看不做,还是不行. ...