【发布时间】:2015-07-21 15:47:10
【问题描述】:
我有一个表 VISIT_INFO,其中包含以下列:
pers_key - unique identifyer for each person
pers_name - name of person
visit_date - date at which they visited a business
还有另一个表 VALID_DATES,包含这些列:
condition - string
start_date - date
end_date - date
我目前有以下查询:
select pers_key, pers_name from VISIT_INFO a
CROSS JOIN
(select start_date, end_date from VALID_DATES where condition = 'condition1') b
WHERE (a.visit_date >= b.start_date and a.visit_date <= b.end_date)
GROUP BY a.pers_key
所以“condition1”有一个特定的 start_date 和 end_date。我需要为两个日期之间的访问过滤 VISIT_INFO。我想知道是否有更有效的方法来做到这一点。根据我目前的理解,它目前必须遍历整个表(数百万行)并将 start_date 和 end_date 添加到每一行。那么它是否必须再次遍历每一行并针对 WHERE 条件进行测试?
我问这个是因为当我删除交叉连接并对条件 1 的 start_date 和 end_date 进行硬编码时,它需要的时间大大减少。我试图避免在日期中进行硬编码,因为它会导致严重的单调乏味。
重申一下,有没有更好的方法可以按 VALID_DATES 中的特定日期过滤 VISIT_INFO?
编辑:我刚刚意识到我遗漏了大量信息,因为这些信息都在 HIVE 中。所以 EXISTS 和 join on(b 和 c 之间的 a)是不可能的。
【问题讨论】:
-
CROSS JOIN几乎从来都不是正确答案。 -
你能提供更好的选择吗?
标签: sql performance date join hive