# Incremental Models ## Trade-Offs 1. အကယ်ရွေ့ data တွေက နောက်ကျပြီးမှ ဝင်လာတဲ့အခါ ဘယ်လို လုပ်မလဲ။ ## Strategies - ပထမဆုံး ကိုယ်က ဘယ် database ကို အသုံးပြုမလဲ ဆိုတာ စဥ်းစားရမယ်။ ဘာကြောင့်လဲ ဆိုတော့ database ပေါ် မူတည်ပြီး support လုပ်တဲ့ strategies တွေက ကွဲသွားလို့။ - dbt [Official Documentation](https://docs.getdbt.com/docs/build/incremental-strategy#supported-incremental-strategies-by-adapter) ထဲမှာ ပြထားတဲ့ Table ကို reference ပြုလုပ်ပါ။ | Data platform adapter | append | merge | delete+insert | insert_overwrite | microbatch | | ------------------------------------------------------------------------ | ------ | ----- | ------------- | ---------------- | ---------- | | dbt-postgres | ✅ | ✅ | ✅ | | ✅ | | dbt-redshift | ✅ | ✅ | ✅ | | ✅ | | dbt-bigquery | | ✅ (D) | | ✅ | ✅ | | dbt-spark | ✅ | ✅ | | ✅ | ✅ | | dbt-databricks | ✅ | ✅ | | ✅ | ✅ | | dbt-snowflake | ✅ | ✅ | ✅ | ✅ | ✅ | | dbt-trino | ✅ | ✅ | ✅ | | ✅ | | dbt-fabric | ✅ | | ✅ | | | | dbt-athena | ✅ | ✅ | | ✅ | ✅ | | dbt-teradata | ✅ | ✅ | ✅ | | ✅ | | [dbt-duckdb](https://github.com/duckdb/dbt-duckdb/blob/master/README.md) | ✅ | | ✅ (Default) | | | Visualizations credits to [Joon Solutions](https://joonsolutions.com/dbt-incremental-choosing-the-right-strategy-part-1/) #### Append - Target table ထဲကို new rows များကိုသာ ပေါင်းထည့်သည်။ - duplicate များကို မစစ်ဆေး။ - updates (မည်သည့် row များတွင် မည်သို့ ပြုပြင်ပြောင်းလဲမှု changes များ လုပ်လိုက်သည် ကို) မစစ်ဆေး။ - အသုံးပြုနိုင်တဲ့ ဒေတာများ - ပြုပြင်ပြောင်းလဲရန် မလွယ်ကူသော event streams/log data များ - `INSERT INTO` sql ကို အသုံးပြုသည်။ #### Merge (Update + Insert = Upsert) - Target table ထဲကို Primary Key (သတ်မှတ်လိုက်တဲ့ `unique_key`) ကို သုံးပြီး ရှိပြီးသား records များကို Updates ပြုလုပ်သည်။ - မရှိသေးသော ဒေတာ အသစ် new records များကို Insert ပြုလုပ်သည်။ - Table တခုလုံးကို scan လုပ်ရသည့်အတွက် data များလာသည်နှင့်အမျှ performance issues များ ရှိနိုင်တယ်။ - Partitioning ပြုလုပ်ပြီး performance ထိန်းနိုင်တယ်။ - Clustering ပါ အသုံးပြုပြီး performance ထိန်းဖို့ နဲ့ cost လျှော့ချဖို့ လုပ်နိုင်တယ်။ - ရှိပြီးသား ဒေတာများအပေါ် overwrite ပြုလုပ်တဲ့အတွက် duplicate issue များ မရှိနိုင်ပါ။ - `MERGE INTO` sql ကို Primary Key (Unique Key) နှင့် တွဲ၍ အသုံးပြုတယ်။ #### Delete + Insert - Target table ထဲကို Primary Key (သတ်မှတ်လိုက်တဲ့ `unique_key`) ကို သုံးပြီး ရှိပြီးသား records များကို **DELETE** ပြုလုပ်တယ်။ - မရှိသေးသော ဒေတာ အသစ် new records များ နှင့် ဖျက်လိုက်သော records များအတွက် အသစ်ဝင်လာတဲ့ new version records များကို Primary Key (`unique_key`) အပေါ် အခြေခံပြီး Insert ပြုလုပ်တယ်။ - Table တခုလုံးကို scan လုပ်ရသည့်အတွက် data များလာသည်နှင့်အမျှ performance issues များ ရှိနိုင်သည်။ (Partitioning ပြုလုပ်ပြီး performance ထိန်းနိုင်တယ်။) - ရှိပြီးသား ဒေတာများအပေါ် DELETE ပြုလုပ်တဲ့အတွက် အန္တရာယ်ရှိနိုင်သည်။ - `DELETE FROM` + `INSERT INTO` sql ကို Primary Key (Unique Key) နှင့် တွဲ၍ အသုံးပြုတယ်။ #### Insert - Overwrite - Target table ထဲရှိ partition အလိုက် replace ပြုလုပ်သည့် ပုံစံ ဖြစ်တယ်။ - သတ်မှတ်လိုက်တဲ့ partition အကန့်ကိုပဲ ဖတ်မှာ ဖြစ်တဲ့အတွက် full table scan မလိုအပ်ဘူး။ - BigQuery အတွက် ဆိုရင် [[#Merge (Update + Insert = Upsert)|Merge strategy]] ထက် ပိုပြီး စွမ်းဆောင်ရည် ကောင်းမွန်တယ်။ - SQL ပုံစံကတော့ BigQuery နဲ့ အခြား DB System တွေအကြား ကွာခြားနိုင်တယ်။ - BigQuery အတွက်ဆိုရင် temp table ဆောက်တယ်။ Replace လုပ်မယ့် partitions ကို သတ်မှတ်ပြီးတော့ သတ်မှတ်တဲ့ အကန့်အတွက်ကို `MERGE INTO` လုပ်တယ်။ - အခြား DB System တွေမှာတော့ `DELETE FROM` + `INSERT INTO` ကို တွဲပြီး လုပ်ဆောင်တယ်။ - Partition ခွဲရာမှာ timestamp နဲ့ လုပ်ဆောင်ဖို့ အသင့်တော်ဆုံး ဖြစ်တဲ့အတွက် အကယ်ရွေ့ ဒေတာထဲမှာ timestamp က ကပေါက်တိ ကပေါက်ချာ ဖြစ်နေတယ် ဆိုရင်တော့ ဒီ strategy ကို သုံးဖို့ မသင့်ဘူး။ #### Microbatch