Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:173TECH Sayn Incremental Loading

From Leeroopedia
Knowledge Sources
Domains Data_Engineering, ETL
Last Updated 2026-10-17 00:00 GMT

Overview

Technique for efficiently processing large datasets by only loading new or changed records since the last execution, using incremental keys for filtering and delete keys for merge deduplication.

Description

Incremental loading solves the problem of reprocessing entire datasets on each run. Instead of full refreshes, only records newer than the last processed incremental value are loaded. Two strategies are supported:

  • Merge incremental: Uses incremental_key for filtering and delete_key for deduplication. New records are merged into the existing table, replacing any with matching delete keys.
  • Append incremental: Uses incremental_key for filtering and appends records with a _sayn_load_ts timestamp. No deduplication — records accumulate.

For SQL tasks, incremental loading is simpler: materialisation: incremental with a delete_key creates a temp table from the query and merges it with the target.

Usage

Use this principle when:

  • Datasets are too large for full refreshes
  • New data arrives with a monotonically increasing key (e.g., timestamp, ID)
  • You need to track load history (append mode with _sayn_load_ts)
  • You need deduplication (merge mode with delete_key)

Theoretical Basis

The incremental loading algorithm for copy tasks:

  1. Determine last value: Query MAX(incremental_key) from the destination table
  2. Filter source: WHERE incremental_key >= last_value OR incremental_key IS NULL
  3. Load batch: Stream filtered records into a temp table
  4. Merge or append:
    • Merge: Delete records with matching delete keys, insert new records
    • Append: Insert all records with _sayn_load_ts timestamp

For SQL tasks, the merge is simpler:

  1. Create temp table from the SQL query
  2. Merge: DELETE FROM target WHERE delete_key IN (SELECT delete_key FROM temp); INSERT INTO target SELECT * FROM temp;

Batched merge: When max_merge_rows is set, the merge is processed in batches:

# Abstract algorithm (NOT real implementation)
while True:
    count = load_and_merge(batch_size=max_merge_rows)
    if count < max_merge_rows:
        break  # No more rows

Full load override: The -f/--full-load CLI flag forces a full refresh instead of incremental, replacing the table entirely.

Practical Guide

SQL incremental task:

-- sql/user_orders.sql
{{ config(materialisation='incremental', delete_key='user_id') }}
SELECT user_id, MAX(order_date) as last_order
FROM {{ src('raw.orders') }}
WHERE order_date >= {{ start_dt }}
GROUP BY user_id

Copy incremental task:

# tasks/copy.yaml
copy_orders:
  type: copy
  source:
    table: raw_orders
    db: source_db
  destination:
    table: orders
    db: warehouse
  incremental_key: order_id
  delete_key: order_id
  max_merge_rows: 100000

Append mode:

copy_events:
  type: copy
  source:
    table: raw_events
    db: source_db
  destination:
    table: events_log
    db: warehouse
  incremental_key: event_ts
  append: true

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment