A Definitive Guide To Hive Performance Tuning- 10 Excellent Tips

原文链接 https://www.hdfstutorial.com/blog/hive-performance-tuning/

You may be knowing some of these hive query optimization techniques like using parallel lines, file formats, optimizing joins, etc. But I will also discuss some advanced hive performance tuning techniques so that you can master the optimization of hive queries.

So let’s start with Hive performance tuning techniques!

  1. Use Tez to Fasten the execution
    Apache TEZ is an execution engine used for faster query execution. It fastens the query execution time to around 1x-3x times.

To use TEZ execution engine, you need to enable it instead of default Map-Reduce execution engine. TEZ can be enabled using the below query-

Set hive.execution.engine=tez;

If you are using Cloudera/Hortonworks, then you will find TEZ option in the Hive query editor as well.

  1. Enable compression in Hive
    Compression techniques reduce the amount of data being transferred and so reduces the data transfer between mappers and reducers.

For better result, you need to perform compression at both mapper and reducer side separately. Although gzip is considered as the best compression format but beware that it is not splittable and so should be applied with caution.

Other formats are snappy, lzo, bzip, etc. You can set compression at mapper and reducer side using codes below-

set mapred.compress.map.output = true;
set mapred.output.compress= true;

Also, the compressed file should not be more than few hundred MBs else it may impact the jobs.

3. Use ORC file format

ORC (optimized record columnar) is great when it comes to hive performance tuning. We can improve the query performance using ORC file format easily. You can check Hadoop file formats in detail here.

There is no barrier like in which table you can use ORC file and in response, you get faster computation and compressed file size.

It is very easy to create ORC table, and you just need to add STORED AS ORC command as shown below.
Syntax:

Create table orctbl (id int, name string, address string) stored as ORC tblproperties (“orc.compress”= “SNAPPY”);

Now simply you can also insert the data like-

Insert overwrite table orctbl select * from tbldetails;
  1. Optimize your joins

If you are using joins to fetch the results, it’s time to revise it. If you have large data in the tables, then it is not advisable to just use normal joins we use in SQL. There are many other joins like Map Join; bucket joins, etc. which can be used to improve Hive query performance.

You can do the following with joins to optimize hive queries-

Use Map Join
Map join is highly beneficial when one table is small so that it can fit into the memory. Hive has a property which can do auto-map join when enabled. Set the below parameter to true to enable auto map join.

Set hive.auto.convert.join to true to enable the auto map join. You can either set this from the command line or from the hive-site.xml file.

<property>
<name>hive.auto.convert.join</name>
<value>true</value>
<description>Whether Hive enables the optimization about converting common join into mapjoin based on the input file size</description>
</property>

Use Skew Join
Skew join is also helpful when your table is skewed. Set the hive.optimize.skewjoin property to true to enable skew join.

<property>
<name>hive.optimize.skewjoin</name>
<value>true</value>
<description>
Whether to enable skew join optimization. The algorithm is as follows: At runtime, detect the keys with a large skew. Instead of processing those keys, store them temporarily in an HDFS directory. In a follow-up map-reduce job, process those skewed keys. The same key need not be skewed for all the tables, and so, the follow-up map-reduce job (for the skewed keys) would be much faster, since it would be a map-join.
</description> </property>

Bucketed Map Join
If tables are bucketed by a particular column, you can use bucketed map join to improve the hive query performance.

You can set the below two property to enable the bucketed map join in Hive.

<property>
<name>hive.optimize.bucketmapjoin</name>
<value>true</value>
<description>Whether to try bucket mapjoin</description>
</property>
<property>
<name>hive.optimize.bucketmapjoin.sortedmerge</name>
<value>true</value>
<description>Whether to try sorted bucket merge map join</description>
</property>
  1. Use partition
    Partition is a useful concept in Hive. It is used to divide the large table based on certain column so that the whole data can be divided into small chunks. It allows you to store the data under sub-directory inside a table.

Selecting the partition table is always a critical decision, and you need to take care of future data as well as the volume of data as well. For example, if you have data of a particular location then partition based on state can be one of the ideal choices.

Here is the syntax to create partition table-

CREATE TABLE countrydata_partition
(Id int, countryname string, population int, description string)
PARTITIONED BY (country VARCHAR(64), state VARCHAR(64))
row format delimited
fields terminated by ‘\t’
stored AS textfile;

There are two types of partition in Hive-

Static partition
Dynamic partition
Static partition is the default one. To use dynamic partition in Hive, you need to set the following property-

set hive.exec.dynamic.partition=true;

set hive.exec.dynamic.partition.mode=nonstrict;

6. Bucketing can also be used

If you have more number of columns on which you want the partitions, bucketing in the hive can be a better option. We use CLUSTERED BY command to divide the tables in the bucket.

Here is the syntax to create bucketed table-

CREATE TABLE emp_bucketed_table(
ID int, name string, address string, salary string )
COMMENT ‘this is a bucketed table’
PARTITIONED BY (country VARCHAR(64))
CLUSTERED BY (state) INTO 10 BUCKETS
STORED AS TEXTFILE;

To enable bucketing in Hive, you need to set the following property-

SET hive.enforce.bucketing=true;

This should be set every time you are writing the data to the bucketed table.

7. Parallel execution

As we know, Hive converts the queries into different stages during execution. These stages are usually getting executed one after the other and thus increases the time of execution. Below are some of the normal steps involved-

• MapReduce stage
• Sampling stage
• Limit stage
• Merge stage etc.

But the good thing is, you can set some of this independent stage to process parallel. This is a parallel execution in Hive. For this, you need to set the below properties to true-

Set hive.exec.parallel = true;

8. Vectorization

Vectorization improves the query performance of all the operation like scans, aggregations, filters and joins, by performing them in batches of 1024 rows at once instead of single row each time.

Again you will have to set some parameter to enable vectorization-

set hive.vectorized.execution.enabled = true;
set hive.vectorized.execution.reduce.enabled = true;

9. Cost based optimization

Cost based optimization (CBO) is the new feature to Hive. CBO offers better hive query performance regarding cost.

To use CBO, you need to set the following properties-

set hive.cbo.enable=true;
set hive.compute.query.using.stats=true;
set hive.stats.fetch.column.stats=true;
set hive.stats.fetch.partition.stats=true;

10. Avoid Global sorting

Global sorting in Hive is getting done by the help of the command ORDER BY in the hive. But the issue is, if you’re using ORDER BY command, then the number of reducers will be set to one which can be illogical when you have large Hadoop dataset.

So when you don’t need global sorting, use SORT BY command which sorts the result per reducer.

Even you can also use DISTRIBUTE BY command if you want to control which particular rows will go with which reducer.

These were some of the best Hive performance tuning techniques one can apply to Hive. Use these techniques and improve Hive query performance easily.

Do let me know if you have any other method to improve the hive query performance.

附:https://www.infoq.cn/article/qDzg3T00sP4QA9C7Ve18

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容