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

Heuristic:173TECH Sayn Batch Size Tuning

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

Overview

Tune `max_batch_rows` (default 50000) and `max_merge_rows` to balance memory usage and throughput during bulk data loading and incremental merges in copy tasks.

Description

SAYN uses two batch-size parameters to control data loading behaviour. `max_batch_rows` (default: 50000) controls how many rows are loaded per batch in `load_data()` and copy tasks — the default INSERT...VALUES method sends one batch at a time to avoid exceeding query size limits or database memory. `max_merge_rows` is an optional parameter for incremental copy tasks that limits the number of rows merged per execute cycle; when set, the copy task runs in a loop, fetching `max_merge_rows` rows at a time, until fewer rows than the limit are returned (up to 100 iterations).

Native bulk loaders (PostgreSQL COPY, Redshift S3+COPY, BigQuery load jobs, Snowflake PUT) override `_load_data_batch()` and are much more efficient than the default INSERT. However, even with native loaders, extremely large batches can cause memory pressure on the client side (data is buffered in memory as JSON/CSV before sending).

Usage

Use this heuristic when you encounter "amount of data is too large" errors during copy or load operations, or when tuning throughput for large datasets. Reduce `max_batch_rows` if you see memory errors or query timeout issues. Use `max_merge_rows` to prevent long-running merge transactions from locking tables or consuming excessive temp space.

The Insight (Rule of Thumb)

  • Action: Set `max_batch_rows` in `settings.yaml` credentials (applies to all tasks using that connection) or per-copy-task in the task config.
  • Value: Default is `50000`. Reduce to `200`–`10000` for wide tables or databases with limited memory. Increase for narrow tables with native bulk loaders.
  • Action: Set `max_merge_rows` on incremental copy tasks to bound merge transaction size.
  • Value: Optional integer. When set, the copy task loops with `LIMIT max_merge_rows` until fewer rows are returned, capped at 100 iterations.
  • Trade-off: Smaller batches reduce memory/timeout risk but increase round trips and total load time. `max_merge_rows` prevents long transactions but may require multiple passes.
  • Compatibility: `max_batch_rows` applies to all database types. `max_merge_rows` only applies to incremental copy tasks (requires `incremental_key`).

Reasoning

The default `max_batch_rows` of 50000 is set in the Database base class constructor (`database/__init__.py:129`):

self.max_batch_rows = common_params.get("max_batch_rows", 50000)

The batch loop in `load_data()` (`database/__init__.py:576-585`) accumulates records in a buffer and flushes when the batch boundary is hit:

for i, record in enumerate(data):
    ...
    if i % batch_size == 0 and len(buffer) > 0:
        self._load_data_batch(table, buffer, schema, db)
        records_loaded += len(buffer)
        buffer = list()
    buffer.append(record)

The `max_merge_rows` loop in `CopyTask.run()` (`tasks/copy.py:350-362`) fetches data in chunks:

if self.max_merge_rows is not None:
    result = self.execute(True, self.run_arguments["debug"],
                          self.is_full_load, self.max_merge_rows)
    if result.is_err:
        return result
    for _ in range(100):
        if result.is_err or result.value < self.max_merge_rows:
            break
        result = self.execute(True, False, False, self.max_merge_rows)

The documentation in `docs/databases/overview.md:51-53` explicitly states:

All databases support a parameter `max_batch_rows` that controls the default size of a batch
when using `load_data` or in copy tasks. If you get an error when running SAYN indicating the
amount of data is too large, adjust this value.

Related Pages

Page Connections

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