- Web Engineer
- アウトバウンド営業
- Webエンジニア(経験者)
- Other occupations (19)
- Development
- Business
【dbt × BigQuery】増分更新について挙動を詳しく確認してみる【insert_overwrite(Dynamic partitions), append】株式会社ライトコード
こんにちは!
普段データ関連のお仕事をしています。
現在所属しているチームではデータマートの作成の際にdbtを使用しており、コスト削減のために差分更新の対応中です。
差分更新処理を書いていると「あれ?どんな挙動になるんだっけ?」ということが多いので、そんな自分に向けて増分更新の詳しい挙動を確認して、備忘録がてらブログを書いていきたいと思います。
環境・準備
- BigQuery
- dbt
差分更新の種類
僕が所属しているチームでは「insert_overwrite(Dynamic partitions)」方式と「append」方式を採用しているので、今回はその2つについて挙動を見ていきたいと思います。
insert_overwrite(Dynamic partitions)
下記のモデルの挙動を確認していきます。
{{
config(
partition_by={
"field": "time_jst",
"data_type": "datetime",
"granularity": "day",
},
cluster_by=["user_id"],
materialized="incremental",
incremental_strategy="insert_overwrite",
)
}}
select user_id, datetime(timestamp, "+9") as time_jst
from {{ ref("source_table_name") }}
{% if is_incremental() %}
where create_date >= current_date("+9") - 7
{% endif %}
初回実行時は、下記のようなクエリが生成されます。
create or replace table `create_table_name`
partition by datetime_trunc(time_jst, day)
cluster by user_id
as (
select user_id, datetime(timestamp, "+9") as time_jst
from `source_table_name`
);
これで「create_table_name」というテーブルが作成されます。
そして、差分更新をするために再度実行してみると下記のようなクエリが生成されました。
create or replace table `create_table_name__dbt_tmp`
partition by datetime_trunc(time_jst, day)
cluster by user_id
as (
select user_id, datetime(timestamp, "+9") as time_jst
from `source_table_name`
where create_date >= current_date("+9") - 7
);
-- generated script to merge partitions into `create_table_name`
declare dbt_partitions_for_replacement array;
-- 1. temp table already exists, we used it to check for schema changes
-- 2. define partitions to update
set (dbt_partitions_for_replacement) = (
select as struct
-- IGNORE NULLS: this needs to be aligned to _dbt_max_partition, which ignores null
array_agg(distinct datetime_trunc(time_jst, day) IGNORE NULLS)
from `create_table_name__dbt_tmp`
);
-- 3. run the merge statement
merge into `create_table_name` as DBT_INTERNAL_DEST
using (
select
* from `create_table_name__dbt_tmp`
) as DBT_INTERNAL_SOURCE
on FALSE
when not matched by source
and datetime_trunc(DBT_INTERNAL_DEST.time_jst, day) in unnest(dbt_partitions_for_replacement)
then delete
when not matched then insert
(`user_id`, `time_jst`)
values
(`user_id`, `time_jst`)
;
-- 4. clean up the temp table
drop table if exists `create_table_name__dbt_tmp`
詳しく見ていきます。
まず1~9行目
create or replace table `create_table_name__dbt_tmp`
partition by datetime_trunc(time_jst, day)
cluster by user_id
as (
select user_id, datetime(timestamp, "+9") as time_jst
from `source_table_name`
where create_date >= current_date("+9") - 7
);
「create_table_name__dbt_tmp」という名前の一時テーブルが作成されます
また、差分更新になるので、モデルの16行目〜18行目に書かれているis_incremental()マクロ内のwhere句が適用されたクエリが生成されています。(上記8行目)
続いて11~21行目
…
記事の続きは下のURLをクリック!
https://rightcode.co.jp/blogs/51987
エンジニア積極採用中です!
現在、WEBエンジニア、モバイルエンジニア、デザイナー、営業などを積極採用中です!
採用ページはこちら:https://rightcode.co.jp/recruit
社員の声や社風などを知りたい方はこちら:https://rightcode.co.jp/blogs?category=life
社長と一杯飲みながらお話しませんか?(転職者向け)
特設ページはこちら: https://rightcode.co.jp/gohan-sake-president-talk
もっとワクワクしたいあなたへ
現在、ライトコードでは「WEBエンジニア」「モバイルエンジニア」「ゲームエンジニア」、「デザイナー」「WEBディレクター」「営業」などを積極採用中です!
ライトコードは技術力に定評のある受託開発をメインにしているIT企業です。
有名WEBサービスやアプリの受託開発などの企画、開発案件が目白押しの状況です。
- もっと大きなことに挑戦したい!
- エンジニアとしてもっと成長したい!
- モダンな技術に触れたい!
現状に満足していない方は、まずは、エンジニアとしても第一線を走り続ける弊社代表と気軽にお話してみませんか?
ネット上では、ちょっとユルそうな会社に感じると思いますが(笑)、
実は技術力に定評があり、沢山の実績を残している会社ということをお伝えしたいと思っております。
- ライトコードの魅力を知っていただきたい!
- 社風や文化なども知っていただきたい!
- 技術に対して熱意のある方に入社していただきたい!
一度、【Wantedly内の弊社ページ】や【コーポレートサイト】をのぞいてみてください。
Wantedly:https://www.wantedly.com/companies/rightcode
コーポレート:https://rightcode.co.jp/