Principle:173TECH Sayn Incremental Loading
| 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_keyfor filtering anddelete_keyfor deduplication. New records are merged into the existing table, replacing any with matching delete keys. - Append incremental: Uses
incremental_keyfor filtering and appends records with a_sayn_load_tstimestamp. 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:
- Determine last value: Query
MAX(incremental_key)from the destination table - Filter source:
WHERE incremental_key >= last_value OR incremental_key IS NULL - Load batch: Stream filtered records into a temp table
- Merge or append:
- Merge: Delete records with matching delete keys, insert new records
- Append: Insert all records with
_sayn_load_tstimestamp
For SQL tasks, the merge is simpler:
- Create temp table from the SQL query
- 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