Scheduled queries in BigQuery

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
Search for a command to run...

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
No comments yet. Be the first to comment.
Hands-on guides to data tooling, infrastructure, and operations — scheduling, pipelines, access control, and the engineering behind data workflows.
🔍 What exactly are BigQuery Labels? They're key-value pairs that you can associate with different BigQuery resources like datasets, tables, views, and even jobs within a session. Consider them as 'tags' that streamline the organization, tracking, an...
Here's a useful Dataform concept: pre_operations and post_operations. As the name implies, these represent a set of actions that run before and after the main operation (table, view, or SQL operations

BigQuery has always been a SQL engine for tabular data. Object tables add an interesting twist to that. Instead of rows containing values, an object table gives you one row per file — pointing at da

Query your data lake with warehouse-grade security and performance — without moving a single file.

Ever run a heavy BigQuery SQL query, processed gigabytes of data — and then accidentally closed the tab or forgot to save the results? 😬 Don't re-run it. Your results are still there. BigQuery automa

You can use query parameters in BigQuery hashtag#SQL (now in the console as well!) — but how are they different from variables, and when should you use each? Both parameters and variables act as place

Scheduled Queries in BigQuery allow users to run SQL tasks on a predefined, automated schedule.
Instead of manually initiating a query each day or week, BigQuery can do it for you. This is perfect for light orchestration of repetitive data transformations, updates, and regular reporting tasks.
A scheduled query can be created manually using the Schedule button in the Query window.

A dialog box is presented where we are prompted to provide the following information:
a name for the scheduled query
a scheduling option or the option to run it manually (on demand)
start date and end date for which this query would run
optionally, a destination dataset and table for the query result (including the possibility of adding a partitioning field)
region selection
encryption options
principal (user or service account) to run the query under
write disposition, in order words what to do with the contents already in the table: keep (and append new data) or overwrite (and replace with the new data)
notification options


A schedule can also be created using Terraform, as per the following example.
This option provides a subset of options from the manual method - at the time of writing, for example, the write disposition cannot be set up here.
variable "envConfig" {
type = map(object({
project_id = string
service_account_name = string
}))
}
variable "config" {
type = map(object({
name = string
query = string
destination_table_name_template = string
}))
}
resource "google_bigquery_data_transfer_config" "query_config" {
for_each = var.config
display_name = each.value["name"]
location = "europe"
data_source_id = "scheduled_query"
schedule = "every saturday 05:00"
destination_dataset_id = "learning"
params = {
destination_table_name_template = each.value["destination_table_name_template"]
write_disposition = "WRITE_APPEND"
query = each.value["query"]
}
service_account_name = var.envConfig["dev"].service_account_name
project = var.envConfig["dev"].project_id
}
It's also possible to create a scheduled query using one of the BigQuery APIs or the bq CLI command - check the GCP documentation here.
Viewing the scheduled query can be done by Accessing the 'Scheduled queries' option in the BigQuery subgroup.
It will display a list of queries, their schedule, region, destination (if any) and next run time.

Click on a particular query would present a Run history and its output. There is also an option for scheduling a backfill (which we can use for a Manual Run).

Clicking on the Configuration tab would display the configurations used to create the query.

In conclusion, BigQuery Scheduled queries can be a useful tool in your toolset, as a quick and easy way to do light orchestration of SQL tasks.
Thanks for reading!
🚀 Delving into BigQuery, Google Cloud and Analytics? Stay connected with me for valuable insights, handy tips, and strategies to effortlessly traverse the vast universe of cloud computing and data analytics!
Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.
Enjoyed this? Here are some related articles you might find useful: