(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to create materialized views in Databricks: step by step

João Barros 05 de December de 2026 4 min read

This tutorial explains how to create and use materialized views in Databricks to accelerate analytical queries over Delta tables. You will see why a materialized view can improve latency for repeated queries and how to manage its refresh and storage.

Prerequisites

  • Databricks account with a workspace and a running cluster.
  • Delta files or Delta tables existing in the metastore.
  • Permissions to create tables and views in the target schema/Database.
  • Basic knowledge of SQL in Databricks and using notebooks.

Step 1: Choose the query to materialize

Identify the heavy or repeated query that justifies a materialized view. Look for aggregation operations, large joins or repeated filters. The idea is to store the pre-computed result to speed up reads.

Step 2: Create the materialized view

Use Databricks SQL syntax to create a materialized view. You can choose the name and the schema. Here is an example that aggregates sales by day from a Delta table called sales_delta.

CREATE MATERIALIZED VIEW IF NOT EXISTS analytics.daily_sales_mv
AS
SELECT
  sale_date,
  region,
  SUM(amount) AS total_amount,
  COUNT(*) AS total_transactions
FROM sales_delta
WHERE sale_date >= date_sub(current_date(), 90)
GROUP BY sale_date, region;

Explanation: we use IF NOT EXISTS to avoid failing if it already exists. The filter for the last 90 days keeps the view limited in size. Adjust partitions and columns as needed.

Step 3: Verify and query the materialized view

Confirm that the view was created and test a read query. Queries against the materialized view should be faster than recalculating the aggregation.

-- Verify existence and metadata
SHOW TABLES IN default LIKE 'daily_sales_mv';
DESCRIBE DETAIL default.daily_sales_mv;

-- Query the materialized view
SELECT sale_date, region, total_amount
FROM default.daily_sales_mv
WHERE sale_date = current_date() - INTERVAL 1 DAY;

Step 4: Refresh and refresh policy

Decide how and when to refresh the materialized view. Databricks can support incremental refresh, but the mechanism depends on the version and configuration. You can choose scheduled refreshes with Workflows or a manual refresh.

-- Full manual refresh
REFRESH MATERIALIZED VIEW default.daily_sales_mv;

-- For a scheduled refresh, create a Job/Workflow that runs the REFRESH at intervals.

If the source data is updated frequently, consider a pipeline that MERGEs into the source Delta and then refreshes the view. For large volumes, consider incremental refreshes (recreate only the affected partition) instead of a full refresh.

Step 5: Optimization and best practices

To improve performance and cost:

  • Limit the dataset materialized (time windows, essential columns).
  • Partition the view/materialization by columns commonly queried (e.g. sale_date).
  • Monitor storage and refresh time; use Jobs for off-peak refreshes.
  • Check whether your Databricks version supports incremental materialized views and enable the corresponding option.

Step 6: Handling common errors

Frequent errors and solutions:

  • Permission denied: ensure CREATE/REFRESH on the schema and access to the Delta tables.
  • View too large: reduce the time window or use filters to partition the data.
  • Slow refreshes: optimize the base query, indexing and compaction of Delta files.
  • Inconsistencies after refresh: ensure the source is in a consistent state (Delta ACID transactions help).

Verify the outcome

Confirm improved latency by comparing execution times before and after. Use EXPLAIN and the execution plan to see if the materialized view is being used. Measure time with simple SELECT queries and record the difference.

-- Check execution plan
EXPLAIN SELECT sale_date, SUM(amount) FROM default.daily_sales_mv GROUP BY sale_date;

-- Measure time (simple example)
SELECT CURRENT_TIMESTAMP();
SELECT sale_date, SUM(amount) FROM sales_delta WHERE sale_date = '2026-01-01' GROUP BY sale_date;
SELECT CURRENT_TIMESTAMP();

-- Then measure query against the view
SELECT CURRENT_TIMESTAMP();
SELECT sale_date, total_amount FROM default.daily_sales_mv WHERE sale_date = '2026-01-01';
SELECT CURRENT_TIMESTAMP();

Conclusion

Materialized views in Databricks are useful to accelerate repeated analytical queries and reduce CPU cost. Next steps: test incremental refreshes, integrate with Databricks Workflows and monitor costs. Tip: start with a small view and validate the gains before materializing more queries.