一、执行计划(Explain)

1)基本语法

EXPLAIN [EXTENDED | DEPENDENCY | AUTHORIZATION] query

2)案例实操

(1)查看下面这条语句的执行计划

没有生成 MR 任务的

hive (default)> explain select * from emp;
Explain
STAGE DEPENDENCIES:Stage-0 is a root stage
STAGE PLANS:Stage: Stage-0Fetch Operatorlimit: -1Processor Tree:TableScanalias: empStatistics: Num rows: 1 Data size: 7020 Basic stats: COMPLETE
Column stats: NONESelect Operatorexpressions: empno (type: int), ename (type: string), job
(type: string), mgr (type: int), hiredate (type: string), sal (type:
double), comm (type: double), deptno (type: int)outputColumnNames: _col0, _col1, _col2, _col3, _col4, _col5,
_col6, _col7Statistics: Num rows: 1 Data size: 7020 Basic stats: COMPLETE
Column stats: NONEListSink

有生成 MR 任务的

hive (default)> explain select deptno, avg(sal) avg_sal from emp group by
deptno;
Explain
STAGE DEPENDENCIES:Stage-1 is a root stageStage-0 depends on stages: Stage-1
STAGE PLANS:Stage: Stage-1Map ReduceMap Operator Tree:TableScanalias: empStatistics: Num rows: 1 Data size: 7020 Basic stats: COMPLETE
Column stats: NONESelect Operatorexpressions: sal (type: double), deptno (type: int)outputColumnNames: sal, deptnoStatistics: Num rows: 1 Data size: 7020 Basic stats: COMPLETE
Column stats: NONEGroup By Operatoraggregations: sum(sal), count(sal)keys: deptno (type: int)mode: hashoutputColumnNames: _col0, _col1, _col2Statistics: Num rows: 1 Data size: 7020 Basic stats:
COMPLETE Column stats: NONEReduce Output Operatorkey expressions: _col0 (type: int)sort order: +Map-reduce partition columns: _col0 (type: int)Statistics: Num rows: 1 Data size: 7020 Basic stats:
COMPLETE Column stats: NONEvalue expressions: _col1 (type: double), _col2 (type:
bigint)Execution mode: vectorizedReduce Operator Tree:Group By Operatoraggregations: sum(VALUE._col0), count(VALUE._col1)keys: KEY._col0 (type: int)mode: mergepartialoutputColumnNames: _col0, _col1, _col2Statistics: Num rows: 1 Data size: 7020 Basic stats: COMPLETE
Column stats: NONESelect Operatorexpressions: _col0 (type: int), (_col1 / _col2) (type: double)outputColumnNames: _col0, _col1Statistics: Num rows: 1 Data size: 7020 Basic stats: COMPLETE
Column stats: NONEFile Output Operatorcompressed: falseStatistics: Num rows: 1 Data size: 7020 Basic stats: COMPLETE
Column stats: NONEtable:input format:
org.apache.hadoop.mapred.SequenceFileInputFormatoutput format:
org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormatserde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDeStage: Stage-0Fetch Operatorlimit: -1Processor Tree:ListSink

(2)查看详细执行计划

hive (default)> explain extended select * from emp;
hive (default)> explain extended select deptno, avg(sal) avg_sal from emp
group by deptno;

二、Fetch 抓取

Fetch 抓取是指,Hive 中对某些情况的查询可以不必使用 MapReduce 计算。例如:SELECT * FROM employees;在这种情况下,Hive 可以简单地读取 employee 对应的存储目录下的文件, 然后输出查询结果到控制台。

在 hive-default.xml.template 文件中 hive.fetch.task.conversion 默认是 more,老版本 hive 默认是 minimal,该属性修改为 more 以后,在全局查找、字段查找、limit 查找等都不走 mapreduce。

<property><name>hive.fetch.task.conversion</name><value>more</value><description>Expects one of [none, minimal, more].Some select queries can be converted to single FETCH task minimizing
latency.Currently the query should be single sourced not having any subquery
and should not have any aggregations or distincts (which incurs RS),
lateral views and joins.0. none : disable hive.fetch.task.conversion1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only2. more : SELECT, FILTER, LIMIT only (support TABLESAMPLE and
virtual columns)</description>
</property>

1)案例实操:

(1)把 hive.fetch.task.conversion 设置成 none,然后执行查询语句,都会执行 mapreduce 程序。

hive (default)> set hive.fetch.task.conversion=none;
hive (default)> select * from emp;
hive (default)> select ename from emp;
hive (default)> select ename from emp limit 3;

(2)把 hive.fetch.task.conversion 设置成 more,然后执行查询语句,如下查询方式都不 会执行 mapreduce 程序。

hive (default)> set hive.fetch.task.conversion=more;
hive (default)> select * from emp;
hive (default)> select ename from emp;
hive (default)> select ename from emp limit 3;

三、本地模式

大多数的 Hadoop Job 是需要 Hadoop 提供的完整的可扩展性来处理大数据集的。不过, 有时 Hive 的输入数据量是非常小的。在这种情况下,为查询触发执行任务消耗的时间可能 会比实际 job 的执行时间要多的多。对于大多数这种情况,Hive 可以通过本地模式在单台机 器上处理所有的任务。对于小数据集,执行时间可以明显被缩短。

用户可以通过设置 hive.exec.mode.local.auto 的值为 true,来让 Hive 在适当的时候自动 启动这个优化。

set hive.exec.mode.local.auto=true; //开启本地 mr
//设置 local mr 的最大输入数据量,当输入数据量小于这个值时采用 local mr 的方式,默认
为 134217728,即 128M
set hive.exec.mode.local.auto.inputbytes.max=50000000;
//设置 local mr 的最大输入文件个数,当输入文件个数小于这个值时采用 local mr 的方式,默
认为 4
set hive.exec.mode.local.auto.input.files.max=10;

1)案例实操:

(2)关闭本地模式(默认是关闭的),并执行查询语句

hive (default)> select count(*) from emp group by deptno;

(1)开启本地模式,并执行查询语句

hive (default)> set hive.exec.mode.local.auto=true;
hive (default)> select count(*) from emp group by deptno;

四、表的优化

1、小表大表 Join(MapJOIN)

将 key 相对分散,并且数据量小的表放在 join 的左边,可以使用 map join 让小的维度表 先进内存。在 map 端完成 join。

实际测试发现:新版的 hive 已经对小表 JOIN 大表和大表 JOIN 小表进行了优化。小表放 在左边和右边已经没有区别。

1.1、案例实操

1)需求介绍

测试大表 JOIN 小表和小表 JOIN 大表的效率

2)开启 MapJoin 参数设置

(1)设置自动选择 Mapjoin

set hive.auto.convert.join = true; 默认为 true

(2)大表小表的阈值设置(默认 25M 以下认为是小表):

set hive.mapjoin.smalltable.filesize = 25000000;

3)MapJoin 工作机制

4)建大表、小表和 JOIN 后表的语句

// 创建大表
create table bigtable(id bigint, t bigint, uid string, keyword string, url_rank int, click_num int, click_url string
) row format delimited fields terminated by '\t';// 创建小表
create table smalltable(id bigint, t bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';// 创建 join 后表的语句
create table jointable(id bigint, t bigint, uid string, keyword string, url_rank int, click_num int, click_url string
) row format delimited fields terminated by '\t';

5)分别向大表和小表中导入数据

hive (default)> load data local inpath '/opt/module/data/bigtable' into
table bigtable;
hive (default)>load data local inpath '/opt/module/data/smalltable' into
table smalltable;

6)小表 JOIN 大表语句

insert overwrite table jointable
select b.id, b.t, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from smalltable s
join bigtable b
on b.id = s.id;

7)大表 JOIN 小表语句

insert overwrite table jointable
select b.id, b.t, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from bigtable b
join smalltable s
on s.id = b.id;

2、大表 Join 大表

1)空 KEY 过滤

有时 join 超时是因为某些 key 对应的数据太多,而相同 key 对应的数据都会发送到相同 的 reducer 上,从而导致内存不够。此时我们应该仔细分析这些异常的 key,很多情况下, 这些 key 对应的数据是异常数据,我们需要在 SQL 语句中进行过滤。例如 key 对应的字段为 空,操作如下:

(1)配置历史服务器

配置 mapred-site.xml

<property><name>mapreduce.jobhistory.address</name><value>hadoop20:10020</value>
</property>
<property><name>mapreduce.jobhistory.webapp.address</name><value>hadoop20:19888</value>
</property>

启动历史服务器

sbin/mr-jobhistory-daemon.sh start historyserver

查看 jobhistory

http://hadoop20:19888/jobhistory

(2)创建原始数据空 id 表

// 创建空 id 表
create table nullidtable(id bigint, t bigint, uid string, keyword string, url_rank int, click_num int, click_url string
) row format delimited fields terminated by '\t';

(3)分别加载原始数据和空 id 数据到对应表中

hive (default)> load data local inpath '/opt/module/data/nullid' into> table nullidtable;
Loading data to table default.nullidtable
OK
Time taken: 2.045 seconds
hive (default)>

数据例子:

\N   20111230000005  57375476989eea12893c0c3811607bcf    奇艺高清    1   1   http:2879www.123qiyi.com/
\N  20111230000005  66c5bb7774e31d0a22278249b26bc83a    凡人修仙传   3   1   http:2879www.123booksky.org/BookDetail.aspx?BookID=1050804&Level=1
\N  20111230000007  b97920521c78de70ac38e3713f524b50    本本联盟    1   1   http:2879www.123bblianmeng.com/
\N  20111230000008  6961d0c97fe93701fc9c0d861d096cd9    华南师范大学图书馆   1   1   http:2879lib.scnu.edu.cn/
\N  20111230000008  f2f5a21c764aebde1e8afcc2871e086f    在线代理    2   1   http:2879proxyie.cn/
\N  20111230000009  96994a0480e7e1edcaef67b20d8816b7    伟大导演    1   1   http:2879movie.douban.com/review/1128960/
\N  20111230000009  698956eb07815439fe5f46e9a4503997    youku   1   1   http:2879www.123youku.com/
\N  20111230000009  599cd26984f72ee68b2b6ebefccf6aed    安徽合肥365房产网  1   1   http:2879hf.house365.com/
\N  20111230000010  f577230df7b6c532837cd16ab731f874    哈萨克网址大全 1   1   http:2879www.123kz321.com/

(4)测试不过滤空 id

hive (default)> insert overwrite table jointable select n.* from
nullidtable n left join bigtable o on n.id = o.id;

(5)测试过滤空 id(推荐)

hive (default)> insert overwrite table jointable select n.* from (select
* from nullidtable where id is not null) n left join bigtable o on n.id =
o.id;

2)空 key 转换

有时虽然某个 key 为空对应的数据很多,但是相应的数据不是异常数据,必须要包含在 join 的结果中,此时我们可以表 a 中 key 为空的字段赋一个随机的值,使得数据随机均匀地 分不到不同的 reducer 上。例如:

2.1)不随机分布空 null 值:

(1)设置 5 个 reduce 个数

set mapreduce.job.reduces = 5;

(2)JOIN 两张表

insert overwrite table jointable
select n.* from nullidtable n left join bigtable b on n.id = b.id;

结果:如下图所示,可以看出来,出现了数据倾斜,某些 reducer 的资源消耗远大于其 他 reducer。

2.2)、随机分布空 null 值

(1)设置 5 个 reduce 个数

set mapreduce.job.reduces = 5;

(2)JOIN 两张表

insert overwrite table jointable
select n.* from nullidtable n full join bigtable o on
nvl(n.id,rand()) = o.id;

结果:如下图所示,可以看出来,消除了数据倾斜,负载均衡 reducer 的资源消耗

3)SMB(Sort Merge Bucket join)

(1)创建第二张大表

create table bigtable2(id bigint,t bigint,uid string,keyword string,url_rank int,click_num int,click_url string)
row format delimited fields terminated by '\t';
load data local inpath '/opt/module/data/bigtable' into table bigtable2;

测试大表直接 JOIN

insert overwrite table jointable
select b.id, b.t, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from bigtable s
join bigtable2 b
on b.id = s.id;

(2)创建分通表 1,桶的个数不要超过可用 CPU 的核数

create table bigtable_buck1(id bigint,t bigint,uid string,keyword string,url_rank int,click_num int,click_url string)
clustered by(id)
sorted by(id)
into 6 buckets
row format delimited fields terminated by '\t';
load data local inpath '/opt/module/data/bigtable' into table
bigtable_buck1;

(3)创建分通表 2,桶的个数不要超过可用 CPU 的核数

create table bigtable_buck2(id bigint,t bigint,uid string,keyword string,url_rank int,click_num int,click_url string)
clustered by(id)
sorted by(id)
into 6 buckets
row format delimited fields terminated by '\t';
load data local inpath '/opt/module/data/bigtable' into table
bigtable_buck2;

(4)设置参数

set hive.optimize.bucketmapjoin = true;
set hive.optimize.bucketmapjoin.sortedmerge = true;
set
hive.input.format=org.apache.hadoop.hive.ql.io.BucketizedHiveInputFormat;

(5)测试

insert overwrite table jointable
select b.id, b.t, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from bigtable_buck1 s
join bigtable_buck2 b
on b.id = s.id;

五、Group By

默认情况下,Map 阶段同一 Key 数据分发给一个 reduce,当一个 key 数据过大时就倾斜 了

并不是所有的聚合操作都需要在 Reduce 端完成,很多聚合操作都可以先在 Map 端进行 部分聚合,最后在 Reduce 端得出最终结果。

1)开启 Map 端聚合参数设置

(1)是否在 Map 端进行聚合,默认为 True

set hive.map.aggr = true

(2)在 Map 端进行聚合操作的条目数目

set hive.groupby.mapaggr.checkinterval = 100000

(3)有数据倾斜的时候进行负载均衡(默认是 false)

set hive.groupby.skewindata = true

当选项设定为 true,生成的查询计划会有两个 MR Job。第一个 MR Job 中,Map 的输出 结果会随机分布到 Reduce 中,每个 Reduce 做部分聚合操作,并输出结果,这样处理的结果 是相同的 Group By Key 有可能被分发到不同的 Reduce 中,从而达到负载均衡的目的;第二 个 MR Job 再根据预处理的数据结果按照 Group By Key 分布到 Reduce 中(这个过程可以保证 相同的 Group By Key 被分布到同一个 Reduce 中),最后完成最终的聚合操作。

hive (default)> select deptno from emp group by deptno;
Stage-Stage-1: Map: 1 Reduce: 5 Cumulative CPU: 23.68 sec HDFS Read:
19987 HDFS Write: 9 SUCCESS
Total MapReduce CPU Time Spent: 23 seconds 680 msec
OK
deptno
10
20
30

优化以后

hive (default)> set hive.groupby.skewindata = true;
hive (default)> select deptno from emp group by deptno;
Stage-Stage-1: Map: 1 Reduce: 5 Cumulative CPU: 28.53 sec HDFS Read:
18209 HDFS Write: 534 SUCCESS
Stage-Stage-2: Map: 1 Reduce: 5 Cumulative CPU: 38.32 sec HDFS Read:
15014 HDFS Write: 9 SUCCESS
Total MapReduce CPU Time Spent: 1 minutes 6 seconds 850 msec
OK
deptno
10
20
30

六、Count(Distinct) 去重统计

数据量小的时候无所谓,数据量大的情况下,由于 COUNT DISTINCT 操作需要用一个 Reduce Task 来完成,这一个 Reduce 需要处理的数据量太大,就会导致整个 Job 很难完成, 一般 COUNT DISTINCT 使用先 GROUP BY 再 COUNT 的方式替换,但是需要注意 group by 造成 的数据倾斜问题

1)案例实操

(1)创建一张大表

create table bigtable(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string
) row format delimited fields terminated by '\t';

(2)加载数据

hive (default)> load data local inpath '/opt/module/data/bigtable' into
table bigtable;

(3)设置 5 个 reduce 个数

set mapreduce.job.reduces = 5;

(4)执行去重 id 查询

hive (default)> select count(distinct id) from bigtable;
Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 7.12 sec HDFS Read:
120741990 HDFS Write: 7 SUCCESS
Total MapReduce CPU Time Spent: 7 seconds 120 msec
OK
c0
100001
Time taken: 23.607 seconds, Fetched: 1 row(s)

(5)采用 GROUP by 去重 id

hive (default)> select count(id) from (select id from bigtable group by
id) a;
Stage-Stage-1: Map: 1 Reduce: 5 Cumulative CPU: 17.53 sec HDFS Read:
120752703 HDFS Write: 580 SUCCESS
Stage-Stage-2: Map: 1 Reduce: 1 Cumulative CPU: 4.29 sec2 HDFS Read:
9409 HDFS Write: 7 SUCCESS
Total MapReduce CPU Time Spent: 21 seconds 820 msec
OK
_c0
100001
Time taken: 50.795 seconds, Fetched: 1 row(s)

虽然会多用一个 Job 来完成,但在数据量大的情况下,这个绝对是值得的。

七、笛卡尔积

尽量避免笛卡尔积,join 的时候不加 on 条件,或者无效的 on 条件,Hive 只能使用 1 个 reducer 来完成笛卡尔积。

八、行列过滤

列处理:在 SELECT 中,只拿需要的列,如果有分区,尽量使用分区过滤,少用 SELECT *。

行处理:在分区剪裁中,当使用外关联时,如果将副表的过滤条件写在 Where 后面, 那么就会先全表关联,之后再过滤,比如:

1)测试先关联两张表,再用 where 条件过滤

hive (default)> select o.id from bigtable b
join bigtable o on o.id = b.id
where o.id <= 10;

Time taken: 34.406 seconds, Fetched: 100 row(s)

2)通过子查询后,再关联表

hive (default)> select b.id from bigtable b
join (select id from bigtable where id <= 10) o on b.id = o.id;

Time taken: 30.058 seconds, Fetched: 100 row(s)

hive3之执行计划(Explain)、Fetch 抓取、本地模式、表的优化、Group By、笛卡尔积、行列过滤相关推荐

  1. MySQL优化篇:执行计划explain中key_len计算方式

    概述 key_len表示索引使用的字节数,根据这个值可以判断索引的使用情况,特别是在使用联合索引的时候,判断该索引有多少部分被使用到非常重要. key_len的长度计算公式很重要(key_len越小, ...

  2. mysql 执行计划extra_mysql执行计划explain type和extra

    mysql执行计划,搞定type和extra就能优化大部分sql了.type为主,extra为辅. type: system表只有一行,MyISAM引擎. const常量连接,表最多只有一行匹配,通用 ...

  3. Wireshark使用(捕获过滤器、显示过滤器、TCP交互抓包示例、抓取本地回环数据包等)

    1.捕获过滤器规则 1.1 作用   捕获过滤器在开始捕捉之前设置,用于从源头控制被过滤的包内容,仅符合规则的包会被捕获并记录进捕获日志文件. 1.2 语法规则 字段:[Protocol][Direc ...

  4. mysql 数据库优化之执行计划(explain)简析

    数据库优化是一个比较宽泛的概念,涵盖范围较广.大的层面涉及分布式主从.分库.分表等:小的层面包括连接池使用.复杂查询与简单查询的选择及是否在应用中做数据整合等:具体到sql语句执行效率则需调整相应查询 ...

  5. MySQL执行计划EXPLAIN详解

    说明 该文章针对InnoDB存储引擎B树索引的优化,前提要对B树索引有一定的理解. 查询SQL执行加载顺序 这是我们写SQL的流程: SELECT a.`name` FROM a LEFT JOIN ...

  6. MySQL中的执行计划(explain)

    explain关键字使用了以后,共有下述12个字段信息展示: 1.id ID列中的数据为一组数字,表示执行select语句的顺序,其取值为1.2.3...n. ID值相同时,执行顺序由上至下. ID值 ...

  7. Mysql 数据库执行计划 EXPLAIN SELECT * FROM

    文章目录 EXPLAIN SELECT * FROM 是干嘛用的 ? EXPLAIN SELECT * FROM 各个字段的含义 1. id: 2.select_type: 3. table: 4.t ...

  8. 22-08-25 MySQL高级(03)MySQL索引、索引演绎、适合加索引的情况、执行计划Explain各字段解释

    "系统,那如果我没有绑定,没有简化,我原先的人生最大的可能是怎么样的",李长生好奇一问.很快系统给出了答案. "如果宿主是小说主角的话,就活个几章" " ...

  9. mysql执行计划中性能最差的是_面试中:mysql性能调优-执行计划explain

    mysql的sql调优大家都不陌生,可是调优前都会先看下执行计划,这个是必须的. 插播图片: explain 这个是关键字执行如下: explain select * from user 结果如下: ...

  10. mysql中的执行计划_MySQL中的执行计划explain详解

    一.用法及定义: explain为sql的执行计划.在sql前面加上explain关键字即可 如:explain select * from tbl_emp; 名词解释: id:[操作表的顺序] 1. ...

最新文章

  1. Intro to Parallel Programming CUDA-第二单元
  2. Neutron 网络基本概念
  3. 交货单批次拆分(BAPI_OUTB_DELIVERY_CHANGE )并更改拣配数量,发货过账(WS_DELIVERY_UPDATE)
  4. Android中http断点下载,Android HttpURLConnection断点下载(单线程)
  5. airpods2怎么查正品 ios11系统_拼多多AirPods2开箱评测,4种办法教你验真假,10个AirPods技巧教你玩...
  6. 回想四叉树LOD地形(上)
  7. template模板函数
  8. Android自定义控件封装之自定义属性的实现
  9. 还在使用集合类完成这些功能?不妨来看看 Guava 集合类!!!
  10. WIN32汇编语言之通用对话框的使用
  11. 计算机网络原理视频学习教程
  12. 我他妈的是什么!!!!
  13. 2018年1月23日腾讯SNG-IMWeb前端工程师 电话远程面试记录
  14. 百奥赛图与TRACON共同宣布YH001(CTLA-4单抗)联合恩沃利单抗(PD-L1)一线治疗软组织肉瘤的临床试验申请获得FDA批准
  15. MyBatis(2.1) 增删改查 mysql java
  16. 思维方式决定成功(古人)
  17. ecmall 调用微信分享接口
  18. Python3 识别图片验证码的步骤
  19. Python超时机制两种办法
  20. 李健清华计算机专业,李建-西南石油大学 - 计算机科学学院

热门文章

  1. 数据库安全性控制及控制流程和常用方法
  2. 音乐指纹识别(一):音乐波形
  3. Xilinx HLS 学习笔记1
  4. CentOS7 wifi安装配置问题总结
  5. 异常来自 HRESULT:0x80070057 (E_INVALIDARG)
  6. 2021 ICPC Gran Premio de Mexico 2da Fecha - F.Flipped Factorization(PN筛)
  7. 当今排队方式方法_当今改善您的设计产品组合的5种方法
  8. 公有云、私有云、私有化_私有云与公共云的评估
  9. 英语学习回炉之绕口令
  10. 用AI让逝去的亲人照片动起来后,数百万网友泪目:原来思念这么重