Community研究&データ分析github.com

dbtモデル構築スキル

staging/intermediate/martsの各レイヤー、ref()とsource()、マテリアライゼーション、適切な戦略を用いたインクリメンタルモデルなど、構造化されたdbtモデルを構築します。dbtモデルの作成やリファクタリング、テーブル・ビュー・インクリメンタルの選択、dbtプロジェクトの構造化、インクリメンタルロジックの記述時に使用します。

dbtモデル構築スキル とは?

このスキルは、dbtプロジェクトにおいて適切に構造化されたモデルを構築するためのベストプラクティスを提供します。staging、intermediate、martsの3層構造に従い、各モデルを適切なレイヤーに配置する方法、`ref()`や`source()`を用いて依存関係を正しく管理する方法、データのサイズや更新頻度に応じたマテリアライゼーション(view、table、incremental、ephemeral)の選択基準を説明します。特にインクリメンタルモデルについては、`unique_key`の設定と`is_incremental()`フィルタによる差分処理、および遅延データを考慮したルックバック期間の設定といった重要なパターンを詳細に解説します。また、ハードコードされたテーブル名の使用、unique_keyの欠如、ルックバックなしのインクリメンタルフィルタなど、よくある落とし穴とその回避方法を示し、テスト可能で再利用可能なモデル構造を実現します。このスキルはClaude CodeやCursor、CodexなどのAIエージェントツール上で動作し、dbtプロジェクトの品質と保守性を向上させるためのガイドとして機能します。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Unknown-333/awesome-data-engineering-skills/tree/main/skills/building-dbt-models

Installed? Explore more 研究&データ分析 skills: affaan-m/uspto-database, affaan-m/scholar-evaluation, affaan-m/literature-review · View all 6 →

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

dbtモデルの構築

使用すべき状況

  • dbtプロジェクトで.sqlモデルを作成またはリファクタリングする場合。
  • マテリアライゼーション(view / table / incremental / ephemeral)を決定する場合。
  • レイヤー(staging → intermediate → marts)を構造化する場合。
  • 大規模で増大するテーブルに対してインクリメンタルモデルを記述する場合。
  • テストの作成には使用しないでください(testing-dbt-projectsを使用)。また、実行失敗のデバッグには使用しないでください(debugging-dbt-runsを使用)。

ワークフロー

- [ ] モデルを適切なレイヤー(staging/intermediate/marts)に配置する
- [ ] 上流の参照はref()/source()のみを使用 — ハードコードされた名前は使用しない
- [ ] サイズと更新要件に基づいてマテリアライゼーションを選択する
- [ ] インクリメンタルモデルの場合、unique_key + is_incremental()フィルタを設定する
- [ ] テストを含むschema.ymlエントリを追加する
  1. レイヤリング。 staging/ = ソーステーブルごとに1モデル、軽いリネーム/型変換、ビューとしてマテリアライズ。intermediate/ = 再利用可能なビジネスロジック。marts/ = BIで利用される最終的なディメンショナルモデル、テーブルとしてマテリアライズ。
  2. 正しく参照する。 {{ ref('stg_orders') }} と {{ source('shop', 'orders') }} を使用して、dbtがDAGを構築し環境を管理できるようにします。生のスキーマ.テーブル名は決して記述しないでください。
  3. マテリアライゼーションを選択: view(低コスト、常に最新、小規模)、table(高速読み取り、実行ごとに再構築)、incremental(大規模な追加/更新テーブル)、ephemeral(インラインCTE、オブジェクトなし)。
  4. インクリメンタルモデル は新しい行または変更された行のみを処理します。

パターン

Stagingモデル — ソースごとに1つ、薄く一貫性を保つ:

-- models/staging/shop/stg_orders.sql
with source as (select * from {{ source('shop', 'orders') }})
select
    order_id,
    customer_id,
    cast(order_ts as timestamp) as ordered_at,
    round(amount_cents / 100.0, 2) as amount
from source

インクリメンタルモデル — 新しい行にフィルタリングし、冪等なマージキーを設定:

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

select * from {{ ref('stg_orders') }}
{% if is_incremental() %}
  -- すでにロード済みのものより新しい行のみ、遅延データを考慮してルックバック
  where ordered_at >= (select coalesce(max(ordered_at), '1900-01-01') from {{ this }})
                      - interval '3 days'
{% endif %}

unique_key + merge により再実行が冪等になります。ルックバックは遅延到着行をキャッチします。BigQuery/Sparkでは、日付パーティションに対して insert_overwrite を推奨します。

よくある落とし穴

  • ハードコードされたテーブル名 を ref()/source() の代わりに使用 — DAG、系統、環境切り替えが壊れます。
  • unique_keyなしのインクリメンタル — 再実行で重複が追記されます。
  • ルックバックなしのmax(id)インクリメンタルフィルタ — 遅延データが静かに欠落します。
  • stagingでのビジネスロジック — stagingは薄く保ちます。結合/集計はintermediate/martsに属します。
  • すべてをtableとしてマテリアライズ — ウェアハウス時間を浪費します。小規模/低コストモデルにはビューを、大規模モデルにはインクリメンタルを使用します。
  • 一つの巨大なモデル — テスト容易性と再利用性のために中間ステップに分割します。

参考資料

Individual skills in this repo

This repo contains 9 individual skills — each has its own dedicated page.

Unknown-333/authoring-airflow-dags

Write production-grade Apache Airflow DAGs using the TaskFlow API — idempotent tasks, correct scheduling and catchup, retries/SLAs, connections/variables, and avoiding top-level code. Use when creating or reviewing Airflow DAGs, scheduling pipelines, wiring task dependencies, configuring retries/backfills, or fixing non-idempotent tasks.

Unknown-333/building-dagster-assets

Build Dagster pipelines using software-defined assets — asset dependencies, partitions, resources and IO managers, asset checks, and schedules/sensors. Use when creating Dagster assets or jobs, modeling data as assets, adding partitions or backfills, wiring resources/IO managers, or migrating from task-based orchestration to assets.

Unknown-333/building-feature-pipelines

Build ML feature pipelines and feature stores — point-in-time-correct joins to avoid label leakage, offline/online parity, feature freshness and backfills, and materialization with tools like Feast. Use when engineering features for ML, preventing train/serve skew or data leakage, building a feature store, or backfilling historical features for training.

Unknown-333/building-iceberg-tables

Design and operate Apache Iceberg tables — partitioning and hidden partitioning, partition/schema evolution, snapshots and time travel, compaction and small-file cleanup, and MERGE/upsert for lakehouse tables on Spark, Flink, Trino, or Snowflake. Use when creating or maintaining Iceberg tables, choosing partitioning, evolving schema/partitions, or fixing small-file and metadata bloat.

Unknown-333/building-ingestion-pipelines

Build batch and incremental data ingestion (extract-load) pipelines — full vs incremental extraction, change data capture (CDC), watermarks and high-water marks, API pagination and rate limits, and choosing managed EL tools (Fivetran, Airbyte) vs custom code. Use when ingesting data from databases, APIs, files, or SaaS into a warehouse/lake, or designing incremental extraction and CDC.

Unknown-333/building-kafka-consumers

Build reliable Apache Kafka consumers and producers — consumer groups and partition assignment, offset commit strategy, at-least-once vs exactly-once, idempotent/transactional producers, rebalancing, and dead-letter handling. Use when writing Kafka consumers/producers, configuring offset commits or consumer groups, tuning throughput, or handling rebalances and poison messages.

Unknown-333/debugging-data-pipelines

Systematically root-cause data pipeline failures and data incidents — job errors, wrong or missing data, duplicates, and freshness misses — by tracing lineage upstream, isolating the failing stage, reconciling against source, and planning a safe fix and backfill. Use when a pipeline fails, numbers look wrong, data is missing or duplicated, a dashboard is stale, or a stakeholder reports a data discrepancy.

Unknown-333/designing-backfills-and-replays

Plan and run safe data backfills and replays — idempotent reprocessing of historical windows, partition-by-partition execution, isolating backfill compute from production, verifying results, and avoiding double-counting or changed history. Use when backfilling a new or fixed model, reprocessing after a bug, replaying events, or loading history for a new pipeline without corrupting existing data.

Unknown-333/designing-data-contracts

Define and enforce data contracts between producers and consumers — explicit schema, semantics, ownership, SLAs, and versioning — to prevent silent upstream changes from breaking downstream pipelines. Use when a producer schema change could break consumers, defining an interface between teams/services and the warehouse, or adding schema enforcement at ingestion.

関連スキル