Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Incremental materialized view - can't query more than once workaround

I need to create incremental materialized view for reporting purposes having the following source table:

productdatesubproductsector
A2024-01-01AAX
A2024-01-01AAX
A2024-01-01ABY
A2024-01-02AAX
B2024-01-01AAX
B2024-01-01ABX
B2024-01-01ABY
B2024-01-01ABY
B2024-01-02AAX
B2024-01-02AAY
C2024-01-02AAX
C2024-01-02AAX

The aggregated result should be the following:


productdatesubproductcount
A2024-01-01AA2
A2024-01-01AB1
A2024-01-02AA1
B2024-01-01AA1
B2024-01-01AB3
B - target2024-01-01AB2
B2024-01-02AA2
B - target2024-01-02AA1
C2024-01-02AA2

In other words, some of the product B (these in sector = Y) need to be counted twice - first time as part of B products and one more time as "B - target", because they are part of sector = Y

I tried to create the view using the following query:

SELECT 
product,
date,
subproduct,
count(*)
FROM table a
GROUP BY product, date, subproduct 
UNION ALL
SELECT 
'B - target' as product,
date,
subproduct,
count(*)
FROM table a
WHERE product = 'B' and sector = 'Y'
GROUP BY product, date, subproduct 

But I receive the the message that in BigQuery I can't target the same table twice in incremental materialized view.

I tried with CASE

CASE
WHEN product = 'B' and sector = 'Y' THEN 'B -target'
ELSE product 
END AS product 

But this time they are not included in the total B count.

Then I tried to use CTE and then use it along with another table that I need to use for other portion of the data:

 

CREATE MATERIALIZED VIEW 'test_view'
AS
WITH
cte as ( select * from table a ),
aggregations( SELECT product, date, subproduct, count(*) FROM cte a GROUP BY product, date, subproduct UNION ALL SELECT 'B - target' as product, date, subproduct, count(*) FROM cte a WHERE product = 'B' and sector = 'Y' GROUP BY product, date, subproduct
UNION ALL
SELECT product, date, subproduct, count(*) FROM table_2 b GROUP BY product, date, subproduct)
SELECT * FROM aggregations

But getting the following warning - Incremental materialized view query contains unsupported features. 

Any ideas what can I do?

Solved Solved
0 5 1,486
1 ACCEPTED SOLUTION

In your case, creating an incremental materialized view in BigQuery becomes tricky due to the limitations on how BigQuery handles UNION ALL queries and certain conditions in materialized views.

Unfortunately, materialized views in BigQuery have specific restrictions, particularly around complex queries such as those involving CTEs, UNION ALL, and aggregations. In your case, this prevents the materialized view from functioning as expected.

One option is to switch to a regular view. Regular views in BigQuery can handle complex queries, including CTEs and UNION ALL. While they do not provide the performance benefits of materialized views, they allow you to structure your query as needed. In this case, you can aggregate your data with a UNION ALL clause for the “B - target” product by creating a standard view with the required logic.

If performance is critical, a better approach might be to use a scheduled query. This involves running a query periodically (e.g., daily) to materialize the results into a new table. This avoids the restrictions on materialized views while still maintaining query performance. You can write your query to aggregate the data and save the results to a table that can be used for reporting purposes.

Finally, if the query complexity is too great for BigQuery, consider preprocessing outside BigQuery. Tools like Dataflow or Dataprep can transform and prepare the data before loading it into BigQuery, making the final table easier to query and process.

These alternatives—using a regular view, scheduling queries to materialize data, or preprocessing outside BigQuery—allow you to handle complex data aggregations without running into materialized view limitations.

View solution in original post

5 REPLIES 5

Or it is not possible to use materialized view in this case?

In your case, creating an incremental materialized view in BigQuery becomes tricky due to the limitations on how BigQuery handles UNION ALL queries and certain conditions in materialized views.

Unfortunately, materialized views in BigQuery have specific restrictions, particularly around complex queries such as those involving CTEs, UNION ALL, and aggregations. In your case, this prevents the materialized view from functioning as expected.

One option is to switch to a regular view. Regular views in BigQuery can handle complex queries, including CTEs and UNION ALL. While they do not provide the performance benefits of materialized views, they allow you to structure your query as needed. In this case, you can aggregate your data with a UNION ALL clause for the “B - target” product by creating a standard view with the required logic.

If performance is critical, a better approach might be to use a scheduled query. This involves running a query periodically (e.g., daily) to materialize the results into a new table. This avoids the restrictions on materialized views while still maintaining query performance. You can write your query to aggregate the data and save the results to a table that can be used for reporting purposes.

Finally, if the query complexity is too great for BigQuery, consider preprocessing outside BigQuery. Tools like Dataflow or Dataprep can transform and prepare the data before loading it into BigQuery, making the final table easier to query and process.

These alternatives—using a regular view, scheduling queries to materialize data, or preprocessing outside BigQuery—allow you to handle complex data aggregations without running into materialized view limitations.

Thank you for the help!

> Unfortunately, materialized views in BigQuery have specific restrictions, particularly around complex queries such as those involving CTEs, UNION ALL, and aggregations. In your case, this prevents the materialized view from functioning as expected.
Hi, does presence of the CTEs and joins (where both left and right tables change) in the Materialized view query is a restriction for incremental updates?

UNION ALL and LEFT OUTER JOIN are both currently supported in preview. See https://cloud.google.com/bigquery/docs/materialized-views-create#left-union

CTE is supported subject to the same limitations as MVV queries without CTE.  See  https://cloud.google.com/bigquery/docs/materialized-views-create#with_clause_and_common_table_expres...