hive SQL最佳化之distribute by和sort by

來源:互聯網
上載者:User

本文出自 “熾天使” 部落格,請務必保留此出處http://3199782.blog.51cto.com/3189782/703873

原創作品,允許轉載,轉載時請務必以超連結形式標明文章
原始出處 、作者資訊和本聲明。否則將追究法律責任。http://3199782.blog.51cto.com/3189782/703873

 最近在最佳化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秒, 效能得到大幅提升

本文出自 “熾天使” 部落格,請務必保留此出處http://3199782.blog.51cto.com/3189782/703873

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.