首页 > 解决方案 > 如何提高使用分区+聚簇表查询的数据量?

问题描述

我有一个 BigQuery 表 - 天分区和集群。但是,当我对它运行查询时,它仍然使用大量数据。这怎么可能?

标签: google-bigquerydatabase-partitioningclustered-index

解决方案


有时没有分区,或者每周/每月/每年的分区会比每天分区表+集群更好。

这是因为 BigQuery 中的每个数据集群都有一个最小大小。如果每日分区表中的每一天数据的数据量都少于该数据量,那么集群表根本不会带来任何好处。

例如,让我们创建一个包含 30 多年天气的表。我将按月对该表进行分区(以将多年放入一个表中):

CREATE TABLE `temp.gsod_partitioned`
PARTITION BY date_month
CLUSTER BY name
AS 
SELECT *, DATE_TRUNC(date, MONTH) date_month
FROM `fh-bigquery.weather_gsod.all` 

现在,让我们对其运行查询 - 使用聚类字段name

SELECT name, state, ARRAY_AGG(STRUCT(date,temp) ORDER BY temp DESC LIMIT 5) top_hot, MAX(date) active_until
FROM `temp.gsod_partitioned`
WHERE name LIKE 'SAN FRANC%'
AND date > '1980-01-01'
GROUP BY 1,2
ORDER BY active_until DESC 
# (2.3 sec elapsed, 3.1 GB processed)

现在,让我们在一个相同的表上执行此操作 - 由假日期分区(因此实际上没有分区),并由同一列聚集:

SELECT name, state, ARRAY_AGG(STRUCT(date,temp) ORDER BY temp DESC LIMIT 5) top_hot, MAX(date) active_until
FROM `fh-bigquery.weather_gsod.all` 
WHERE name LIKE 'SAN FRANC%'
AND date > '1980-01-01'
GROUP BY 1,2
ORDER BY active_until DESC
# (1.5 sec elapsed, 62.8 MB processed)

仅处理了 62.8 MB 的数据(与 3.1GB 相比)!

这是因为没有分区的集群在每天没有大量 GB 的表上效率更高。

奖励:按地理位置聚集:

SELECT name, state, ARRAY_AGG(STRUCT(date,temp) ORDER BY temp DESC LIMIT 5) top_hot, MAX(date) active_until  
FROM `fh-bigquery.weather_gsod.all_geoclustered`  
WHERE date > '1980-01-01'
AND ST_DISTANCE(point_gis, ST_GEOGPOINT(-122.465, 37.807)) < 40000
GROUP BY 1,2
ORDER BY ST_DISTANCE(ANY_VALUE(point_gis), ST_GEOGPOINT(-122.465, 37.807))
# (2.1 sec elapsed, 100.7 MB processed)

推荐阅读