분할 테이블을 만들어 놓고도 WHERE 없이 전체를 읽는 쿼리가 하나 돌면 효과가 사라집니다. 사람은 실수하게 마련이라, 실수를 막는 장치를 테이블에 거는 쪽이 낫습니다.
BigQuery에는 이를 위한 옵션이 있습니다. require_partition_filter입니다.
하는 일
옵션을 켜면 그 테이블을 읽는 쿼리는 파티션 열에 대한 조건을 반드시 달아야 합니다. 문서의 표현으로는 파티션 열만 참조하는 조건이 적어도 하나 있어야 합니다.
조건이 없으면 쿼리가 실행되지 않고 오류가 납니다.
Cannot query over table 'project_id.dataset.table' without a filter that can be used for partition elimination.
문서는 이 요구가 성능을 높이고 비용을 줄일 수 있다고 설명합니다. 조건 덕분에 필요한 파티션만 읽기 때문입니다. 이 테이블을 참조하는 뷰와 머티리얼라이즈드 뷰에도 같은 요구가 적용됩니다.
켜는 법
SQL은 한 줄입니다.
ALTER TABLE mydataset.mypartitionedtable
SET OPTIONS (
require_partition_filter = true);
bq 명령으로도 됩니다.
bq update --require_partition_filter mydataset.mytable
다른 프로젝트의 테이블이면 프로젝트:데이터셋.테이블 형태로 적습니다.
알아 둘 것
- 콘솔에서는 테이블을 만든 뒤에 이 옵션을 추가할 수 없습니다. 만들 때 정하거나, SQL·bq·API로 바꿔야 합니다.
- API로 바꿀 때는
tables.update보다 tables.patch가 권장됩니다. update는 테이블 리소스 전체를 덮어쓰기 때문입니다.
- 문서에는 켜는 예시만 있습니다. 끄는 SQL은 문서에서 확인하지 못해서 이 글에서는 다루지 않았습니다.
쓸지 말지
자주 읽는 큰 분할 테이블이라면 켜 둘 이유가 충분해 보입니다. 다만 분석가가 탐색용으로 가끔 전체를 훑어야 하는 테이블이라면 오류가 불편할 수 있습니다. 그런 곳은 이 옵션보다 쿼리 비용 상한을 거는 쪽을 먼저 보는 것이 맞을 수 있는데, 이 글에서는 그쪽은 확인하지 않았습니다.
Even if you create a partitioned table, the effect disappears if a query runs that reads the entire table without a WHERE clause. People are bound to make mistakes, so it's better to put a mechanism on the table to prevent mistakes.
BigQuery has an option for this: require_partition_filter.
What it does
When the option is enabled, queries reading that table must include a condition on the partition column. According to the documentation, there must be at least one condition that references only the partition column.
If there is no condition, the query will not run and an error will occur.
Cannot query over table 'project_id.dataset.table' without a filter that can be used for partition elimination.
The documentation explains that this requirement can improve performance and reduce costs. This is because the condition allows only the necessary partitions to be read. The same requirement applies to views and materialized views that reference this table.
How to enable it
SQL is one line.
ALTER TABLE mydataset.mypartitionedtable
SET OPTIONS (
require_partition_filter = true);
It can also be done with the bq command.
bq update --require_partition_filter mydataset.mytable
For tables in another project, write it in the form project:dataset.table.
Things to know
- In the console, you cannot add this option after creating the table. You must decide when creating it, or change it via SQL,
bq, or API.
- When changing via API,
tables.patch is recommended over tables.update. This is because update overwrites the entire table resource.
- The documentation only has examples for enabling it. The SQL to disable it was not confirmed in the documentation, so this article did not cover it.
Whether to use it
For large partitioned tables that are read frequently, there seems to be sufficient reason to enable it. However, if it's a table that analysts occasionally need to scan entirely for exploration, errors might be inconvenient. In such cases, it might be better to first look into setting a query cost cap rather than this option, though this article did not verify that side.