在运行时添加对 AWS Athena / SQL 中的所有行都相同的列的最快方法



我正在使用 aws athena 创建一个表。创建此表时,我想将创建日期(如 2019-09-05(作为列添加到表中。最快的方法是什么?

以下是一些可能的方法(注意:current_date是一个presto函数,更多详细信息请点击此处:https://prestodb.github.io/docs/current/functions/datetime.html(:

1. select [
...,
current_date
]
from a;
2. with variables as (select current_date as date_created)
select [
...,
variables.date_created
]
from a, variables;
3. Using python to replace the expression
select [
...,
<REPLACE_ME>
]
from a;
# In python
s = query.replace("<REPLACE_ME>", datetime.now())
# run query in python

据我所知,方法 3 将是最快的,但是只能使用 sql 吗?方法 2 创建一个笛卡尔积,所以如果我们想添加多个列并且方法 1 为每一行执行函数,这可能是一个问题。

那么,最快和最好的方法是什么?由于我使用的是基于 presto 的 athena,因此无法使用变量 afaik。谢谢。

第一种方法是最好的:

SELECT <all other columns>, current_date
FROM ...

current_date将执行一次。其值在查询规划期间内联。任何其他确定性标量表达式也会发生同样的情况。

最新更新