gpt4 book ai didi

sql - 有没有办法优化我的 bigquery sql 以便运行得更快?

转载 作者:行者123 更新时间:2023-12-03 16:54:43 27 4
gpt4 key购买 nike

有一些列我想在条件下进行筛选。

列是:point_id, weight(row currentness index 0 to 7, 0 is the most current row), localdate (YYYYmmdd), tmin(最低温度), tmax(最高温度)和precip_amount(降水量mm)。

条件是:
(本地日期 >= 20151201 和本地日期 <= 20160104)和(tmin < 0 OR tmax < 0) AND precip_amount > 40

我的目标是根据权重获取最新的行。

我编写了一个运行良好的 sql(经过 2.7 秒,处理了 64.4 MB)。

但是,有没有办法优化我的 sql 以便运行得更快?

我的 sql :

select a.point_id as point_id , a.weight as min_weight,a.localdate as local_date, a.tmin as temp_min, a.tmax as temp_max, a.precip_amount as precipitation
from table1 a
join (select point_id, min(weight) as min_weight
from
(select point_id, localdate, tmin, tmax, precip_amount,weight
from table1
where (localdate >= 20151201 and localdate <= 20160104) and (tmin < 0 or tmax < 0) and precip_amount > 40
order by weight)
group by point_id) b
on a.point_id = b.point_id and a.weight = b.min_weight
where (a.localdate >= 20151201 and a.localdate <= 20160104) and (a.tmin < 0 OR a.tmax < 0) and a.precip_amount > 40
order by a.weight, a.localdate

最佳答案

试试这个查询。我已将最小重量的计算移至子查询。

SELECT a.point_id as point_id, 
(SELECT MIN(A2.weight) FROM Table A2
WHERE A2.point_id = A.point_id
AND A2.localdate >= 20151201
AND A2.localdate <= 20160104
AND (A2.tmin < 0 or A2.tmax < 0)
AND A2.precip_amount > 40) AS min_weight,
A.localdate as local_date,
A.tmin as temp_min,
A.tmax as temp_max,
A.precip_amount as precipitation
FROM Table AS A
WHERE A.localdate >= 20151201
AND A.localdate <= 20160104
AND (A.tmin < 0 OR A.tmax < 0)
AND A.precip_amount > 40
ORDER BY A.weight, A.localdate

您也可以尝试使用窗口函数。例如

SELECT T.* FROM
(SELECT A.point_id,
MIN(A.weight) OVER(PARTITION BY A.point_id) AS min_weight,
A.localdate AS local_date,
A.tmin AS temp_min,
A.tmax AS temp_max,
A.precip_amount as precipitation
FROM Table A
WHERE A.localdate >= 20151201
AND A.localdate <= 20160104
AND (A.tmin < 0 OR A.tmax < 0)
AND A.precip_amount > 40) AS T
ORDER BY T.min_weight,
T.localdate

关于sql - 有没有办法优化我的 bigquery sql 以便运行得更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34590110/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com