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

标签 sql optimization google-bigquery

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

列是: 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/

相关文章:

c# - SQL 插入 : Duplicate Key Value Specified?

mysql - 从子查询中选择 "the best result"?

algorithm - 实现此算法的最佳方法是什么?

mysql - 如何在不存在的情况下对大型数据库执行查询

google-bigquery - bq 命令行是否支持 writeDisposition : WRITE_TRUNCATE?

google-bigquery - 使用 bq 命令行工具时引用包含连字符的项目名称时遇到问题

sql - sql查询中的神秘变量

sql - 在 Netezza 中将带分数转换为 float

c++ - 按值返回还是按引用返回?

python - BigQuery 是否应该使用 table.insert_data() 且skip_invalid_rows=True 抛出错误?