How to choose the latest partition in BigQuery table?

后端 未结 7 969
暗喜
暗喜 2020-12-09 21:21

I am trying to select data from the latest partition in a date-partitioned BigQuery table, but the query still reads data from the whole table.

I\'ve tried (as far a

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 22:03

    I found the workaround to this issue. You can use with statement, select last few partitions and filter out the result. This is I think better approach because:

    1. You are not limited by fixed partition date (like today - 1 day). It will always take the latest partition from given range.
    2. It will only scan last few partitions and not whole table.

    Example with last 3 partitions scan:

    WITH last_three_partitions as (select *, _PARTITIONTIME as PARTITIONTIME 
        FROM dataset.partitioned_table 
        WHERE  _PARTITIONTIME > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 3 DAY))
    SELECT col1, PARTITIONTIME from last_three_partitions 
    WHERE PARTITIONTIME = (SELECT max(PARTITIONTIME) from last_three_partitions)
    

提交回复
热议问题