- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
- Data
BigQuery Query Cost and Performance Optimization
A query that returns the right answer can still scan far more data than it needs to. In this lab, you'll take a working BigQuery report query and start where most analysts stop: after it runs. You'll record its bytes processed, bytes billed, and slot time, then use the execution graph to work out where that work goes. Once you've found the cause, you'll rewrite the query to skip the unnecessary work, run it again, and verify within BigQuery that the report hasn't changed and the query now costs a fraction of what it did.
Lab Info
Table of Contents
-
Challenge
Run the baseline query and record its processing evidence
Establish what the existing finance report query costs before you change anything.
- Review the business requirement and the baseline query.
- Note the validator's bytes estimate before running.
- Run the baseline query with cached results turned off.
- Record bytes processed, bytes billed, and slot milliseconds.
- Save the query result as the
baseline_resulttable.
-
Challenge
Inspect the query plan and identify the main inefficiency
Use BigQuery's execution evidence to find out where the baseline query's work goes.
- Find the most expensive stages in the execution graph.
- Relate the input and aggregation stages back to the SQL.
- Check the table's clustering configuration and key uniqueness.
- Identify why the query reads every column and every block of the table.
-
Challenge
Change the query to remove that inefficiency
Rewrite the query so it reads only what the report needs.
- Replace the whole-row dedupe with only the required columns.
- Rewrite the month filter as a direct range on the clustered column.
- Compare the validator's new estimate with the baseline estimate.
-
Challenge
Run the optimized query against the same requirement
Measure what the optimized query actually costs.
- Run the optimized query with cached results turned off.
- Record bytes processed, bytes billed, and slot milliseconds.
- Compare the actual bytes processed with the validator's estimate.
- Save the query result as the
optimized_resulttable.
-
Challenge
Verify the result matches and the processing cost is lower
Prove that the optimized query returns the same report for less.
- Compare the two result tables in both directions.
- Retrieve both jobs' processing statistics from job history metadata.
- Save the side-by-side evidence as the
run_evidencetable.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.