hive 性能调优
前言
hive在大数据离线开发使用过程占比还是挺大的,熟练掌握hive调优,是每个大数据从业人员的基本要求了
目录
- SQL 优化
- 数据块大小对性能影响
- JOIN优化
- 存储格式对性能影响
- 分区表分同表
- 引擎
SQL优化
- with的使用
with语法将数据查询到内存,然后后面其它查询可以直接使用
-- with常用的几种方式
-- routine style
with a1 as ( select * from a where id between 1 and 20 )
select * from a1;
-- from style
with a1 as ( select * from a where id between 1 and 20 )
from a1
select *;
-- chaining CTEs
with c as ( select * from b where id between 5 and 10 ), -- 链式风格: 数据从a -> b -> c,b、c放内存中供查询
b as ( select * from a where id between 1 and 20 )
select * from c ;
-- union example
with a1 as (select id,name from a where id between 5 and 10),
b1 as (select id,name from b where id between 25 and 30)
select * from a1 union all select * from b1;
-- insert example
--create table b like a;-- 创建一张空表,相同表结构
create table b select * from a where 0 = 1; --创建一张空表,如果是分区表,会丢失分区分桶等信息,
with a1 as (select id, namefrom a where id between 1 and 20)
from a1
insert overwrite table b
select *;
-- ctas example (with 搭配 create table as select 建表语法)
create table b as
with a1 as (select id,name from a where id between 1 and 20)
select * from a1;
-- view example
create view v1 as
with a1 as (select id,name from a where id between 1 and 20)
select * from a1;
-- view example, name collision
create view v1 as
with a1 as (select id,name from a where id between 5 and 10)
select * from a1;
with b1 as (select id,name from a where id between 1 and 20)
select * from v1;
- from 语法使用
- distinct(count(1))与 count( group by子查询 )
数据块大小优化
todo
JOIN优化
map端join
mapJoin的主要意思就是,当链接的两个表是一个比较小的表和一个特别大的表的时候,我们把比较小的table直接放到内存中去,然后再对比较大的表格进行map操作。join就发生在map操作的时候,每当扫描一个大的table中的数据,就要去去查看小表的数据,哪条与之相符,继而进行连接。这里的join并不会涉及reduce操作。map端join的优势就是在于没有shuffle,真好。在实际的应用中,我们这样设置:
set hive.auto.convert.join=true;
common join
common join也叫做shuffle join,reduce join操作。这种情况下生再两个table的大小相当,但是又不是很大的情况下使用的。具体流程就是在map端进行数据的切分,一个block对应一个map操作,然后进行shuffle操作,把对应的block shuffle到reduce端去,再逐个进行联合,这里优势会涉及到数据的倾斜,大幅度的影响性能有可能会运行speculation,这块儿在后续的数据倾斜会讲到。
SMBJoin
smb是sort merge bucket操作,首先进行排序,继而合并,然后放到所对应的bucket中去,bucket是hive中和分区表类似的技术,就是按照key进行hash,相同的hash值都放到相同的buck中去。在进行两个表联合的时候。我们首先进行分桶,在join会大幅度的对性能进行优化。也就是说,在进行联合的时候,是table1中的一小部分和table1中的一小部分进行联合,table联合都是等值连接,相同的key都放到了同一个bucket中去了,那么在联合的时候就会大幅度的减小无关项的扫描。
set hive.auto.convert.sortmerge.join=true;
set hive.optimize.bucketmapjoin = true;
set hive.optimize.bucketmapjoin.sortedmerge = true;
set hive.auto.convert.sortmerge.join.noconditionaltask=true;
参考
参考文档
存储格式优化
todo
分区表 分桶表
todo
引擎
todo
hive使用spark引擎 https://www.cnblogs.com/lyy-blog/p/9598433.html