Why Databricks materialized views matter
When a team needs a new table in the gold or presentation layer, the fastest way to keep it current is to drop and recreate it every day. That works at first. Then the table grows to 10 or 20 million rows, and rebuilding it from scratch every day drives up compute costs.
Databricks materialized views offer a better option. They can refresh incrementally, processing only what changed, instead of rebuilding everything. In this demo, Green Leaf Data Architect Jessica Hudzik shows how materialized views are created and refreshed, and what it takes to get incremental refresh working.
Step 1: Set up the demo environment (1:43)
Jessica starts with a catalog set up for the demo, a destination warehouse schema, and a source OLTP schema with a raw data volume. The first cells in the notebook set the catalog and schema, drop any objects left over from earlier runs, and clear out the raw data folder.
Step 2: Find sample data in Databricks datasets (2:41)
Databricks includes a library of sample datasets you can browse right from a notebook. There are JSON, CSV, transactional and streaming datasets, so it’s an easy place to find test data. This demo uses the NYC taxi trip data.
Step 3: Copy files into a volume (4:10)
Jessica copies three compressed NYC taxi files into the raw data volume with a short loop. Spark can read the compressed files directly, so there’s no need to unzip them first.
Step 4: Create the transactional source table (5:23)
From the first file, which holds about 7 million records for October 2019, she creates a taxi trips table in the source schema, partitioned by vendor ID. She adds a unique ID column to each row so individual records can be updated later in the demo. The table includes pickup and drop-off times, trip distance, location IDs, and charge fields such as fare amount, tip amount and total amount.
Step 5: Define the materialized view (8:16)
In Databricks, a materialized view is defined by a query. Unlike a regular view, its results are stored on disk. Jessica’s view pulls every column from the taxi trips table and adds simple business logic: a proposed fare amount 20% higher than the current fare, and a new proposed total based on it.
Step 6: Understand how refresh works (9:27)
This is the point that tripped Jessica up when she was building the demo. A materialized view does not update on its own. It doesn’t rebuild when you query it, and it doesn’t watch the source tables for changes. If you create one and do nothing else, it will never refresh.
You have three options to keep it current:
- Schedule the refresh pipeline that Databricks creates with the view
- Add a trigger on update clause to the view definition
- Refresh or recreate the view manually
Step 7: Use a SQL warehouse (11:33)
Creating the view on general serverless compute fails. Databricks requires a serverless or pro SQL warehouse for materialized view operations. Jessica uses the serverless starter warehouse, sized at 2X-Small with the shortest auto-stop timeout to keep costs low on a personal account. Keep in mind that a SQL warehouse can’t run Python, so switch back to general compute for any Python cells.
Step 8: Find the pipeline behind the view (13:33)
Once the view is created, Databricks adds a materialized view pipeline under Jobs & Pipelines. You can start it or schedule it in a job, for example to run daily. Any other change to the pipeline has to be made by updating the view’s SQL definition.
Step 9: Learn how Databricks chooses a refresh type (15:01)
You can force a full refresh. By default, though, Databricks uses a cost optimizer to decide between an incremental and a full refresh. From what Jessica found, the optimizer leans toward incremental refresh even when it looks more expensive on paper, because updating the data in place is less disruptive downstream than rebuilding the whole view.
Step 10: Check incremental refresh eligibility (17:07)
Running EXPLAIN CREATE MATERIALIZED VIEW shows whether a query can refresh incrementally, without creating the view.
- Non-aggregated query (18:19): The row-by-row pricing view isn’t eligible. The output flags that row tracking isn’t enabled on the source table, so this view would do a full refresh every time.
- Aggregated query (19:00): A query that groups by vendor, pickup date and pickup location is eligible with no issues. Because it’s aggregated, Databricks can use metadata already in the Delta log to skip unchanged files.
Some functions and features aren’t compatible with incremental refresh, so it’s worth running this check on any query before you build a view on it.
Step 11: Enable row tracking and compare results (21:11)
Jessica enables row tracking on the source taxi trips table and recreates the materialized view. The query performance stats show the first refresh reading about 7.2 million rows, as expected for an initial load.
To compare, she loads the same query into a regular table (22:46). A regular table that’s dropped and recreated every day reads and writes its full size on every run, roughly 160 to 180 MB in this case. With incremental refresh, later refreshes of the materialized view should read and write far less. The demo tracks rows and bytes rather than run time, since the 2X-Small warehouse makes timing an unreliable measure.
Key takeaways
- Materialized views need a refresh plan. Without a schedule, a trigger or a manual refresh, the view never updates.
- Check eligibility before you build. EXPLAIN CREATE MATERIALIZED VIEW shows whether a query can refresh incrementally and what’s blocking it.
- Row tracking unlocks incremental refresh for non-aggregated views. Turn it on for the source table so Databricks can find changed rows without a full rebuild.
Frequently asked questions
Do Databricks materialized views refresh automatically?
No. You need to schedule the refresh pipeline, add a trigger on update clause, or refresh the view manually.
Why can’t I create a materialized view on serverless compute?
Databricks requires a serverless or pro SQL warehouse for creating and refreshing materialized views.
What is the difference between incremental and full refresh?
A full refresh rebuilds the entire view. An incremental refresh processes only the data that changed, which reduces reads, writes and compute cost.
How do I know if my materialized view can refresh incrementally?
Run EXPLAIN CREATE MATERIALIZED VIEW with your query. The output shows whether the view is eligible and lists anything blocking incremental refresh, such as row tracking not being enabled.
In Part 2, Jessica puts incremental refresh to the test with single-row updates, multi-million-row updates and new inserts, and looks at how the optimizer picks a refresh strategy. Watch Part 2.