27 - BigQuery 101 - SQL Data Warehouse
Welcome to Day 27 of Learn GCP in 30 Days! Today, we kick off Week 5: Analytics, DevOps & AI Capstone.
Until now, we've focused on databases designed to run your day-to-day web apps (Cloud SQL, Firestore). But what happens when your CEO asks: "What were our top 10 selling products across all 50 countries over the last 10 years?" If you run that query on your live transactional database, your website freezes and crashes!
Enter Google's legendary analytical engine: BigQuery.
Today's Goal Today, you will understand the difference between Transactional (OLTP) and Analytical (OLAP) databases, discover how Columnar Storage lets BigQuery scan billions of rows in under 2 seconds, master Partitioning and Clustering to cut query costs by 90%, query massive Google Public Datasets for free, and connect Python to BigQueryβall backed by our $0.00 credit safety guarantee!
π The Core Problem: Why Live Databases Freeze on Reports
Imagine running an e-commerce website with 50 million completed orders:
What Existed Previously:
Traditional databases like MySQL, PostgreSQL, and Oracle are Row-Oriented OLTP (Online Transaction Processing) engines:
- They store data row-by-row on disk.
- When an analyst asks for the average purchase price, the database reads every single column of all 50 million rows off the hard drive just to extract one single number!
Problems Faced:
- π’ Glacial Query Speeds: Analytical queries scanning millions of rows took hours or days to complete.
- π₯ Production Outages: Heavy reporting queries locked tables and exhausted server memory, taking down live customer-facing web apps.
- ποΈ Cluster Management Hell: Traditional data warehouses (like legacy Hadoop or on-prem Teradata) required managing hundreds of complex virtual servers, installing software updates, and manually sharding data.
- πΈ Astronomical Licensing Costs: Traditional enterprise data warehouses cost hundreds of thousands of dollars in fixed hardware and annual software licenses.
How Present Technology Solves It:
Google created BigQuery (Serverless, Petabyte-Scale Cloud Data Warehouse):
- 100% Serverless: Zero servers to provision, zero disks to manage, and zero maintenance windows.
- Columnar Storage (Capacitor Engine): Only scans the exact columns requested in your SQL query, skipping 95% of unnecessary disk I/O.
- Decoupled Massive Compute: When you run a query, Google automatically assigns thousands of CPU cores (Dremel Slots) to process your data in parallel in 1 to 3 seconds.
- Perpetual Free Sandbox: Google gives every user 1 Terabyte (TB) of query scanning for FREE every single month!
π€― The "Aha!" Moment: Row Storage vs. Columnar Storage
Why is BigQuery so unbelievably fast? Look at how data is physically arranged on disk:
π The Supermarket Analogy:
- Traditional Row Storage = Opening Every Shopping Bag:
- Imagine 1,000 customers leave a grocery store. If you want to count how many total apples were sold, you have to open every single shopping bag, pull out every loaf of bread, milk jug, and cereal box, find the apple, and put everything back.
- BigQuery Columnar Storage = The Master Item Receipt Ledger:
- BigQuery stores all apples in one aisle, all milk jugs in another aisle, and all cereal boxes in a third aisle.
- To count apples, BigQuery looks only at the apples aisle and completely ignores the other 99 aisles!
π What People Believe vs. What Actually Is
π§ Myth #1: "BigQuery is just another database like Cloud SQL."
- What People Believe: You can use BigQuery to run your mobile app user logins and shopping cart checkouts.
- What Actually Happens: BigQuery is an OLAP Data Warehouse, not an OLTP database. It is built for massive aggregate queries (
SUM,COUNT,AVG,GROUP BYacross 1 billion rows), not single-row lookups (UPDATE user WHERE id = 5;).
π§ Myth #2: "I need to configure cluster sizes and RAM before querying."
- What People Believe: You need to choose a 32-node or 64-node cluster like old Hadoop systems.
- What Actually Happens: BigQuery has zero servers. You just paste a SQL query and click Run. Google automatically spins up 2,000 CPU workers behind the scenes for 3 seconds and spins them down immediately!
π§ Myth #3: "Running SELECT * with LIMIT 10 is cheap."
- What People Believe: If I add
LIMIT 10, BigQuery only reads 10 rows. - What Actually Happens:
LIMIT 10does NOT reduce query cost in BigQuery! Because of Columnar Storage, BigQuery must scan the entire column from disk before picking the top 10 rows. - Golden Rule: Always select only the specific columns you need (e.g.,
SELECT name, scoreinstead ofSELECT *).
π The Two Golden Optimizations: Partitioning & Clustering
When working with terabytes of data, how do you make queries run 10x faster and cost 90% less? Use Partitioning and Clustering.
1. Partitioning (The Filing Cabinet Drawers)
- Concept: Divides a giant table into daily or monthly "drawers" based on a timestamp column (e.g.,
order_date). - Benefit: If your query asks for
WHERE order_date = '2026-08-26', BigQuery opens only today's drawer and completely ignores the other 364 days of the year!
2. Clustering (Sorting the Folders Inside the Drawer)
- Concept: Sorts data inside each partition by specific columns with high cardinality (e.g.,
customer_countryoruser_id). - Benefit: BigQuery skips irrelevant data blocks inside the partition, delivering sub-second response times.
π οΈ Step-by-Step Hands-On Lab: Querying Big Data for Free
Let's explore BigQuery using Google's free Public Datasets and build a partitioned table!
Step 1: Open BigQuery Studio in the Web Console
- In the Google Cloud Web Console, search for BigQuery in the top search bar.
- Click BigQuery Studio.
- You will see the SQL Query Editor on the right and the Explorer panel on the left.
Step 2: Query 100+ Million Records in Google Public Datasets
Google hosts petabytes of public data (Wikipedia edits, NYC Taxi trips, GitHub commits, US Census) that anyone can query for free.
In the BigQuery Query Editor, paste this query to find the most popular baby names in the United States over the last 100+ years (scanning millions of rows):
SELECT
name,
gender,
SUM(number) AS total_babies
FROM
`bigquery-public-data.usa_names.usa_1910_current`
GROUP BY
name, gender
ORDER BY
total_babies DESC
LIMIT 10;
Notice the Magic:
- Look at the top right of the editor: BigQuery shows: "This query will process ~24 MB when run."
- Click Run.
- In under 1.5 seconds, BigQuery scans millions of records and returns the all-time top names (e.g., James, John, Robert, Mary)!
Step 3: Cloud Shell CLI β Dry Runs & Cost Estimation
You can interact with BigQuery using the bq CLI tool. The most important command every cloud engineer must know is --dry_run (which checks syntax and calculates query data size without costing a single cent).
Open Google Cloud Shell and run:
# Estimate the bytes scanned WITHOUT executing the query ($0.00 check)
bq query \
--use_legacy_sql=false \
--dry_run \
'SELECT name, SUM(number) FROM `bigquery-public-data.usa_names.usa_1910_current` GROUP BY name ORDER BY 2 DESC LIMIT 5;'
Expected Output:
Query successfully validated. Assuming the tables are not modified,
running this query will process 23979402 bytes (22.87 MB).
Step 4: Create a Partitioned & Clustered Table
Let's create our own dataset and a partitioned table to see how date-based pruning works:
1. Create a BigQuery Dataset
bq --location=us-central1 mk -d ecommerce_analytics
2. Create a Partitioned Table with SQL DDL
Run this query to create a table partitioned by transaction_date and clustered by country:
bq query --use_legacy_sql=false '
CREATE OR REPLACE TABLE `ecommerce_analytics.sales_records` (
transaction_id STRING,
customer_name STRING,
country STRING,
amount FLOAT64,
transaction_date DATE
)
PARTITION BY transaction_date
CLUSTER BY country;
'
3. Insert Sample Records Across Multiple Dates
bq query --use_legacy_sql=false '
INSERT INTO `ecommerce_analytics.sales_records` (transaction_id, customer_name, country, amount, transaction_date)
VALUES
("TX101", "Alice", "US", 150.00, DATE "2026-08-25"),
("TX102", "Bob", "UK", 89.50, DATE "2026-08-25"),
("TX103", "Carol", "US", 320.00, DATE "2026-08-26"),
("TX104", "David", "IN", 45.00, DATE "2026-08-26");
'
4. Run a Partition-Pruned Query
bq query --use_legacy_sql=false '
SELECT
country,
SUM(amount) AS total_revenue
FROM
`ecommerce_analytics.sales_records`
WHERE
transaction_date = "2026-08-26"
GROUP BY
country;
'
(Because of WHERE transaction_date = '2026-08-26', BigQuery opens only today's partition and skips all previous dates!)
π Python SDK: Querying BigQuery in Code
Here is how Data Analysts and Data Engineers query BigQuery using Python:
Creating the Script in Cloud Shell
Terminal File Creation Options Choose either the 1-Click command or the manual editor:
β‘ Option A: Fast 1-Click Way (Copy & Paste):
cat << 'EOF' > query_bigquery.py
from google.cloud import bigquery
# 1. Initialize BigQuery Client
client = bigquery.Client()
# 2. Define analytical query
query = """
SELECT
country,
COUNT(transaction_id) as total_orders,
SUM(amount) as total_revenue
FROM `ecommerce_analytics.sales_records`
GROUP BY country
ORDER BY total_revenue DESC
"""
# 3. Execute and display results
query_job = client.query(query)
print("\n--- π BigQuery Analytics Report ---")
for row in query_job:
print(f"Country: {row['country']:4} | Orders: {row['total_orders']} | Revenue: ${row['total_revenue']:,.2f}")
EOF
π Option B: Manual Way with Nano:
nano query_bigquery.py
(Paste the code above, press Ctrl+O Enter to save, then Ctrl+X to exit).
Run the Script:
python3 query_bigquery.py
π‘οΈ Step 5: Credit Safety & Resource Teardown ($0.00 Guarantee)
BigQuery provides a generous free tier (10 GB storage free + 1 TB queries free per month). However, to keep your project completely clean:
Delete the Demo Dataset & Tables
bq rm -r -f ecommerce_analytics
(Output: Dataset 'ecommerce_analytics' successfully removed.)
π§ Daily Practice Drill & Knowledge Check
Test your understanding of BigQuery:
// Try answering these:
1. Why does adding LIMIT 10 to a query in BigQuery NOT reduce the total cost of the query?A) BigQuery only runs on Linux serversB) BigQuery uses Columnar Storage; it must read the entire requested column from disk before applying the LIMITC) BigQuery charges per row returned, not per byte scannedD) LIMIT is not supported in ANSI SQL
2. What is the primary architectural difference between Cloud SQL (MySQL/Postgres) and BigQuery?A) Cloud SQL is for OLTP (row-based transactional apps); BigQuery is for OLAP (columnar analytical data warehousing)B) Cloud SQL only runs in the US; BigQuery runs globallyC) Cloud SQL is serverless; BigQuery requires managing virtual machinesD) BigQuery cannot process SQL queries
3. How does table Partitioning help reduce query execution costs in BigQuery?A) It compresses images into ZIP filesB) It prunes (skips) partitions that don't match your WHERE date filter, scanning far fewer bytesC) It converts SQL queries into Python codeD) It deletes data after 24 hours automatically
4. How much query scanning does Google Cloud provide for FREE every month under the BigQuery sandbox/free tier?A) 100 MBB) 1 Terabyte (TB)C) 10 Gigabytes (GB)D) Zero; BigQuery has no free tier
π‘ Click for Solutions
- B (Columnar Storage reads full column blocks) β Because data is stored in columns, BigQuery scans the requested column across the entire dataset before returning the top 10 rows. Always filter columns explicitly (
SELECT colA, colB) instead of usingSELECT *. - A (OLTP vs OLAP) β Cloud SQL is built for fast single-row reads/writes (user logins, checkouts), while BigQuery is built for analytical aggregation across billions of rows.
- B (Partition Pruning) β By filtering on the partitioned column (e.g.,
WHERE transaction_date = '2026-08-26'), BigQuery skips scanning all other dates, slashing costs and latency. - B (1 Terabyte / month) β Google provides 1 TB of free query data processing and 10 GB of free active storage every month to every account.
π Day 27 Cheat Sheet Summary
Tomorrow, in Day 28, we level up our DevOps and security skills with Secret Manager & Cloud Build CI/CD: securely storing API keys and setting up automated GitHub deployment pipelines! π
β 26 - Hands-on Lab - Full-Stack App with DB & GCS | Next Topic β 28 - Secret Manager & Cloud Build CI-CD