■ はじめに
DBT での色々な更新について徐々にではあるがまとめていく
目次
【0】materialized 1)incremental_strategy 2)is_incremental() 【2】使用上の注意 1)PostgreSQLの場合
【0】materialized
cf. materialized = 具体化/具現化する
https://docs.getdbt.com/docs/build/materializations
table
* データモデルを「テーブル」として作成(洗い替え)
view
* データモデルを「ビュー」として作成
incremental
* データモデルを「テーブル」として作成(追記・更新)
ephemeral
* 共通テーブル式(CTE)として扱う cf. ephemeral (エフェメラル) = つかの間の、はかない、短命な
materialized view
* データモデルを「マテリアライズドビュー」として作成
1)incremental_strategy
https://iomete.com/docs/guides/dbt/dbt-incremental-models-by-examples
* 取りうる値は以下の通り
append
* データ追加(insert)
merge (Default)
* データ更新(update or insert)
insert_overwrite
* 上書き追記
delete+insert
* 削除後に insert
2)is_incremental()
* 以下の条件を当てはまる場合、True になる + テーブルが既に存在する場合 + 「full-refresh」モードで動いていない + materialized='incremental'
https://docs.getdbt.com/docs/build/incremental-models#understanding-the-is_incremental-macro
サンプル
{% if is_incremental() %} -- this filter will only be applied on an incremental run -- (uses >= to include records arriving later on the same day as the last run of this model) where date_day >= (select max(date_day) from {{ this }}) {% endif %}
【2】使用上の注意
1)PostgreSQLの場合
* PostgreSQL が古いバージョンだと動かない場合がある => DBTが裏でMERGE INTO を使っており、 PostgreSQLの場合、MERGE INTO は PostgreSQL v15からサポートされる => PostgreSQL v12 を使っていたので、結構ハマった、、、 => なお、PostgreSQLのバージョン確認は、「SELECT VERSION();」
参考文献
https://docs.getdbt.com/docs/build/incremental-models
https://docs.getdbt.com/docs/build/materializations
関連記事
dbt ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/06/30/000000
dbt ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/05/30/151003
dbt ~ 基本編 / Source ~
https://dk521123.hatenablog.com/entry/2023/12/08/111012
dbt ~ 更新 / Update or Insert ~
https://dk521123.hatenablog.com/entry/2023/12/19/224453
dbt ~ 更新 / Delete and Insert ~
https://dk521123.hatenablog.com/entry/2023/12/20/000104
dbt ~ 更新 / DROP + CTAS ~
https://dk521123.hatenablog.com/entry/2023/12/04/000000