Overview and What You Will Learn
BigQuery is Google Cloud's serverless data warehouse, and with on-demand pricing you pay for the bytes a query scans, not for rows returned. In this lab you will load data into a partitioned table, run SQL against it, and use dry runs and byte limits to see the cost of a query before you pay for it.
By the end you will be able to:
- Create a dataset and a partitioned table, and load a CSV file.
- Run standard SQL queries from the command line.
- Estimate a query's scan size with
--dry_run. - Compare
SELECT *with a column-specific query on a large public table. - Use a required partition filter and a maximum bytes billed cap as guardrails.
Why This Matters in Production
BigQuery makes it easy to run an expensive query by accident. A single SELECT * on a multi-terabyte table, run by a dozen engineers every day just to preview a few rows, can become one of the largest lines on a cloud bill without any incident to point to. Cost control in BigQuery is a set of query habits, and the earlier a team learns them, the smaller the surprise.
Core Principles
BigQuery separates storage from compute. Data is stored by column, and a query reads only the columns it names, so naming fewer columns scans fewer bytes. On a partitioned table, a filter on the partition column lets BigQuery skip whole partitions. Cost follows bytes scanned, not result size or query complexity.
Two details help you read cost numbers correctly. A dry run is free and reports the bytes a query would process. On-demand billing also rounds each table read up to a minimum of 10 MB, so tiny tables always look slightly more expensive than their real size.
Detailed Step-by-Step Practical Lab
You need gcloud, the bq tool, and a billing account ID. Cloud Shell has everything.
Step 1: Create a project and enable the API
export BILLING_ACCOUNT=0X0X0X-0X0X0X-0X0X0X gcloud projects create gcp-bq-lab-2026 --name="BigQuery Lab"gcloud billing projects link gcp-bq-lab-2026 --billing-account=$BILLING_ACCOUNTgcloud config set project gcp-bq-lab-2026gcloud services enable bigquery.googleapis.comStep 2: Create a dataset in Mumbai
bq mk --dataset --location=asia-south1 gcp-bq-lab-2026:sales_dataStep 3: Create a partitioned table that requires a partition filter
Partitioning by order_date lets queries skip dates they do not need, and the filter requirement stops accidental full scans.
bq mk --table \ --time_partitioning_type=DAY \ --time_partitioning_field=order_date \ --require_partition_filter \ gcp-bq-lab-2026:sales_data.orders \ region:STRING,revenue:FLOAT,order_date:DATEStep 4: Load sample data from a CSV file
cat > orders.csv << 'CSV_EOF'region,revenue,order_dateMumbai,4599.00,2026-01-15Delhi,2300.50,2026-01-16Mumbai,1899.00,2026-01-17Bengaluru,3120.75,2026-01-17Delhi,980.00,2026-02-02CSV_EOF bq load --source_format=CSV --skip_leading_rows=1 \ gcp-bq-lab-2026:sales_data.orders orders.csvStep 5: Run an aggregate query with a partition filter
bq query --use_legacy_sql=false \ 'SELECT region, SUM(revenue) AS total_revenue FROM `gcp-bq-lab-2026.sales_data.orders` WHERE order_date BETWEEN "2026-01-01" AND "2026-01-31" GROUP BY region ORDER BY total_revenue DESC'You should see Mumbai first with 6498.0.
Step 6: See the guardrail reject an unfiltered query
bq query --use_legacy_sql=false \ 'SELECT region, SUM(revenue) FROM `gcp-bq-lab-2026.sales_data.orders` GROUP BY region'BigQuery refuses it with an error saying you must filter on order_date. That refusal is the point, because the same rule on a large table prevents an expensive mistake.
Step 7: Estimate costs with a dry run on a large public table Your own table is too small to show the effect, so use a public table of about 20 GB. Dry runs do not run the query or cost anything.
bq query --use_legacy_sql=false --dry_run \ 'SELECT * FROM `bigquery-public-data.samples.natality`' # Only the columns you needbq query --use_legacy_sql=false --dry_run \ 'SELECT year, state, weight_pounds FROM `bigquery-public-data.samples.natality`'Compare the two "will process N bytes" messages. The second is a small fraction of the first, even though both describe the same table and neither returns a different number of rows.
TipRun
--dry_runbefore any query on a table you have not used before. The estimate is free and takes about a second.
Step 8: Put a hard cap on a query's cost
--maximum_bytes_billed makes BigQuery fail a query that would bill more than your limit, before any charge happens.
bq query --use_legacy_sql=false --maximum_bytes_billed=1000000000 \ 'SELECT * FROM `bigquery-public-data.samples.natality`'This fails with an error that the query exceeds the limit for bytes billed, because it would scan far more than 1 GB.
Step 9: Check what a real job was billed
bq ls -j -n 1# Copy the job ID from the output, then:bq show --format=prettyjson -j JOB_IDLook for totalBytesBilled, which is the number that drives your cost.
Step 10: Clean up
bq rm -r -f -d gcp-bq-lab-2026:sales_datarm -f orders.csvgcloud projects delete gcp-bq-lab-2026 --quietProduction Best Practices & Common Pitfalls
- Name columns, never
SELECT *. Columns not referenced are not read, so this is the biggest single saving. - Partition large tables and require the filter. Partition by a date or timestamp column and set
require_partition_filter, so nobody can scan history by mistake. - Cluster on frequent filters. Clustering by a column such as
customer_idfurther reduces the data read for filtered queries. - Set byte limits. Use
maximum_bytes_billedin scheduled queries and applications, and project-level custom quotas for people. - Find your expensive queries. This query ranks users by bytes billed over a week. Replace the region with the one your jobs run in:
SELECT user_email, ROUND(SUM(total_bytes_billed) / POW(1024, 4), 3) AS tib_billedFROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECTWHERE creation_time > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)GROUP BY user_emailORDER BY tib_billed DESC;- Consider capacity pricing for steady workloads. When monthly on-demand spend is large and predictable, slot reservations can cost less and cap spend.
- Do not use BigQuery as an application database. It is built for analytical scans, not for frequent small reads and writes on every user request. Use Cloud SQL, Spanner, or Firestore for that.
Common MistakeBelieving
LIMIT 10makes a query cheap. On most tables BigQuery still scans every referenced column in full, so you pay for the scan regardless of how few rows are returned.
Common MistakeWrapping the partition column in a function, such as
WHERE CAST(order_date AS STRING) = "2026-01-15". That can stop partition pruning, so filter on the raw column.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
bq mk --dataset |
Create a dataset |
bq mk --table --time_partitioning_field |
Create a partitioned table |
bq load |
Load a file into a table |
bq query --use_legacy_sql=false |
Run a standard SQL query |
bq query --dry_run |
Estimate bytes processed without running |
bq query --maximum_bytes_billed= |
Fail a query that would bill too much |
bq show -j JOB_ID |
Inspect a job, including totalBytesBilled |
| Symptom | Likely fix |
|---|---|
| Error about a missing partition filter | Add a WHERE on the partition column, such as order_date |
| Query exceeds the limit for bytes billed | Select fewer columns, filter partitions, or raise the cap deliberately |
| Table not found in query | Use the full project.dataset.table name, and match the dataset's location |
| Dry run shows a surprisingly large scan | Check for SELECT * and for functions around the partition column |