Sub topic 13
How materialized views refresh incrementally
This chapter assumes you know how to define a materialized view in a pipeline, the difference between triggered and continuous updates, and basic Delta Lake concepts. It explains how a pipeline decides between recomputing a materialized view and updating only the changed part.
The working model
A materialized view stores the result of a query. On each update, the pipeline picks one of two refresh types:
- Incremental refresh: the pipeline processes only the changes in the source data and merges the effect into the stored result.
- Full recompute: the pipeline runs the whole query again and replaces the stored result.
You don't choose the type in your code. The pipeline compares the query and the source changes, estimates the cost of each approach, and picks the cheaper one. If it can't refresh incrementally, it falls back to a full recompute. The result is the same in both cases. Only the cost differs.
The daily revenue view from the medallion chapter is a typical candidate:
CREATE OR REFRESH MATERIALIZED VIEW daily_revenue
AS SELECT
date(order_ts) AS order_date,
SUM(amount) AS revenue
FROM orders_silver
GROUP BY date(order_ts);When new orders arrive in orders_silver, only the dates touched by those orders need new totals. An incremental refresh updates those rows and leaves the others alone.
What makes incremental refresh possible
Several conditions have to hold:
- Serverless compute. Incremental refresh of materialized views is supported on serverless pipelines. Classic compute pipelines always do a full recompute. This is one reason the sample bundle in this tutorial sets
serverless: true. - Change tracking on the sources. The pipeline needs to know what changed in each source table. Delta features such as row tracking and change data feed help it. Pipelines enable what they need on tables they manage, but source tables you own may need these features turned on.
- A supported query shape. Common operations such as filters, projections, joins and aggregations like
SUMandCOUNTare candidates. Non-deterministic functions, such ascurrent_timestamp()orrand(), generally force a full recompute, because the result can change without the source changing. - A cost advantage. If most of the source changed, recomputing is cheaper than tracking changes. The pipeline then recomputes.
Supported operations change between Databricks releases. Check the current documentation for the list that applies to your workspace.
Seeing which refresh happened
The event log records the refresh type for each materialized view update. Query it with the event_log function:
SELECT
timestamp,
origin.flow_name AS flow_name,
details:planning_information AS planning_information
FROM event_log(TABLE(main.demo.daily_revenue))
WHERE event_type = 'planning_information'
ORDER BY timestamp DESC
LIMIT 5;The planning information shows the technique chosen and, when the pipeline fell back to a full recompute, the reason. Use it before you rewrite a query. The reason often points to one specific cause.
Common errors
- Assuming incremental refresh is guaranteed. The pipeline is free to recompute. A view that was incremental yesterday can be recomputed today if the data changed heavily.
- Using non-deterministic functions. Placing
current_timestamp()in a filter or column makes the view a full recompute on every update. - Running on classic compute and expecting savings. Check the compute type first.
- Confusing a full recompute with a full refresh. A full recompute is the pipeline's internal choice for one update. A full refresh is an action you request, and it also resets streaming tables, which can reprocess source data.
Exercise
A materialized view filters its source with WHERE order_ts > current_timestamp() - INTERVAL 7 DAYS. The pipeline runs on serverless compute, yet every update is a full recompute. Why does this happen, and how would you confirm it?
Show answer
currenttimestamp() is non-deterministic. The result can change between updates even when the source data does not, so the pipeline can't safely update only the changed rows. To confirm, query the event log for planninginformation events for the view and read the technique and the reason for the fallback. To fix it, remove the time-relative filter from the view and apply it in a downstream query, or filter on a fixed column.