首页 > 解决方案 > 过滤条件的性能输出

问题描述

我想知道查询中特定过滤条件的位置是否会导致显着的性能差异。

我有一个示例表 - date_dim:它始终只包含与当前执行日期有关的 1 条记录:

dt | frst_day_mth | last_day_mth
16/05/2019 | 01/05/2019 | 31/05/2019  -- Table always has only 1 row for that day

现在我有一个类似的查询:

select a.id, b.name, c.salary
from tableA a
inner join tableB b
on a.id = b.id
inner join tableC c
on b.name = c.name

现在,我必须在诸如tableA.eff_dt <= date_dim.last_mth_day. 我的问题是 - 从性能的角度来看,哪个选项(如下)是最好的?将它放在(选项1)中是否更好,ON clause以便Join记录subquery可以尽早减少,或者稍后在where子句中应用它(选项2)?表 A、B 和 C 各有大约 20 百万行。我正在使用 Spark SQL。

选项1:

select a.id, b.name, c.salary
from tableA a
inner join tableB b
on a.id = b.id
and a.eff_dt <= (select last_mth_day from date_dim) -- Using subquery early on
inner join tableC c
on b.name = c.name 

选项 2:

select a.id, b.name, c.salary
from tableA a
inner join tableB b
on a.id = b.id
inner join tableC c
on b.name = c.name
cross join date_dim dt 
where a.eff_dt <= dt.last_mth_day -- Using Later in WHERE clause

请让我知道您的意见。

标签: sqlapache-spark-sqlpyspark-sql

解决方案


由于您的查询就像根据表中的唯一值 - date_dim 从表中过滤行 - tableA。

所以我相信,无论你在哪里保留过滤器,火花查询优化器都只会读取 tableA 中与过滤条件匹配的行(这是由于下推过滤机制而发生的)。所以只有那些行参与连接。

您可以参考此链接了解更多信息: https ://jaceklaskowski.gitbooks.io/mastering-spark-sql/spark-sql-Optimizer-PushDownPredicate.html


推荐阅读