📖 Scenario: You work as a data analyst for an online store. Your team uses dbt to manage SQL queries that prepare sales data for reporting. Some queries run slowly, and you want to profile and optimize them to improve performance.
🎯 Goal: You will create a dbt model with sales data, add a configuration to limit data for faster testing, optimize the SQL query by filtering and aggregating efficiently, and finally run the model to see the optimized output.
📋 What You'll Learn
Create a dbt model SQL file with a SELECT query from the raw_sales table
Add a config variable limit_rows to limit rows for profiling
Optimize the query by filtering sales with amount greater than 100 and grouping by product
Print the final aggregated sales per product
💡 Why This Matters
🌍 Real World
Data analysts and engineers often need to profile and optimize SQL queries in dbt to improve data pipeline speed and efficiency.
💼 Career
Skills in query optimization and dbt modeling are valuable for roles like data analyst, data engineer, and analytics engineer.
Progress0 / 4 steps
1
Create initial dbt model with raw sales data
Create a dbt model SQL file named sales_data.sql with a SELECT query that selects order_id, product_id, and amount from the table raw_sales.
dbt
Hint
Use a simple SELECT statement to get the three columns from raw_sales.
2
Add a config variable to limit rows for profiling
Add a config variable named limit_rows and set it to 1000. Modify the SELECT query to limit the number of rows returned to limit_rows using a WHERE clause with row_number() over().
dbt
Hint
Use a subquery with row_number() over () to assign row numbers and filter with WHERE rn <= limit_rows.
3
Optimize query by filtering and grouping sales
Modify the query to select only sales where amount > 100. Group the results by product_id and calculate the total sales amount as total_amount using SUM(amount). Keep the row limiting logic intact.
dbt
Hint
Filter rows with amount > 100 inside the subquery, then group by product_id and sum the amounts.
4
Run the model and print the optimized sales output
Run the dbt model and print the resulting aggregated sales per product. Use print() to display the final query result as a table or list.
dbt
Hint
Print the header and some example rows to show the aggregated sales per product.
Practice
(1/5)
1.
What is the main purpose of query profiling in dbt?
easy
A. To write new SQL queries from scratch
B. To create visual dashboards
C. To find slow parts of your data queries
D. To backup your database automatically
Solution
Step 1: Understand the goal of query profiling
Query profiling is used to analyze how queries run and identify slow parts.
Step 2: Match the purpose with the options
Only To find slow parts of your data queries describes finding slow parts, which is the main goal.
Final Answer:
To find slow parts of your data queries -> Option C
Quick Check:
Query profiling = find slow parts [OK]
Hint: Profiling = finding slow query parts fast [OK]
Common Mistakes:
Confusing profiling with writing queries
Thinking profiling creates dashboards
Assuming profiling backs up data
2.
Which of the following is the correct way to run a dbt model named sales_summary from the command line?
easy
A. dbt build sales_summary
B. dbt run --model sales_summary
C. dbt execute sales_summary
D. dbt run model sales_summary
Solution
Step 1: Recall dbt command syntax for running models
The correct command uses 'dbt run --model <model_name>'.
Step 2: Compare options with correct syntax
Only dbt run --model sales_summary matches the correct syntax exactly.
Final Answer:
dbt run --model sales_summary -> Option B
Quick Check:
Run model = dbt run --model [OK]
Hint: Use 'dbt run --model' to run specific models [OK]
Common Mistakes:
Using 'dbt execute' which is invalid
Omitting the '--model' flag
Using wrong command order
3.
Given this dbt model SQL snippet:
select customer_id, count(*) as order_count from orders group by customer_id
Which database command helps you understand why this query might be slow?
medium
A. SELECT * FROM orders LIMIT 10
B. DESCRIBE orders
C. SHOW TABLES
D. EXPLAIN SELECT customer_id, count(*) as order_count from orders group by customer_id
Solution
Step 1: Identify the tool for query profiling
The EXPLAIN command shows how the database runs a query and where it may be slow.
Step 2: Match EXPLAIN with the given query
EXPLAIN SELECT customer_id, count(*) as order_count from orders group by customer_id uses EXPLAIN with the query, which helps understand performance.
Final Answer:
EXPLAIN SELECT customer_id, count(*) as order_count from orders group by customer_id -> Option D
Quick Check:
EXPLAIN = query profiling tool [OK]
Hint: Use EXPLAIN before query to profile it [OK]
Common Mistakes:
Using DESCRIBE which shows table structure only
Using SHOW TABLES which lists tables
Running SELECT without profiling
4.
Look at this dbt model SQL snippet:
select * from orders where order_date = '2023-01-01'
After profiling, you see a full table scan causing slowness. What is the best fix?
medium
A. Add an index on the order_date column
B. Remove the where clause
C. Select only order_id column
D. Change order_date to order_id in where clause
Solution
Step 1: Understand cause of full table scan
Full table scans happen when no index supports the filter condition.
Step 2: Choose optimization to speed up filtering
Adding an index on order_date helps the database find rows faster.
Final Answer:
Add an index on the order_date column -> Option A
Quick Check:
Index on filter column = faster queries [OK]
Hint: Index columns used in WHERE to avoid full scans [OK]
Common Mistakes:
Removing WHERE loses filtering purpose
Selecting fewer columns doesn't fix scan
Changing filter column arbitrarily
5.
You have a dbt model joining two large tables:
select c.customer_id, sum(o.amount) as total from customers c join orders o on c.customer_id = o.customer_id group by c.customer_id
Profiling shows the join is slow. Which optimization is best?
hard
A. Ensure both customer_id columns are indexed
B. Remove the GROUP BY clause
C. Replace join with a subquery
D. Add a LIMIT 10 to the query
Solution
Step 1: Identify cause of slow join
Joins on large tables are slow if join keys lack indexes.
Step 2: Apply indexing to join keys
Indexing customer_id on both tables speeds up join operations.
Final Answer:
Ensure both customer_id columns are indexed -> Option A
Quick Check:
Index join keys = faster joins [OK]
Hint: Index join keys to speed up large table joins [OK]
Common Mistakes:
Removing GROUP BY changes query meaning
Using subqueries may not improve speed
Adding LIMIT only reduces output, not join speed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.