SQL - 在根据另一个 table 的值检查分区字段时,我可以使用分区吗?

SQL - Can I make use of a partition when checking the partitioned field against value from another table?

我在 Athena SQL 中查询以下用例:

我有一个 table A 分区日期: 日期 |购买数量 |类别

在另一个 table B 中,我有 500 个事件发生在特定日期。我想在每个事件发生前一周访问 A 的汇总数据: 事件编号 | Event_Date | 7_Days_Before_Event_Date |类别

我想为每个事件得出事件发生日期前 7 天的购买总额。

但是,当为此例如使用 where 子句时。 A.Date between B.7_Days_Before_Event_Date and B.Event_Date A上的分区不再使用,查询所有数据,大大降低性能。

我如何在使用分区的同时获取每个事件前一周的数据,从而保持高性能?

SQL 查询:

select b.event_id, sum(a.number_of_purchases)
from dbo.tableA a
inner join dbo.tableB b on a.category = b.category
where a.date between b.7_days_before_event_date and b.event_date
group by b.event_id

A​​thena 基于 presto,在 presto 中,您的查询正在尝试为 between b.7_days_before_event_date and b.event_date 子句动态生成值,并且该值直到计划时间才知道,因此您的查询最终会扫描所有分区。

社区已经在开发一项名为 dynamic filtering 的功能,这将有助于解决此类与性能相关的问题。

您还可以参考 link,其中详细讨论了此问题以及可能的解决方法。