【问题标题】:How can I improve the amount of data queried with a partitioned+clustered table?如何提高使用分区+聚簇表查询的数据量?
【发布时间】:2020-01-30 04:06:13
【问题描述】:

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

【问题讨论】:

    标签: google-bigquery database-partitioning clustered-index


    【解决方案1】:

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

    这是因为 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 2020-05-06
      相关资源
      最近更新 更多