# BigQuery CREATE MATERIALIZED VIEW: Syntax and Examples | Masthead

CREATE MATERIALIZED VIEW stores the result of an aggregation over a BigQuery table and keeps it current automatically. Queries read the small precomputed result plus recent changes instead of rescanning the base table, cutting cost for dashboards that repeat the same GROUP BY.

> For the index of this section of the site, see [llms.txt](https://mastheadata.com/llms.txt)

BigQuery · DDL statement

# BigQuery CREATE MATERIALIZED VIEW

CREATE MATERIALIZED VIEW stores the result of an aggregation over a BigQuery table and keeps it current automatically. Queries read the small precomputed result plus recent changes instead of rescanning the base table, cutting cost for dashboards that repeat the same GROUP BY.

GoogleSQL for BigQuery · Checked against the [official reference](https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_materialized_view_statement) in November 2026

A materialized view is a query whose result BigQuery stores and maintains for you. By default it is incremental: each refresh processes only the changes to the base table since the last refresh. When you query the view, BigQuery combines the stored result with any changes since that refresh, so results are always current. With smart tuning, BigQuery also rewrites queries against the base table to read the view instead, as long as the view contains every column and row the query needs. Querying the view directly always uses it.

Refresh is automatic unless you set `enable_refresh = FALSE`. BigQuery tries to start a refresh within 5 minutes of a base table change, but no more often than `refresh_interval_minutes` (default 30, allowed from 1 minute to 7 days), and only on a best-effort basis: refreshes run like batch queries and wait when the project has no capacity. You can refresh at any time with `CALL BQ.REFRESH_MATERIALIZED_VIEW('project.dataset.view')`. Appends to the base table refresh incrementally. `UPDATE`, `DELETE`, `MERGE`, truncation and partition expiration on the base table invalidate the stored data, and until the next refresh queries fall back to reading the base table. In a partitioned view only the affected partitions are invalidated, which is a good reason to partition the view the same way as the base table.

Incremental views accept a restricted query: `SELECT`, `FROM`, `WHERE`, `GROUP BY` and inner joins, with aggregates such as `COUNT`, `COUNTIF`, `SUM`, `AVG`, `MIN`, `MAX`, `APPROX_COUNT_DISTINCT`, `ANY_VALUE`, `ARRAY_AGG`, `LOGICAL_AND`/`LOGICAL_OR` and `MAX_BY`/`MIN_BY`. Aggregates must be outputs, so `COUNT(*) / 10` or a filter on an aggregate is rejected. Window functions, `RIGHT` and `FULL` outer joins, self-joins, UDFs, `TABLESAMPLE`, `FOR SYSTEM_TIME AS OF` and non-deterministic functions such as `CURRENT_DATE()` are not allowed; `LEFT JOIN` and `UNION ALL` are in Preview. For anything else, set `allow_non_incremental_definition = TRUE` together with `max_staleness`: the view then accepts most SQL, but every refresh recomputes it in full and smart tuning no longer applies. You cannot change a view's query after creation (use `CREATE OR REPLACE`), write to it with DML, or build a materialized view on top of another one.

You pay for three things: refreshes (bytes processed on demand, or slots), storage of the view, and queries, which read the view plus whatever part of the base table has changed since the last refresh. Automatic refreshes are billed to the view's project and run as the `bigquery-adminbot@system.gserviceaccount.com` service account, so they show up in that project's job history. `max_staleness` (30 minutes to 3 days) lets queries on the view skip the base table entirely while the last refresh is recent enough. On reservations, Standard edition can only query existing materialized views; creating them, automatic refresh and smart tuning need Enterprise, Enterprise Plus or on-demand pricing.

## Syntax

```
CREATE [OR REPLACE] MATERIALIZED VIEW [IF NOT EXISTS] view_name
[PARTITION BY partition_expression]
[CLUSTER BY column[, ...]]
[OPTIONS (
  enable_refresh = { TRUE | FALSE },
  refresh_interval_minutes = n,
  max_staleness = INTERVAL ...,
  allow_non_incremental_definition = { TRUE | FALSE }, ...)]
AS query_expression

ALTER MATERIALIZED VIEW view_name SET OPTIONS (option = value[, ...])

CALL BQ.REFRESH_MATERIALIZED_VIEW('project.dataset.view_name')
```

## Arguments and return value

| Argument | Description |
| --- | --- |
| `PARTITION BY` | Allowed only when the base table is partitioned, on the same partitioning column with the same granularity. The column can be a finer truncation of the base column, such as `TIMESTAMP_TRUNC(ts, HOUR)` over a table partitioned by `DATE(ts)`. The view inherits the base table's partition expiration. |
| `CLUSTER BY` | Up to four output columns of the view. Aggregate outputs cannot be clustering columns. |
| `enable_refresh` | BOOL, default TRUE. FALSE stops automatic refresh; you can still refresh manually. |
| `refresh_interval_minutes` | FLOAT64, default 30. The minimum time between automatic refreshes, from 1 minute to 7 days. |
| `max_staleness` | INTERVAL between 30 minutes and 3 days, disabled by default. Queries on the view return stored data without reading the base table while the last refresh is within this interval. |
| `allow_non_incremental_definition` | BOOL, default FALSE, settable only at creation. TRUE accepts most SQL (outer joins, HAVING, window functions) but requires `max_staleness`, recomputes the whole view on each refresh and disables smart tuning. |

**Returns:** A materialized view: stored, automatically maintained query results that can be queried like a table and used to answer matching queries on the base table.

## Examples

### Hourly revenue per store, partitioned like the base table

`orders` is partitioned by `DATE(order_ts)`. The view truncates the timestamp to the hour and partitions by its date, so a DELETE in one day of `orders` invalidates only that day of the view.

```
CREATE MATERIALIZED VIEW `project.dataset.hourly_revenue_mv`
PARTITION BY DATE(order_hour)
CLUSTER BY store_id
OPTIONS (enable_refresh = TRUE, refresh_interval_minutes = 60)
AS
SELECT
  TIMESTAMP_TRUNC(order_ts, HOUR) AS order_hour,
  store_id,
  COUNT(*) AS orders,
  SUM(amount) AS revenue
FROM `project.dataset.orders`
GROUP BY order_hour, store_id;
```

### Read the view from a dashboard query

Re-aggregating the hourly rows is cheap: the scan covers the view's November partitions, not the raw orders. The same question asked of `orders` is rewritten to use the view only if every column and filter is an output of the view, for example a filter on `TIMESTAMP_TRUNC(order_ts, HOUR)` rather than on `order_ts`.

```
SELECT
  store_id,
  SUM(orders) AS orders,
  SUM(revenue) AS revenue
FROM `project.dataset.hourly_revenue_mv`
WHERE order_hour >= '2026-11-01'
GROUP BY store_id
ORDER BY revenue DESC;
```

### Slow down refreshes and refresh by hand

For a report read twice a day, a 4-hour refresh cap and an 8-hour staleness window keep refresh cost down and let queries skip the base table. The CALL refreshes immediately and is billed to the project that runs it.

```
ALTER MATERIALIZED VIEW `project.dataset.hourly_revenue_mv`
SET OPTIONS (
  refresh_interval_minutes = 240,
  max_staleness = INTERVAL "8:0:0" HOUR TO SECOND
);

CALL BQ.REFRESH_MATERIALIZED_VIEW('project.dataset.hourly_revenue_mv');
```

### A non-incremental view for a query incremental views reject

HAVING is not allowed in an incremental view. With `allow_non_incremental_definition` it works, but every refresh reruns the whole query over `orders`, so the refresh interval directly sets the cost.

```
CREATE MATERIALIZED VIEW `project.dataset.high_value_customers_mv`
OPTIONS (
  enable_refresh = TRUE,
  refresh_interval_minutes = 720,
  max_staleness = INTERVAL "24:0:0" HOUR TO SECOND,
  allow_non_incremental_definition = TRUE
)
AS
SELECT customer_id, SUM(amount) AS lifetime_value
FROM `project.dataset.orders`
GROUP BY customer_id
HAVING SUM(amount) > 10000;
```

## Common mistakes

*   Computing on an aggregate inside an incremental view. `SUM(amount) / COUNT(*) AS avg_order` or `COUNT(*) / 10` is rejected; store `SUM(amount)` and `COUNT(*)` and divide when you query the view.
*   Using `CURRENT_DATE()` to keep the view to a rolling window. Non-deterministic functions are not allowed. Store all dates in the view, or let partition expiration on the base table trim it, and filter by date when you query.
*   Running frequent UPDATE, DELETE or MERGE on the base table of an unpartitioned view. Each change invalidates the whole view, so queries read the base table until the next full refresh and you pay for both. Partition the view like the base table so only touched partitions are invalidated.
*   Expecting a filter on the raw column to be rewritten to the view. Smart tuning needs every column and filter to be an output of the view: a view that outputs `DATE(order_ts)` is not used by a query filtering on `order_ts`.
*   Forgetting that `require_partition_filter` on the base table also applies to queries on the view: they fail with [Cannot query over table without a filter that can be used for partition elimination](https://mastheadata.com/bigquery/errors/cannot-query-without-partition-filter.md) unless they filter on the partitioning column.

## Cost and performance

*   Refresh cost is the hidden line item. An incremental refresh processes only rows appended since the last one, but a non-incremental view reruns its whole query on every refresh, and UPDATE, DELETE or MERGE on the base table forces the affected data to be recomputed (all of it if the view is unpartitioned). Refresh jobs run as `bigquery-adminbot@system.gserviceaccount.com` in the view's project, so you can find them in INFORMATION\_SCHEMA.JOBS.
*   Between refreshes, queries read the view plus the base table changes since the last refresh. With a long `refresh_interval_minutes` on a busy table, that delta grows and so does query cost; `max_staleness` caps it by serving stored results while they are fresh enough.
*   For `AVG`, `ARRAY_AGG` and `APPROX_COUNT_DISTINCT`, the view stores an intermediate sketch rather than the final value, and storage is calculated on that sketch as BYTES, not on the NUMERIC or FLOAT64 the column shows.

## FAQ

How often does a BigQuery materialized view refresh?

By default BigQuery starts a refresh within 5 minutes of a base table change, but no more than once every 30 minutes (`refresh_interval_minutes`). It is best effort. Run `CALL BQ.REFRESH_MATERIALIZED_VIEW(...)` to refresh immediately.

What is the difference between a view and a materialized view in BigQuery?

A [view](https://mastheadata.com/bigquery/sql/create-view.md) stores only SQL and reruns it, with full cost, on every query. A materialized view stores the result, keeps it current incrementally and can answer queries on the base table, at the price of refresh and storage costs and a restricted query syntax.

Can a BigQuery materialized view use a LEFT JOIN or a window function?

Incremental views allow inner joins; `LEFT JOIN` and `UNION ALL` are in Preview. Window functions, outer joins and HAVING need `allow_non_incremental_definition = TRUE` with `max_staleness`, which turns every refresh into a full recompute.

Who pays for materialized view refreshes?

Automatic refreshes are billed to the project that contains the view; a manual `BQ.REFRESH_MATERIALIZED_VIEW` call is billed to the project that runs it.

## Related

[CREATE VIEW](https://mastheadata.com/bigquery/sql/create-view.md)[CREATE TABLE](https://mastheadata.com/bigquery/sql/create-table.md)[PARTITION BY (partitioned tables)](https://mastheadata.com/bigquery/sql/partition-by.md)[CLUSTER BY](https://mastheadata.com/bigquery/sql/cluster-by.md)[INFORMATION\SCHEMA.JOBS](https://mastheadata.com/bigquery/sql/information-schema-jobs.md)[MERGE](https://mastheadata.com/bigquery/sql/merge.md)[INFORMATION\SCHEMA](https://mastheadata.com/bigquery/sql/information-schema.md)

### Errors you may hit

[Cannot query over table without a filter for partition elimination](https://mastheadata.com/bigquery/errors/cannot-query-without-partition-filter.md)

### More DDL statements

[ALTER TABLE](https://mastheadata.com/bigquery/sql/alter-table.md)[CREATE FUNCTION](https://mastheadata.com/bigquery/sql/create-function.md)[CREATE TEMP TABLE](https://mastheadata.com/bigquery/sql/create-temp-table.md)

[All BigQuery DDL statements](https://mastheadata.com/bigquery/ddl.md) · [All statements and clauses](https://mastheadata.com/bigquery/sql.md) · [BigQuery error messages](https://mastheadata.com/bigquery/errors.md)
---

## Other pages in sql

> For the full index of this section, see [BigQuery Reference](https://mastheadata.com/llms/llms-bigquery-reference.txt).

- [BigQuery ALTER TABLE: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/alter-table.md): ALTER TABLE changes a BigQuery table without rewriting it: ADD COLUMN, DROP COLUMN, RENAME TO, RENAME COLUMN, SET OPTIONS (expiration, description, labels, partition filter) and ALTER COLUMN SET DATA TYPE for widening changes such as INT64 to NUMERIC. These are metadata changes and process no bytes.
- [BigQuery CLUSTER BY: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/cluster-by.md): CLUSTER BY sorts a BigQuery table's storage blocks by up to four columns, so queries that filter on those columns skip blocks and bill fewer bytes. Use it on large tables filtered by columns such as customer_id or status, usually on top of date partitioning.
- [BigQuery CREATE FUNCTION: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/create-function.md): CREATE FUNCTION defines a user-defined function (UDF) in SQL or JavaScript. CREATE TEMP FUNCTION lives for one query or script; CREATE FUNCTION dataset.name is persistent and shared. Prefer SQL UDFs: they are inlined, while JavaScript UDFs use more slots and lack 64-bit integers.
- [BigQuery CREATE TABLE: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/create-table.md): CREATE TABLE creates a BigQuery table from a column list, from a query (CREATE TABLE AS SELECT), or both, with optional partitioning, clustering and OPTIONS such as expiration and description. Add OR REPLACE to overwrite a table or IF NOT EXISTS to skip one that exists.
- [BigQuery CREATE TEMP TABLE: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/create-temp-table.md): CREATE TEMP TABLE stores intermediate results in a table that exists only for the current multi-statement query or session, without a dataset. Use it to compute an expensive step once and reuse it, instead of a CTE that BigQuery re-runs at every reference.
- [BigQuery CREATE VIEW: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/create-view.md): CREATE VIEW saves a query under a table-like name. A BigQuery view stores no data and re-runs its query on every use, billed for the underlying tables' bytes. Authorize the view on the source dataset to share a filtered subset without granting access to the tables.
- [BigQuery DECLARE and SET: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/declare-set.md): DECLARE creates a variable in a BigQuery multi-statement query and SET assigns it: DECLARE run_date DATE DEFAULT CURRENT_DATE(); SET run_date = '2026-11-22'. Variables make scripts reusable, and a date variable in a WHERE clause still prunes partitions.
- [BigQuery DELETE: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/delete.md): DELETE removes the rows of a BigQuery table that match a WHERE clause, which is mandatory. A DELETE that covers whole partitions and filters only on the partitioning column can drop them without scanning bytes; any other DELETE bills the bytes read plus the size of the table or partitions it rewrites.
- [BigQuery EXECUTE IMMEDIATE: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/execute-immediate.md): EXECUTE IMMEDIATE runs a SQL statement built as a string at run time, with values bound through USING and results stored with INTO. Use it for dynamic table names, generated PIVOT column lists and maintenance across many tables; bind values as parameters, never by concatenation.
- [BigQuery EXPORT DATA: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/export-data.md): EXPORT DATA writes the result of a query to files in Cloud Storage (CSV, JSON, Avro or Parquet), or to Bigtable, Spanner, Pub/Sub or AlloyDB. The export itself is free; you pay for the query that produces the data, and the URI must contain one  wildcard.
- [BigQuery GROUP BY ROLLUP, CUBE and GROUPING SETS: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/group-by-rollup.md): ROLLUP, CUBE and GROUPING SETS compute several GROUP BY levels in one query: ROLLUP(a, b) adds subtotals per a and a grand total, CUBE(a, b) adds every combination, and GROUPING SETS lists the exact groupings you want. GROUPING() tells subtotal rows from real NULLs.
- [BigQuery INFORMATION_SCHEMA.JOBS: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/information-schema-jobs.md): INFORMATION_SCHEMA.JOBS lists every BigQuery job in a project for the last 180 days, with bytes billed, slot milliseconds, errors and query text. Query it to find the most expensive queries, attribute cost by user or label, and turn total_bytes_billed into an on-demand cost estimate.
- [BigQuery INFORMATION_SCHEMA: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/information-schema.md): INFORMATION_SCHEMA is BigQuery's set of read-only metadata views for tables, columns, partitions, storage and jobs. Query mydataset.INFORMATION_SCHEMA.TABLES or region-us.INFORMATION_SCHEMA.TABLE_STORAGE to audit schemas, find large tables and estimate storage cost. Each on-demand query bills at least 10 MB.
- [BigQuery INSERT: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/insert.md): INSERT adds rows to a BigQuery table from a VALUES list or a SELECT query. On-demand it bills only the bytes the SELECT reads. Insert in batches: BigQuery is not built for row-by-row INSERTs, and after 1,500 INSERT statements on a table in a day, extra ones are queued.
- [BigQuery JOIN: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/join.md): JOIN combines rows of two tables on a condition: INNER JOIN keeps matches, LEFT, RIGHT and FULL OUTER JOIN also keep unmatched rows with NULLs, and CROSS JOIN pairs every row with every row. In BigQuery a comma or CROSS JOIN with UNNEST flattens an array column.
- [BigQuery LOAD DATA: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/load-data.md): LOAD DATA loads files from Cloud Storage (CSV, JSON, Avro, Parquet, ORC) into a BigQuery table from SQL: LOAD DATA INTO appends, LOAD DATA OVERWRITE replaces, and a missing table is created. If the load fails, the table is left unchanged.
- [BigQuery LOOP, WHILE and FOR...IN: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/loops.md): BigQuery scripts loop with LOOP ... END LOOP (exit with BREAK or LEAVE), WHILE condition DO ... END WHILE, REPEAT ... UNTIL ... END REPEAT, and FOR row IN (query) DO ... END FOR. Use them for backfills and maintenance, not row-by-row processing: each statement inside runs as its own job.
- [BigQuery MERGE: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/merge.md): MERGE updates, inserts and deletes rows of a BigQuery table in one atomic statement, by matching it against a source table or query. It is BigQuery's upsert: WHEN MATCHED THEN UPDATE, WHEN NOT MATCHED THEN INSERT, and WHEN NOT MATCHED BY SOURCE THEN DELETE for rows that disappeared.
- [BigQuery PARTITION BY (partitioned tables): Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/partition-by.md): PARTITION BY in CREATE TABLE splits a BigQuery table into partitions by a DATE, TIMESTAMP or DATETIME column, by ingestion time, or by integer ranges. Queries that filter on the partitioning column read and bill only the matching partitions, which makes it the main lever on query cost.
- [BigQuery PIVOT: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/pivot.md): PIVOT turns row values into columns with an aggregate: PIVOT(SUM(revenue) FOR quarter IN ('Q1', 'Q2', 'Q3', 'Q4')) gives one revenue column per quarter. The IN list must be constants, so build it with EXECUTE IMMEDIATE when the values are dynamic.
- [BigQuery QUALIFY: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/qualify.md): QUALIFY filters rows on the result of a window function, the way HAVING filters on aggregates. Use it to keep the latest row per key or the top N per group without a subquery: QUALIFY ROW_NUMBER() OVER (PARTITION BY id ORDER BY updated_at DESC) = 1.
- [BigQuery SELECT * EXCEPT: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/select-except.md): SELECT  EXCEPT (col1, col2) returns every column except the ones you list. In BigQuery it also lowers on-demand cost, because excluded columns are not read: dropping a wide payload or blob column from SELECT  can cut the bytes billed by most of the table.
- [BigQuery SELECT * REPLACE: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/select-replace.md): SELECT  REPLACE (expression AS column) returns every column but swaps the value of the named columns for an expression, keeping their names and positions. Use it to mask, round, cast or clean a few columns without listing all the others.
- [BigQuery TABLESAMPLE SYSTEM: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/tablesample.md): TABLESAMPLE SYSTEM (n PERCENT) reads a random subset of a BigQuery table's storage blocks instead of the whole table, so exploratory queries on large tables scan and bill only about n percent of the bytes. Results are approximate and change on every run.
- [BigQuery TRUNCATE TABLE: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/truncate-table.md): TRUNCATE TABLE removes every row from a BigQuery table but keeps its schema, partitioning, clustering, description and labels. It is a metadata operation, so it costs nothing, unlike DELETE WHERE TRUE, which is billed as DML.
- [BigQuery UNION ALL and UNION DISTINCT: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/union.md): UNION ALL stacks the rows of two queries; UNION DISTINCT does the same and removes duplicates. BigQuery requires the ALL or DISTINCT keyword, matches columns by position unless you add BY NAME, and also offers INTERSECT DISTINCT and EXCEPT DISTINCT.
- [BigQuery UNPIVOT: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/unpivot.md): UNPIVOT turns columns into rows: UNPIVOT(amount FOR month IN (jan, feb, mar)) gives one row per month with its value in amount. Rows whose value is NULL are dropped unless you add INCLUDE NULLS.
- [BigQuery UPDATE: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/update.md): UPDATE changes column values in existing BigQuery rows. The WHERE clause is mandatory (use WHERE TRUE to update every row), and UPDATE ... FROM joins another table. On-demand it bills the bytes read plus the full size of the table, or of the partitions being updated.
- [BigQuery WITH RECURSIVE: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/with-recursive.md): WITH RECURSIVE defines a CTE that references itself, so a query can walk hierarchies and graphs of unknown depth: org charts, category trees, dependency lineage. BigQuery stops a recursive CTE with an error after 500 iterations.
- [BigQuery WITH (CTE): Syntax and Examples | Masthead](https://mastheadata.com/bigquery/sql/with.md): WITH defines named subqueries (common table expressions) that the rest of a query can reference like tables. In BigQuery a non-recursive CTE is not materialized: each reference runs it again, so a CTE over a large table used three times is scanned and billed three times.
- [BigQuery SQL Syntax: Statements, Clauses, DDL and DML | Masthead](https://mastheadata.com/bigquery/sql.md): BigQuery SQL syntax reference: QUALIFY, PIVOT, MERGE, CTEs, partitioned and clustered tables, scripting and INFORMATION_SCHEMA. 31 statements and clauses with examples and cost notes.
