1、缓存数据到内存
eg:df.cache()
以上的作用就是将数据缓存到内存。这个操作会将df的表结果发生改变。将行转列,一旦调用该方法df在缓存中变成了列式存储。
查询某列的时候就只扫描某列数据,就减少了扫描量提升了查询的性能,除此之外还会自动的调节压缩(最小化内存的使用率)
2、参数调优
具体详见后面的调优参数
3、数据倾斜的调优——shuffle
在进行聚合操作的使用,会产生shuffle,势必会有数据倾斜的可能。
例子(单词统计)
表(info) 数据: a b j a c d e
f o p z o a j d
select
temp.word,
count(1) as count
from
(
select explode(split(" ")) word
from info
) temp
group by temp.word
这个例子中就有聚合操作,可能会出现shuffle
解决方案1:可以在每一个单词的前面加一个前缀
处理方式:总分总
1、加上前缀后,以后我们分区就不以单词进行分区,而是以前缀和单词进行分区
2、进行局部聚合(尽量的减少shuffle,但是不能避免)
3、最后一步我们需要把前缀去掉
4、进行全局聚合
1、加前缀之后的sql
select
t1.word
concat_ws("_",cast(floor(rand()*4)as string),t1.word) as word_prefix,
from
(select explode(split(line,' ')) word from test) t1
2、在上一个基础上进行局部聚合之后的结果
select
concat_ws("_",cast(floor(rand()*4)as string),t1.word) as word_prefix,
count(1) as count
from
(select explode(split(line,' ')) word from test) t1
group by word_profix
3、去除前缀的后的代码
select
t2.word_prefix,
substr(t2.word_prefix,instr(t2.word_prefix,"_")+1) up_word
t2.count
from
(select
concat_ws("_",cast(floor(rand()*4)as string),t1.word) as word_prefix,
count(1) as count
from
(select explode(split(line,' ')) word from test) t1
group by word_profix) t2
5、进行全局聚合后的代码
select
substr(t2.word_prefix,instr(t2.word_prefix,"_")+1) up_word,
sum(t2.count) as cnts
from
(select
concat_ws("_",cast(floor(rand()*4)as string),t1.word) as word_prefix,
count(1) as count
from
(select explode(split(line,' ')) word from test) t1
group by word_profix) t2
group by up_word