近期在优化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. 原生JS实现页面内定位

    需求:点击跳转到页面指定位置 <div id="test">点击跳转到此处</div> [法一]: 利用a标签的锚点跳转 <a href=" ...

  2. docker部署mysql 实现远程连接

    1. docker search mysql    # 查看mysql版本 2. docker pull mysql:5.7   # 拉取mysql 5.7 3. docker images  # 查 ...

  3. wmic linux python

    sudo aptitude install wmi-client Example of usage is; wmic -U DOMAIN/administrator%password //10.99. ...

  4. Android Studio配置SVN 以及使用代码管理

    一.Android Studio配置SVN Android Studio关联配置SVN非常easy,在Settings里面.找到Version Control->Subversion.在这个页面 ...

  5. Android学习笔记技巧之给文本加边框

    BorderTextViews.Java package xiaosi.BorderTextView; import android.content.Context; import android.g ...

  6. java初始化过程中成员变量

    package day01; class Base{ int j; //1.j=0 Base(){ add(1); //2.调用子类add()方法 System.out.println(j); //4 ...

  7. Linux平台下使用AdventNet ManageEngine OpUtils监控网络

    AdventNet ManageEngine OpUtils 是一套系统和网络监视工具,它有Linux/Windows系统平台的免费版和企业版,该软件是一款用于监视诸如路由器,交换机,服务器或者桌面这 ...

  8. golang filepath.Walk遍历指定目录下的所有文件

    package main import ( "fmt" "os" "path/filepath" ) func walkFunc(path ...

  9. Code froces 831 A. Unimodal Array

    A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  10. Echarts Y轴min显示奇葩问题(做此记录)

    项目需求是根据不同情况显示最大值最小值   有9-35  12-50 9-50 三种情况 前面两种可以显示出来  但是9-50的话  最小值9显示不出来  8,7等就可以显示出来 后面想到强制从最小值 ...