Guide demonstrating BigQuery materialized views including creation, limitations, refresh options, joins, performance benefits, and SQL examples using employees and departments tables
Welcome back. This guide expands on BigQuery fundamentals — creating datasets, tables, and views — and walks through materialized views in BigQuery: what they are, when to use them, and how to manage refresh behavior.Materialized views store precomputed query results to speed up repeated queries. They can refresh automatically on a schedule or be refreshed manually when you need immediate, up-to-date results.Prerequisites
A BigQuery dataset (this example uses kodekloud-gcp-training.demo_dataset).
A base employees table already present in the dataset (used for the materialized view source).
Quick check — view current summary
SELECT *FROM `kodekloud-gcp-training.demo_dataset.employee_summary`ORDER BY salary DESC;
Create a departments table
To demonstrate joining a materialized view with another table, create a simple departments table:
CREATE TABLE `kodekloud-gcp-training.demo_dataset.departments` ( department_id INT64 NOT NULL, department_name STRING NOT NULL, location STRING, budget INT64);
SELECT department_id, department_name, location, budgetFROM `kodekloud-gcp-training.demo_dataset.departments`ORDER BY department_id;
Relational context
Now you have two related tables: employees and departments. This is a classic relational setup where department_id in employees references department_id in departments, enabling joins at query time.Important: materialized view limitations
BigQuery materialized views are optimized for performance but have SQL restrictions. They do not support arbitrary joins across multiple base tables or every SQL construct. If you require a precomputed result combining many tables or unsupported SQL, use:
a regular view (virtual, computed at query time), or
a scheduled query that writes a denormalized table for fast access.
Warning: use materialized views when queries match the supported patterns and when caching precomputed results is beneficial.Create a materialized view (precomputed employees)
This example creates a materialized view that precomputes selected columns from the employees table, including department_id so you can join to departments in queries. The materialized view is configured to auto-refresh every 30 minutes.
The materialized view is created. In the BigQuery UI it has a distinct icon that differentiates materialized views from regular views and tables:
Query the materialized view
Run a simple query against the materialized view to read the precomputed results:
SELECT *FROM `kodekloud-gcp-training.demo_dataset.employee_department_view`ORDER BY department_id, salary DESC;
Join the materialized view with departments
If you need department metadata (name, budget), join the materialized view with the departments table at query time:
SELECT mv.employee_id, mv.full_name, mv.email, mv.salary, d.department_name, d.budgetFROM `kodekloud-gcp-training.demo_dataset.employee_department_view` AS mvJOIN `kodekloud-gcp-training.demo_dataset.departments` AS d ON mv.department_id = d.department_idORDER BY d.department_name, mv.salary DESC;
Materialized view metadata & benefits
BigQuery displays metadata for materialized views (for example, row counts and logical bytes stored). Materialized views store logical results and can reduce both query cost and latency for repeated queries that match the materialized query pattern.Table — Materialized view vs Regular view (summary)
Feature
Materialized View
Regular View
Storage
Stores precomputed results (logical)
No storage; computed at query time
Performance
Faster for repeated, supported queries
Depends on the underlying query
Refresh
Automatic interval or manual (REFRESH)
Always reflects current base tables
SQL support
Limited; no arbitrary multi-table joins
Full SQL support
Use case
Cache common aggregations/filters
Flexible joins/complex queries
Refresh behavior — demo and manual refresh
Automatic refresh: materialized view results refresh automatically on the configured interval (30 minutes in this example).
Manual refresh: you can force an immediate refresh to ensure the materialized view reflects the latest base-table changes.
Check current values for an employee (example: employee_id = 1):
After the forced refresh, the materialized view should show the updated salary (85,000).
Materialized views are useful for precomputing and accelerating common queries. They have limitations (for example, not all SQL constructs or non-deterministic functions are supported). You can rely on scheduled refresh intervals or trigger refreshes (for example, from Airflow or other orchestration tools) when you need up-to-date results.