Data Engineering
dbt Core Best Practices: Modular SQL Transformation Pipelines for Production Data Warehouses
technology.efektifpro.com – dbt (data build tool) has become the lingua franca of the modern data stack, deployed at over 30,000 organizations for SQL-based data transformation. Its genius is simple: bring software engineering best practices — version control, testing, documentation, modularity — to SQL transformation pipelines. Organizations adopting dbt report 60–80% reductions in data quality incidents and 40% faster time-to-insight.
The Medallion Architecture: Staging to Intermediate to Mart
# dbt project structure — layered medallion architecture
models/
- staging/ # Layer 1: Raw source cleaning (1:1 with source tables)
- payments/
- stg_payments__transactions.sql
- stg_payments__accounts.sql
- _stg_payments__models.yml
- intermediate/ # Layer 2: Business logic, joins, calculations
- int_transactions_enriched.sql
- marts/ # Layer 3: Business-facing dimensional/fact models
- finance/
- fct_daily_revenue.sql
- dim_accounts.sql
Incremental Models: Only Processing New Data
-- models/staging/payments/stg_payments__transactions.sql
{{ config(
materialized='incremental',
unique_key='transaction_id',
incremental_strategy='merge'
) }}
SELECT
transaction_id,
account_id,
amount::DECIMAL(18,4) AS amount,
status,
updated_at::TIMESTAMP AS updated_at
FROM {{ source('payments_raw', 'transactions') }}
{% if is_incremental() %}
WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }})
{% endif %}
dbt Testing: The Four Essential Test Types
# models/marts/finance/_finance__models.yml
version: 2
models:
- name: fct_daily_revenue
columns:
- name: revenue_key
tests: [unique, not_null]
- name: total_revenue_usd
tests:
- not_null
- dbt_utils.expression_is_true:
expression: ">= 0"
- name: account_id
tests:
- relationships:
to: ref('dim_accounts')
field: account_id
dbt Best Practices Checklist
- ✅ Staging models are 1:1 with sources — no business logic in staging layer
- ✅ Every model has unique and not_null tests on primary keys
- ✅ Incremental models on all large fact tables (>10M rows)
- ✅ ref() and source() functions exclusively — no hardcoded table names
- ✅ Model ownership documented in YAML (meta.owner field)
- ✅ dbt docs generated and published after every production run
- ✅ dbt test runs in CI before any PR merge to main
- ✅ Production runs orchestrated by Airflow or dbt Cloud with Slack alerting
