developing-incremental-models

用于开发与调试 dbt 增量模型。适用于创建新的增量模型(选择策略、unique_key、分区)、处理任务中提到“增量/追加/合并/更新插入/迟到的数据”的场景、排查增量失败(合并错误、分区裁剪、模式漂移)以及优化增量性能或决定使用表还是增量模型。指导策略选择并处理常见增量陷阱。

仓库最近推送
GitHub Stars
137
Forks
13
Issues / PRs
5

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "developing-incremental-models":https://skillpi.com/zh/skills/deepskill-67932e4a5ef2b1c3ae4df52cc14c56534f237bdbd764ffd50eeaf2b4bd530bbb。安装前核对上游来源和版本:https://github.com/AltimateAI/data-engineering-skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

用于开发与调试 dbt 增量模型。适用于创建新的增量模型(选择策略、unique_key、分区)、处理任务中提到“增量/追加/合并/更新插入/迟到的数据”的场景、排查增量失败(合并错误、分区裁剪、模式漂移)以及优化增量性能或决定使用表还是增量模型。指导策略选择并处理常见增量陷阱。

0 下载

介绍来源: third_party · AltimateAI · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
适合谁
翻译待完成,请参阅来源文档。
输入和输出
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: developing-incremental-models
description: |
  Develops and troubleshoots dbt incremental models. Use when working with incremental materialization for:
  (1) Creating new incremental models (choosing strategy, unique_key, partition)
  (2) Task mentions "incremental", "append", "merge", "upsert", or "late arriving data"
  (3) Troubleshooting incremental failures (merge errors, partition pruning, schema drift)
  (4) Optimizing incremental performance or deciding table vs incremental
  Guides through strategy selection, handles common incremental gotchas.

dbt Incremental Model Development

Choose the right strategy. Design the unique_key carefully. Handle edge cases.

When to Use Incremental

Scenario Recommendation
Source data < 10M rows Use table (simpler, full refresh is fast)
Source data > 10M rows Consider incremental
Source data updated in place Use incremental with merge strategy
Append-only source (logs, events) Use incremental with append strategy
Partitioned warehouse data Use insert_overwrite if supported

Default to table unless you have a clear performance reason for incremental.

Critical Rules

  1. ALWAYS test with --full-refresh first before relying on incremental logic
  2. ALWAYS verify unique_key is truly unique in both source and target
  3. If merge fails 3+ times, check unique_key for duplicates
  4. Run full refresh periodically to prevent data drift

Workflow

1. Confirm Incremental is Needed

# Check source table size
dbt show --inline "select count(*) from {{ source('schema', 'table') }}"

If count < 10 million, consider using table instead. Incremental adds complexity.

2. Understand the Source Data Pattern

Before choosing a strategy, answer:

  • Is data append-only? (new rows added, never updated)
  • Are existing rows updated? (need merge/upsert)
  • Is there a reliable timestamp? (for filtering new data)
  • What's the unique identifier? (for merge matching)
# Check for timestamp column
dbt show --inline "
  select
    min(updated_at) as earliest,
    max(updated_at) as latest,
    count(distinct date(updated_at)) as days_of_data
  from {{ source('schema', 'table') }}
"

3. Choose the Right Strategy

Strategy Use When How It Works
append Data is append-only, no updates INSERT only, no deduplication
merge Data can be updated MERGE/UPSERT by unique_key
delete+insert Data updated in batches DELETE matching rows, then INSERT
insert_overwrite Partitioned tables (BigQuery, Spark) Replace entire partitions

Default: merge is safest for most use cases.

Note: Strategy availability varies by adapter. Check the dbt incremental strategy docs for your specific warehouse.

4. Design the Unique Key

CRITICAL: unique_key must be truly unique in your data.

# Verify uniqueness BEFORE creating model
dbt show --inline "
  select {{ unique_key_column }}, count(*)
  from {{ source('schema', 'table') }}
  group by 1
  having count(*) > 1
  limit 10
"

If duplicates exist:

  • Add more columns to make composite key
  • Add deduplication logic in model
  • Use delete+insert instead of merge

5. Write the Incremental Model

{{
    config(
        materialized='incremental',
        incremental_strategy='merge',  -- or append, delete+insert
        unique_key='id',               -- MUST be unique
        on_schema_change='append_new_columns'  -- handle new columns
    )
}}

select
    id,
    column_a,
    column_b,
    updated_at
from {{ source('schema', 'table') }}

{% if is_incremental() %}
where updated_at > (select max(updated_at) from {{ this }})
{% endif %}

6. Build with Full Refresh First

ALWAYS verify with full refresh before trusting incremental logic.

# First run: full refresh to establish baseline
dbt build --select <model_name> --full-refresh

# Verify output
dbt show --select <model_name> --limit 10
dbt show --inline "select count(*) from {{ ref('model_name') }}"

7. Test Incremental Logic

# Run incrementally (no --full-refresh)
dbt build --select <model_name>

# Verify row count changed appropriately
dbt show --inline "select count(*) from {{ ref('model_name') }}"

8. Handle Schema Changes

Set on_schema_change based on your needs:

Setting Behavior
ignore (default) New columns in source are ignored
append_new_columns New columns added to target
sync_all_columns Target schema matches source exactly
fail Error if schema changes

Common Incremental Problems

Problem: Merge Fails with Duplicate Key

Symptom: "Cannot MERGE with duplicate values"

Cause: Multiple rows with same unique_key in source or target.

Fix:

-- Add deduplication using a CTE (cross-database compatible)
with deduplicated as (
    select *,
        row_number() over (partition by id order by updated_at desc) as rn
    from {{ source('schema', 'table') }}
    {% if is_incremental() %}
    where updated_at > (select max(updated_at) from {{ this }})
    {% endif %}
)
select * from deduplicated where rn = 1

Problem: No Partition Pruning (Full Table Scan)

Symptom: Incremental runs take as long as full refresh.

Cause: Dynamic date filter prevents partition pruning.

Fix:

{% if is_incremental() %}
-- Use static date instead of subquery for partition pruning
where updated_at >= {{ dbt.dateadd('day', -3, dbt.current_timestamp()) }}
  and updated_at > (select max(updated_at) from {{ this }})
{% endif %}

Problem: Late-Arriving Data is Missed

Symptom: Some records never appear in incremental model.

Cause: Filtering by max(updated_at) misses late arrivals.

Fix: Use a lookback window with a fixed offset from current date:

{% if is_incremental() %}
-- Lookback 3 days to catch late-arriving data
where updated_at >= {{ dbt.dateadd('day', -3, dbt.current_timestamp()) }}
{% endif %}

Alternatively, use a variable for the lookback period:

{% set lookback_days = 3 %}

{% if is_incremental() %}
where updated_at >= {{ dbt.dateadd('day', -lookback_days, dbt.current_timestamp()) }}
{% endif %}

Problem: Schema Drift Causes Errors

Symptom: "Column X not found" after source adds column.

Fix: Set on_schema_change='append_new_columns' in config.

Problem: Data Drift Over Time

Symptom: Counts diverge between incremental and full refresh.

Fix: Schedule periodic full refresh:

# Weekly full refresh
dbt build --select <model_name> --full-refresh

Incremental Strategy Reference

Append (Simplest)

{{ config(materialized='incremental', incremental_strategy='append') }}

select * from {{ source('events', 'raw') }}
{% if is_incremental() %}
where event_timestamp > (select max(event_timestamp) from {{ this }})
{% endif %}
  • No unique_key needed
  • Fastest performance
  • Only use for append-only data (logs, events, immutable records)

Merge (Default)

{{ config(
    materialized='incremental',
    incremental_strategy='merge',
    unique_key='id'
) }}

select * from {{ source('crm', 'contacts') }}
{% if is_incremental() %}
where updated_at > (select max(updated_at) from {{ this }})
{% endif %}
  • Requires unique_key
  • Handles updates and inserts
  • Most common strategy

Delete+Insert (Batch Updates)

{{ config(
    materialized='incremental',
    incremental_strategy='delete+insert',
    unique_key='id'
) }}

select * from {{ source('orders', 'raw') }}
{% if is_incremental() %}
where order_date >= {{ dbt.dateadd('day', -7, dbt.current_timestamp()) }}
{% endif %}
  • Deletes all matching rows first
  • Good for reprocessing batches
  • Use when merge has duplicate key issues

Insert Overwrite (Partitioned)

{{ config(
    materialized='incremental',
    incremental_strategy='insert_overwrite',
    partition_by={'field': 'event_date', 'data_type': 'date'}
) }}

select * from {{ source('events', 'raw') }}
{% if is_incremental() %}
where event_date >= {{ dbt.dateadd('day', -3, dbt.current_timestamp()) }}
{% endif %}
  • Replaces entire partitions
  • Best for partitioned tables in BigQuery/Spark
  • No unique_key needed (operates on partitions)

Anti-Patterns

  • Using incremental for small tables (< 10M rows)
  • Not testing with full-refresh first
  • Using append strategy when data can be updated
  • Not verifying unique_key uniqueness
  • Relying on exact timestamp match without lookback
  • Never running full refresh (causes data drift)
  • Using merge with non-unique keys

Testing Checklist

  • [ ] Model runs with --full-refresh
  • [ ] Model runs incrementally (without flag)
  • [ ] unique_key verified as truly unique
  • [ ] Row counts reasonable after incremental run
  • [ ] Late-arriving data handled (lookback window)
  • [ ] Schema changes handled (on_schema_change set)
  • [ ] Periodic full refresh scheduled

当前收录版本

0.0.1

核对来源版本 ↗

安全与评测信息

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值85/100

亮点

  • 提供完整的增量模型开发闭环:从策略选择、unique_key设计到常见陷阱处理
  • 覆盖append/merge/delete+insert/insert_overwrite四大策略及适用场景
  • 包含具体SQL示例、CLI验证命令和测试清单,实用性强
触发精准90/100

亮点

  • 触发场景描述清晰完整,覆盖增量模型四大核心用例
  • 关键词精准(incremental/append/merge/upsert/late arriving data)
  • 边界清晰,不易误触或漏触
结构规范95/100

待提升项

  • description写错位置 原报告严重程度:轻微

    description写在frontmatter内而非顶层独立字段

    改进建议:建议将description移至顶层JSON字段,frontmatter内保留name即可

亮点

  • Phase划分清晰:确认需求→理解数据→选择策略→设计unique_key→编写模型→测试验证
  • 每步含具体操作、SQL示例、CLI命令,执行路径明确
  • 提供完整端到端示例和各策略代码模板
执行可靠90/100

待提升项

  • 部分策略兼容性说明不足 原报告严重程度:轻微

    insert_overwrite策略仅限BigQuery/Spark,但未显式说明

    改进建议:建议在insert_overwrite示例旁添加注释说明适配器限制

亮点

  • 覆盖主要异常场景:重复键、分区裁剪、迟到数据、模式漂移、数据漂移
  • 提供测试清单和验证命令,确保执行可靠性
  • 不可逆操作(如delete+insert)有明确适用场景说明
证据可验 (前四项均值)90/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

文档引用的域名或地址

这些地址可能用于文档链接或示例,不是已批准的联网清单。使用前核对实际目的地及发送的数据。

  • docs.getdbt.com

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。