Sitemap
Abderrahmane Oubagha

Abderrahmane Oubagha

Data Engineer / Data Scientist

Press enter or click to view image in full size

Dataform Incremental Load Optimizations

Four practical techniques that dramatically reduced BigQuery costs and execution time for production Dataform incremental pipelines.

6 min readJun 28, 2026

--

Introduction

Dataform makes building incremental pipelines remarkably simple. With just a few lines of configuration, you can process only new or updated data instead of reloading entire tables. However, writing an incremental pipeline doesn’t necessarily mean it will be efficient.

While optimizing several production pipelines, I discovered that some of our incremental jobs were scanning terabytes of data even though only a tiny fraction of the records had changed. The pipelines were technically incremental, yet a few implementation details were preventing BigQuery from taking advantage of partition pruning and other query optimizations.

In this article, I’ll share four practical techniques that significantly reduced both execution time and BigQuery costs across our Dataform pipelines. These optimizations don’t change your business logic — they simply help BigQuery scan far less data.

1. Always Filter on the Partition Column

Dataform makes it easy to build incremental pipelines. To enable incremental loading, simply set the table type to incremental in the config block:

config {
type: "incremental",
schema: "bq_dataset",
name: "bq_table"
………
}

Once that’s done, you can define different logic for full refreshes and incremental runs using the when(incremental()) helper .
A common approach is to use a watermark column, such as updated_at, to process only newly added or modified records:

 SELECT user_id, country
FROM ${ref("source_table")}
${
when(incremental(),
`WHERE updated_at > (SELECT MAX(updated_at) FROM ${self()})`
)
}

With this WHERE filter, only new data will be processed. However, there is one important edge case: what if the table is partitioned by date (updated_date) rather than by the updated_at timestamp column?

In that case, the BigQuery job will scan the entire table to find the relevant rows, because it doesn’t know which partitions to skip — the WHERE clause operates on a non-partition column, so partition pruning cannot take effect.

The solution is to combine both filters — one on the partition column to prune partitions, and one on the precise timestamp for accuracy:

jaSELECT user_id, country
FROM ${ref("source_table")}
${
when(incremental(),
`WHERE updated_date >= (SELECT MAX(updated_date) FROM ${self()})
AND updated_at > (SELECT MAX(updated_at) FROM ${self()})`
)
}

With this combined filter, only the required partitions will be scanned and BigQuery will skip all irrelevant ones.

Assumption
We Assume that the source table is partitioned by date, this is the norm for large scale BQ tables.

2. Use Variables Instead of Dynamic Subqueries

The subquery approach shown above is known as a dynamic partition filter, whereas using a predicate with a constant value is called a static filter. Dynamic filters don’t guarantee that partition pruning works correctly, because BigQuery doesn’t know the value at compile time. The problem is compounded if you add transformations on one or both sides of the comparison, or if there is a type mismatch between the two sides (e.g. a DATE column compared against a TIMESTAMP).

To fix this, we can use procedural language to declare a variable, assign the value of the incremental watermark to it, and then reference that variable in the WHERE clause. In plain SQL it looks like this:

DECLARE max_updated_date DATE;
SET max_updated_at = (SELECT MAX(updated_date) FROM bq_source_table);

In Dataform, we achieve the same result using pre_operations blocks, like this:

pre_operations {
DECLARE
max_updated_date DATE;
${
when(incremental(),
`SET max_updated_date = (
SELECT MAX(updated_date)
FROM ${self()}
);`
)
}
}

Warnning :
Ensure that the DECLARE statement is inside the pre_operations block, as Dataform expects only a SELECT statement in the query body.

Press enter or click to view image in full size
Dataform job scans the entire table when using a dynamic subquery

💡 For one of our jobs, the bytes processed dropped from 2.2 TB to 80 GB after switching from a dynamic subquery to a variable in pre_operations.

Press enter or click to view image in full size
Dataform job scans only the required data when using procedural language

3. Use a Temp Table Instead of a CTE

CTEs are a great option for writing readable SQL, but they don’t always improve performance.
When the same CTE is referenced multiple times, BigQuery’s optimizer may execute the underlying subquery multiple times depending on the execution plan.

Get Abderrahmane Oubagha’s stories in your inbox

Join Medium for free to get updates from this writer.

Consider a query that joins the same users CTE twice — once to match clients, and once to match partners:

WITH users AS (...)

SELECT
order_id,
...
FROM ${ref('orders')} AS orders
JOIN users AS clients
ON orders.user_id = clients.id
AND clients.type = 'client'
JOIN users AS partners
ON orders.user_id = partners.id
AND partners.type = 'partner'

The optimizer may evaluate the expensive users query twice.

If the CTE performs significant computation, it’s often better to materialize it once.
You can use a temp table in pre_operations so the data is materialized once and reused:

pre_operations {
CREATE TEMP TABLE users AS ....
}

SELECT
order_id, ...
FROM ${ref('orders')} AS orders
JOIN users AS clients ON orders.user_id = clients.id AND clients.type = 'client'
JOIN users AS partners ON orders.user_id = partners.id AND partners.type = 'partner'

post_operations {
DROP TABLE IF EXISTS users;
}

4. Reduce MERGE Costs with updatePartitionFilter

When inserting data on the target table Datafrom give you two options:

When inserting data into the target table, Dataform gives you two options:

  • Insert (default): simple and with no overhead cost or time, but can produce duplicates.
  • Merge: you can specify a unique key (one or more columns) in the config file, and at insert time, Dataform will merge existing records based on the key from the source data and insert any new ones.

During the merge step, Dataform needs to identify which records already exist in the target table — which means it must scan it. On a large partitioned table, this comes with a significant cost. Fortunately, the updatePartitionFilter config option lets you limit how much of the target table is scanned during the merge.

Here is a config without updatePartitionFilter:

config {
type: "incremental",
name: "target",
uniqueKey: ["id"],
bigquery: {
partitionBy: "created_date"
}
}
Press enter or click to view image in full size
MERGE without updatePartitionFilter — the entire target table is scanned

Now add updatePartitionFilter to restrict the scan to only the relevant partition window:

config {
.....
uniqueKey: ["id"],
bigquery: {
...........
updatePartitionFilter:
"created_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)"
.....
}
}

After setting this, the Dataform job will scan only the past week of data when checking for records to merge, rather than the full table history.

Press enter or click to view image in full size
MERGE with updatePartitionFilter — only the relevant partitions are scanned

Warning — Potential Duplicates
If the source data contains records older than the interval specified in updatePartitionFilter, you might end up with duplicates.
To avoid this, apply the same date filter to your source data to ensure that records older than the interval either don't exist or are not relevant.

Conclusion

Most Dataform optimization discussions focus on SQL transformations. In practice, the largest gains often come from helping BigQuery scan less data.

The combination of partition pruning, static filtering with procedural variables, temp table materialization, and MERGE optimization can reduce both execution time and query costs by an order of magnitude on large incremental pipelines.

--

--