I am using Twillio Segment to sync web_app_ui events into bigquery (serves as data lake).
In Bigquery data we have many tables with similar structure. Most fields are common, but some are different.
We have many such tables (~200), and many are added regularly.
Each table has a view with similar structure aimed at records deduplication and performance optimization.
So I have a table: mytable
, and next to it mytable_view
I would like to query all these tables together using Table_Suffix wildcard functionality. However… since eache table has a view that matches the wildcard, I get the error message: Views cannot be queried through prefix.
I am looking for an idea for going around this limitation. And querying all tables in one query, while ignoring all views. or in other words, how to make this work?
Select a,b,c
from `my_dataset`.*
where _TABLE_SUFFIX not like "%_view'
2
Answers
As mentioned above, table wildcard won't work if it would match both views and tables. Eventually I went for the solution of creating a view using dynamic SQL. I use this code:
(simplified version)
This is scheduled to run daily. It creates a view that does Union All of all relevant tables. One can also pick relevant fields in similar manner from
INFORMATION_SCHEMA.COLUMNS
.According to the documentation:
https://cloud.google.com/bigquery/docs/querying-wildcard-tables#limitations
So, the only option you have is to isolate the views on a different dataset or project given that any _TABLE_SUFFIX will not be evaluated if there is any view matched by the wildcard in the first place.