有没有可能在sparksql中优化交叉连接?要求填充一列 band_id
基于另一个表中定义的年龄范围。到目前为止,我已经能够通过 Cross Join
以及 WHERE
条款。但是,我希望是否有更好的方法来编写代码并缓解性能问题。我能用广播提示吗(sql(以下提供)
客户:(10米记录)
id | name | age
X1 | John | 22
V2 | Mark | 29
F4 | Peter| 42
年龄带表:(10条记录)
band_id | low_age | high_age
B123 | 10 | 19
X745 | 20 | 29
P134 | 30 | 39
Q245 | 40 | 50
预期产量:
id | name | age | band_id
X1 | John | 22 | X745
V2 | Mark | 29 | X745
F4 | Peter| 42 | Q245
查询:
select
from cust a
cross join age_band b
where a.age between b.low_age and b.high_age;
请告知。
2条答案
按热度按时间h9vpoimq1#
从
SparkStrategies.scala
在你的情况下,似乎你可以,但你不必指定cross
或者broadcast
提示,因为spark无论如何都会选择广播嵌套循环联接:9rbhqvlz2#
您不需要使用
cross join
但是left join
够了。当我同时执行这两种操作时,查询执行的物理计划略有不同。我更喜欢用后者。