Hi @aaron_harkins migrating from a multi-tenant MySQL structure to BigQuery while utilizing Dataform is a great choice. Consolidating common tables across multiple datasets will simplify your analysis and reporting workflows. Below is a step-by-step guide to help you achieve this efficiently:
Step 1: Identify Datasets and Tables
- List all datasets (schemas) in your BigQuery project.
- Pinpoint the common tables you want to merge, such as Account or Transactions.
- If certain datasets or tables differ due to add-ons, make sure to address these cases with conditional logic.
Step 2: Automate Table Consolidation Using Dataform
a. Create a Parameterized SQL Template
In Dataform, you can automate table merging by writing SQL-based transformations. Start by using BigQuery’s INFORMATION_SCHEMA to dynamically fetch datasets and tables. For example, the following query lists all datasets with a specific naming pattern:
SELECT schema_name
FROM <your_project>.region-us.INFORMATION_SCHEMA.SCHEMATA
WHERE schema_name LIKE ‘customer_%’;
b. Dynamically Combine Tables
To merge tables like Account across datasets, you can write a SQL script in Dataform:
SELECT *
FROM customer_1.Account
UNION ALL
SELECT *
FROM customer_2.Account
UNION ALL
SELECT *
FROM customer_3.Account;
You can automate this process further by building dynamic UNION queries using JavaScript or SQLX in Dataform.
Step 3: Automate Using BigQuery Scripting
If you prefer scripting, BigQuery allows you to loop through datasets programmatically and generate queries dynamically. Here’s an example script:
DECLARE datasets ARRAY;
SET datasets = ARRAY(
SELECT schema_name
FROM your_project.region-us.INFORMATION_SCHEMA.SCHEMATA
WHERE schema_name LIKE ‘customer_%’
);
DECLARE query STRING DEFAULT “”;
FOR dataset IN UNNEST(datasets) DO
SET query = CONCAT(query, "SELECT * FROM ", dataset, ".Account UNION ALL ");
END FOR;
– Remove the final “UNION ALL” and execute the query
SET query = LEFT(query, LENGTH(query) - 10);
EXECUTE IMMEDIATE query;
You can schedule this script to run regularly in BigQuery to keep your consolidated tables updated.
Step 4: Explore Third-Party Tools for Streamlined Automation
If manually handling scripting and SQL transformations feels overwhelming, tools like Windsor.ai can make the process much simpler. While Windsor.ai primarily focuses on data integration, its automation features allow you to:
- Seamlessly merge and load data into BigQuery.
- Automate complex workflows without requiring advanced SQL or scripting.
Final thoughts
- Use Dataform for creating parameterized SQL templates and automating transformations.
- Take advantage of BigQuery’s INFORMATION_SCHEMA and scripting to dynamically build and execute UNION queries.
- If you want a more intuitive and less manual solution, consider using tools like Windsor.ai for streamlined data workflows.
Hope this can be helpful!