sql - 过滤条件的性能输出
问题描述
我想知道查询中特定过滤条件的位置是否会导致显着的性能差异。
我有一个示例表 - 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
请让我知道您的意见。
解决方案
由于您的查询就像根据表中的唯一值 - date_dim 从表中过滤行 - tableA。
所以我相信,无论你在哪里保留过滤器,火花查询优化器都只会读取 tableA 中与过滤条件匹配的行(这是由于下推过滤机制而发生的)。所以只有那些行参与连接。
您可以参考此链接了解更多信息: https ://jaceklaskowski.gitbooks.io/mastering-spark-sql/spark-sql-Optimizer-PushDownPredicate.html
推荐阅读
- python - GCP 记录到 IoT 设备上的本地和云记录?
- node.js - 优先排序分页
- javascript - 如何在博客提要中获取最后一个元素数组
- javascript - 从 Ng-Model 创建一个数组
- html - 使用 href 的文件路径问题。存储在不同文件夹中的页面。全新的 HTML
- javascript - 如何高效处理来自 mongodb 的大数据并导出到电子表格
- swift - 视频播放器上方和下方的大空间
- material-ui - Material-ui 数据网格。将“数字”类型与右侧对齐的标题呈现的原因是什么以及如何使其看起来像其他类型?
- r - 如何以特定格式将数据框导出为 json
- css - 如何在 Laravel+ TailwindCss+ Vuejs 项目中添加和使用本地自定义字体?