- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
In Declarative Pipelines, every decorated function becomes an independent dataset with its own refresh query and Spark DataFrames are inherently lazy. spark.read.table("source_table") captures a logical plan but not data. Module level source_df variable looks like it reads once but nothing is executed at assignment time. When the pipeline refreshes each of the five materialized views builds and runs its own physical plan from that shared logical plan - so the source table gets scanned five separate times with once per MV. There is no implicit caching across independently scheduled queries.
Materialize the shared read once. The right pattern is to promote the common source read to its own materialized view. It gets computed and persisted once per update like any MV and keep it private. It keeps it invisible to the catalog and downstream consumers and exists as a pipeline scoped staging step. All five downstream MVs then read from that materialized intermediate instead of the raw source, so the scan happens once and the five views each do targeted work on top of it. On serverless compute it compounds nicely - materialized views are supported incremental sources, so the downstream MVs can refresh incrementally against the staging MV (processing upstream changes since the last update) and DP model decides increment vs full recompute automatically. Enable row tracking and deletion vectors on the source table to maximize incremental eligibility.
# Staging MV
@dp.materialized_view(name="source_base", private=True)
def source_base():
return spark.read.table("source_table")
# Downstream MV reads the materialized staging MV — no repeated source scans
@dp.materialized_view(name="mv_1")
def mv_1():
return spark.read.table("source_base").filter("condition_1")
@dp.materialized_view(name="mv_2")
def mv_2():
return spark.read.table("source_base").filter("condition_2")
@dp.materialized_view(name="mv_3")
def mv_3():
return spark.read.table("source_base").filter("condition_3")