0
0
DbtConceptBeginner · 3 min read

View Materialization in dbt: What It Is and How It Works

In dbt, view materialization creates a database view instead of a table. This means the data is not stored physically but is generated dynamically when queried, making it fast to update but potentially slower to read for large datasets.
⚙️

How It Works

Imagine a view as a window into your data rather than a stored snapshot. When you use view materialization in dbt, it tells the database to create a virtual table that runs the SQL query every time you look through the window. This means the data is always fresh but not saved as a separate table.

This is like having a recipe book (the SQL query) instead of a prepared meal (a table). Every time you want to eat, you cook fresh from the recipe. This saves storage space and keeps data up-to-date but can take longer to get the meal ready if the recipe is complex.

💻

Example

This example shows how to define a model in dbt that uses view materialization. The model creates a view that selects all customers from a source table.
yaml and sql
version: 2
models:
  - name: customers_view
    materialized: view
    description: "A view of all customers"

-- In models/customers_view.sql
select * from raw.customers
Output
A database view named 'customers_view' is created that dynamically shows all rows from 'raw.customers' whenever queried.
🎯

When to Use

Use view materialization when you want to save storage space and keep data always fresh without needing to rebuild tables. It is ideal for small to medium datasets or when the underlying data changes frequently.

For example, if you have a dashboard that needs the latest data instantly and the dataset is not huge, a view is a good choice. However, for large datasets or complex transformations, tables might be faster because they store results physically.

Key Points

  • View materialization creates a virtual table that runs the query on demand.
  • It saves storage but can be slower for large or complex queries.
  • Best for fresh data needs and smaller datasets.
  • Defined in dbt by setting materialized: view in the model config.

Key Takeaways

View materialization creates a dynamic, virtual table that runs the query each time it is accessed.
It saves storage space but may slow down queries on large datasets.
Use views for fresh data needs and smaller datasets with frequent updates.
In dbt, set materialization to 'view' in your model configuration to use this.
For heavy or complex data, consider table materialization instead for better performance.