📖 Scenario: You are working on a data project using dbt to transform raw sales data into useful reports. Your project has multiple models that depend on each other. Understanding how to define these dependencies and run models in parallel will help you save time and avoid errors.
🎯 Goal: Build a simple dbt project with three models where one model depends on the other two. Learn how to define dependencies using ref() and understand how dbt runs models in parallel when possible.
📋 What You'll Learn
Create three dbt models: stg_sales.sql, stg_customers.sql, and fct_orders.sql
Use ref() to define dependencies in fct_orders.sql
Configure dbt to run models in parallel
Print the order in which models run to understand dependencies and parallelism
💡 Why This Matters
🌍 Real World
In real data projects, defining model dependencies ensures data is transformed in the correct order. Running models in parallel speeds up the workflow, saving time and computing resources.
💼 Career
Data engineers and analysts use dbt to build reliable data pipelines. Understanding dependencies and parallelism is key to optimizing data workflows and delivering timely insights.
Progress0 / 4 steps
1
Create initial dbt models
Create two dbt models named stg_sales.sql and stg_customers.sql. Each model should select all columns from their respective raw tables: raw_sales and raw_customers. Write the SQL code for both models.
dbt
Hint
Use simple select * from statements for each model.
2
Create dependent model with ref()
Create a dbt model named fct_orders.sql that depends on stg_sales and stg_customers. Use ref('stg_sales') and ref('stg_customers') to join these two models on customer_id. Select all columns from stg_sales and the customer_name from stg_customers.
dbt
Hint
Use {{ ref('model_name') }} to refer to other models inside your SQL.
3
Configure dbt to run models in parallel
Create a dbt_project.yml file and set threads to 4 to enable parallel model runs. This allows dbt to run independent models at the same time.
dbt
Hint
Set threads: 4 at the root level in dbt_project.yml.
4
Run dbt models and observe execution order
Run the command dbt run --models stg_sales stg_customers fct_orders in your terminal. Observe and print the order in which models run. Write a print statement that shows the models run in this order: stg_sales and stg_customers run first (in parallel), then fct_orders runs last.
dbt
Hint
Use a simple print() statement to show the order.
Practice
(1/5)
1. What does the {{ ref('model_name') }} function do in dbt models?
easy
A. It sets the number of threads for execution.
B. It creates a dependency on another model, ensuring it runs first.
C. It runs the model in parallel with others.
D. It deletes the referenced model from the project.
Solution
Step 1: Understand the purpose of {{ ref() }}
The {{ ref('model_name') }} function tells dbt that the current model depends on another model named 'model_name'.
Step 2: Recognize dependency effect
This means dbt will run the referenced model first before running the current one, ensuring correct order.
Final Answer:
It creates a dependency on another model, ensuring it runs first. -> Option B
Quick Check:
ref() sets dependencies = C [OK]
Hint: Remember: ref() links models to run in order [OK]
Common Mistakes:
Thinking ref() controls parallelism
Confusing ref() with thread settings
Assuming ref() deletes models
2. Which command line option controls how many models dbt runs at the same time?
easy
A. --models
B. --exclude
C. --full-refresh
D. --threads
Solution
Step 1: Identify parallelism control option
The --threads option tells dbt how many models it can run simultaneously.
Step 2: Differentiate from other options
Other options like --models select which models to run, but do not control parallelism.
Final Answer:
--threads -> Option D
Quick Check:
Parallelism = --threads [OK]
Hint: Threads set parallel runs count in dbt [OK]
Common Mistakes:
Confusing --models with parallelism control
Using --full-refresh to control threads
Thinking --exclude affects parallelism
3. Given these models and dependencies:
model_a.sql (no dependencies)
model_b.sql (depends on model_a)
model_c.sql (depends on model_a)
model_d.sql (depends on model_b and model_c)
If you run dbt run --threads 2, which models can run in parallel first?
medium
A. model_a only
B. model_b and model_c
C. model_a and model_b
D. model_c and model_d
Solution
Step 1: Identify models without dependencies
Only model_a has no dependencies, so it must run first.
Step 2: Understand parallelism with threads=2
Since only model_a is ready, it runs alone first. After it finishes, model_b and model_c can run in parallel.
Final Answer:
model_a only -> Option A
Quick Check:
First run models without dependencies = model_a [OK]
Hint: Run models without dependencies first, then parallelize [OK]
Common Mistakes:
Running dependent models before their dependencies
Assuming multiple models run before dependencies finish
Ignoring dependency order
4. You wrote this dbt model:
select * from {{ ref('model_x') }}
But when running, dbt errors saying model_x does not exist. What is the likely problem?
medium
A. You used --threads with too many threads.
B. The ref function is misspelled.
C. You forgot to create or run model_x before this model.
D. You need to add --full-refresh to run.
Solution
Step 1: Understand the error meaning
The error says model_x does not exist, meaning dbt cannot find it to build dependency.
Step 2: Check model creation and run order
This usually happens if model_x was not created or run before this model, so dependency is missing.
Final Answer:
You forgot to create or run model_x before this model. -> Option C
Quick Check:
Missing dependency model causes error = B [OK]
Hint: Ensure all referenced models exist and run first [OK]
Common Mistakes:
Blaming thread count for missing model error
Assuming ref() spelling error without checking
Thinking --full-refresh fixes missing model
5. You have 5 models with these dependencies:
model_1 (no dependencies)
model_2 (depends on model_1)
model_3 (depends on model_1)
model_4 (depends on model_2)
model_5 (depends on model_3)
If you run dbt run --threads 3, what is the minimum number of sequential steps dbt needs to complete all models?
hard
A. 3
B. 4
C. 5
D. 2
Solution
Step 1: Identify dependency layers
Layer 1: model_1 (no dependencies) Layer 2: model_2 and model_3 (depend on model_1) Layer 3: model_4 (depends on model_2) and model_5 (depends on model_3)
Step 2: Calculate steps with 3 threads
Step 1: Run model_1 alone Step 2: Run model_2 and model_3 in parallel (2 threads used) Step 3: Run model_4 and model_5 in parallel (2 threads used)
Final Answer:
3 -> Option A
Quick Check:
Dependency layers = 3 steps [OK]
Hint: Count dependency layers, parallelize within each layer [OK]
Common Mistakes:
Counting each model as separate step ignoring parallelism
Ignoring dependency order
Assuming all models run in one step
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.