Load and query data in BigQuery with cost controls
Load a small dataset into a partitioned, clustered table and preview query cost before running it.
By Invicly Tech, , in Google Cloud Engineering and Services. About 9 minutes to read.
In short
BigQuery is Google Cloud's serverless data warehouse. You load data into tables, run SQL, and pay mostly for how much data each query reads. Partitioning, clustering and a dry run let you keep that cost small and predictable.
Think of it like this: A partitioned table is a filing cabinet with one drawer per month: asking for March means you open one drawer, not the whole cabinet.
Explained simply
BigQuery is a place to keep large tables and ask questions about them with SQL. You do not set up servers. You create a dataset, load data into a table and run queries. Google handles the machines behind it.
The key idea is that a query is billed by how much data it reads, not by how long it takes. That is why BigQuery stores data by column. If your table has 30 columns and you ask for two, it reads only those two.
Two features cut the amount read even more. Partitioning splits a table into slices, usually by day. Clustering keeps rows with similar values, such as the same customer, near each other. A filter on the partition column lets BigQuery skip whole slices.
Before you run a big query, do a dry run. It tells you how many bytes the query would read and costs nothing. You can also set a maximum bytes billed limit, so a mistake fails fast instead of making a large bill.
BigQuery is not your app's database. It is good at totals over millions of rows. For a shop that saves one order at a time, use a database like Cloud SQL and copy the data into BigQuery for reports.
Press Play on a diagram to walk through it one step at a time.
- 1Upload
Put the CSV in a Cloud Storage bucket
- 2Load
bq load into a dataset table
- 3Partition and cluster
Split by date, sort by a key column
- 4Dry run
Ask how many bytes the query would read
- 5Run with a cap
Set maximum bytes billed
- Analyst to BigQuery: Dry run: how many bytes?1Dry run: how many bytes?
- BigQuery to Analyst: Estimate: 1.2 GB2Estimate: 1.2 GB
- Analyst to BigQuery: Add partition filter, dry run again3Add partition filter, dry run again
- BigQuery to Analyst: Estimate: 40 MB4Estimate: 40 MB
- Analyst to BigQuery: Run with maximum bytes billed5Run with maximum bytes billed
- BigQuery to Analyst: Results6Results
In depth
How BigQuery charges for a query
With on-demand pricing you pay for the bytes a query processes. The count depends on which columns you select and which partitions are read. It does not depend on how many rows you return, so LIMIT does not reduce it. Check current prices on the BigQuery pricing page, because they change. The other model is capacity pricing, where you reserve compute slots and pay for those instead. Learners almost always start on-demand. Storage is billed separately and older, unchanged data can cost less.
- Select only the columns you need
- Filter on the partition column
- LIMIT does not lower bytes billed
- Dry run shows the estimate
- Maximum bytes billed is a safety cap
Partitioning and clustering together
Partition on a column you almost always filter by, such as a date. Cluster on columns you filter or join on within that, such as customer_id. BigQuery keeps metadata about each block of data, so a filter on a clustered column lets it skip blocks. You can also set require_partition_filter on a table so queries without a partition filter are rejected. This is a simple guard against full scans of a large table.
- Partition by date, timestamp or integer range
- Cluster by up to four columns
- Order of clustering columns matters
- Very small tables gain little from either
- Partition 2026-01-14
- Cluster: customer A to F
- Cluster: customer G to M
- Partition 2026-01-15
- Cluster: customer A to F
- Cluster: customer G to M
- Partition 2026-01-16
- Cluster: customer A to F
- Cluster: customer G to M
Warehouse and operational database
An operational (OLTP) database like Cloud SQL stores each row together, uses indexes and handles many small reads and writes with strong transaction rules. A warehouse like BigQuery stores each column together and is tuned to scan huge amounts of data fast. Using the wrong one hurts. Updating single rows all day in BigQuery is slow and awkward. Running a yearly sales report on your app database can slow the app down. A common design is to keep both and copy data from one to the other.
- Row storage
- Fast single-row reads and writes
- Indexes and transactions
- Powers the app
- Column storage
- Scans millions of rows fast
- Billed per bytes read
- Powers reports
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Load a CSV into a dataset~35m
- 2Partition and cluster a table~35m
- 3Dry-run a query to estimate bytes~35m
- 4Set a maximum bytes billed limit~35m
- 5Compare warehouse and OLTP access~35m
Key points
- BigQuery stores data by column, so selecting fewer columns reads fewer bytes.
- A partitioned table is split by a date or integer column. A filter on that column skips whole partitions.
- Clustering sorts data inside each partition by up to four columns, which helps filters and joins on those columns.
- A dry run returns the bytes a query would process without running it. Maximum bytes billed makes a query fail instead of overspending.
- BigQuery is built for scans and totals over many rows. A database like Cloud SQL is built for fast reads and writes of single rows.
Key terms
Tap a card to flip it. 0/6 known
Examples
bq mk --dataset --location=US shop_demo
bq load --source_format=CSV --autodetect --skip_leading_rows=1 \
--time_partitioning_field=order_date \
--clustering_fields=customer_id \
shop_demo.orders gs://example-bucket/orders.csv
bq query --use_legacy_sql=false --dry_run \
'SELECT customer_id, total FROM shop_demo.orders WHERE order_date = "2026-01-15"'
bq query --use_legacy_sql=false --maximum_bytes_billed=100000000 \
'SELECT COUNT(*) FROM shop_demo.orders'Load a CSV, then check cost before querying. Names are placeholders.
Load a CSV into a partitioned, clustered table
Prerequisites: a Google Cloud project with billing or the BigQuery sandbox, Cloud Shell, and a small CSV you made yourself with columns order_id, order_date, customer_id and total. Upload it to a bucket, create a dataset, then load. Expected evidence: bq show shows a time partitioning and clustering section. Cleanup: bq rm -r -f the dataset and delete the bucket. The free BigQuery sandbox works without a credit card, with limits such as table expiry.
gcloud storage cp orders.csv gs://example-bucket/orders.csv
bq mk --dataset --location=US shop_demo
bq load --source_format=CSV --autodetect --skip_leading_rows=1 \
--time_partitioning_field=order_date \
--clustering_fields=customer_id \
shop_demo.orders gs://example-bucket/orders.csv
bq show shop_demo.orders
# cleanup
bq rm -r -f shop_demoEstimate, then cap a query
Run the query with --dry_run and read the bytes it prints. Then run it with --maximum_bytes_billed set a little above that. If you forget the date filter, the second command fails with a bytes-billed error instead of charging you. Expected evidence: the dry run prints a number of bytes, and a too-small cap produces an error.
SQL='SELECT customer_id, SUM(total) AS spend FROM shop_demo.orders WHERE order_date BETWEEN "2026-01-01" AND "2026-01-31" GROUP BY customer_id'
bq query --use_legacy_sql=false --dry_run "$SQL"
bq query --use_legacy_sql=false --maximum_bytes_billed=50000000 "$SQL"Quick reference
- bq query --dry_run shows bytes without running the query
- --maximum_bytes_billed makes an over-budget query fail
- Partition on the date you filter by; cluster on up to four columns
- require_partition_filter blocks queries with no partition filter
- LIMIT does not reduce bytes billed
- Avoid SELECT *; name only the columns you need
- Dataset location is fixed when you create it
Common mistakes
- Running SELECT * on a big table. You pay for every column even if you only look at a few.
- Thinking LIMIT lowers the cost. BigQuery still reads the columns it scans, so LIMIT alone does not cut bytes billed.
- Using BigQuery as the main database for a web app. It is a warehouse and is not meant for many single-row updates.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Run a dry run for SELECT * on a table, then for SELECT with two named columns. Compare the bytes.
- Exercise 2medium
Create a table partitioned by order_date that rejects queries without a date filter.
- Exercise 3hard
Design a plan to report monthly revenue from a Cloud SQL orders table without slowing the app.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is BigQuery free?
- There is a free monthly query allowance and a free sandbox with limits. Beyond that you pay for storage and bytes processed, so check the current pricing page.
- Why is my query so expensive when it returns few rows?
- Cost follows the bytes scanned, not the rows returned. Filter on the partition column and select fewer columns.
- Can I partition an existing table?
- Not in place. Create a new partitioned table from a query on the old one and switch over.