ref() function for model dependencies in dbt - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using dbt, the ref() function helps models depend on each other. Understanding how this affects execution time is important.
We want to know how the time to build models grows as the number of dependencies increases.
Analyze the time complexity of the following dbt model code using ref().
select *
from {{ ref('base_model') }}
where id in (
select id from {{ ref('dependent_model') }}
)
This code selects data from one model that depends on two other models using ref().
Look for repeated work in model building and data fetching.
- Primary operation: Running SQL queries for each referenced model.
- How many times: Once per model dependency, plus the current model.
As you add more models that depend on others, the total work grows.
| Number of Models (n) | Approx. Operations |
|---|---|
| 3 | 3 queries |
| 10 | 10 queries |
| 100 | 100 queries |
Pattern observation: Each model adds one query to run, so work grows linearly.
Time Complexity: O(n)
This means the time to build models grows directly with the number of models referenced.
[X] Wrong: "Using ref() does not add any extra time because it just points to models."
[OK] Correct: Each ref() causes dbt to run that model's SQL, so more refs mean more queries and more time.
Knowing how dependencies affect build time helps you design efficient data pipelines and explain your choices clearly.
What if we changed from many small models with ref() to one big model without dependencies? How would the time complexity change?
Practice
ref() function in dbt?Solution
Step 1: Understand the role of
Theref()ref()function is used to link one model to another in dbt, so dbt knows the order to run models and their dependencies.Step 2: Identify what
It does not write raw SQL, create users, or schedule runs. Its main role is linking models.ref()does not doFinal Answer:
To link models and define dependencies between them -> Option CQuick Check:
ref() links models = A [OK]
- Thinking ref() writes SQL code
- Confusing ref() with scheduling tools
- Assuming ref() manages database users
customers inside another model using ref()?Solution
Step 1: Recall dbt Jinja syntax for ref()
In dbt,ref()must be wrapped in double curly braces and the model name must be a string in quotes.Step 2: Check each option
select * from {{ ref('customers') }} uses{{ ref('customers') }}which is correct. Options B and C miss the curly braces or quotes. select * from {{ ref(customers) }} misses quotes around the model name.Final Answer:
select * from {{ ref('customers') }} -> Option AQuick Check:
Use {{ ref('model_name') }} syntax = A [OK]
- Omitting curly braces {{ }}
- Not putting model name in quotes
- Using ref() without Jinja syntax
orders model exists?select order_id, customer_id
from {{ ref('orders') }}Solution
Step 1: Understand what ref() compiles to
Theref()function compiles to the actual table name of the referenced model, usually just the model name like 'orders'.Step 2: Check the compiled SQL output
The compiled SQL replaces{{ ref('orders') }}withorders, so the output isselect order_id, customer_id from orders.Final Answer:
select order_id, customer_id from orders -> Option AQuick Check:
ref('orders') compiles to orders = C [OK]
- Leaving ref() uncompiled in SQL
- Adding extra schema prefix without config
- Using ref() as a string literal
select * from ref('sales')When you run dbt, you get an error. What is the problem?
Solution
Step 1: Check the syntax of ref() usage
In dbt,ref()must be wrapped in double curly braces to be interpreted as Jinja code.Step 2: Identify the error cause
The code usesref('sales')without{{ }}, so dbt treats it as plain text, causing an error.Final Answer:
Missing double curly braces around ref() -> Option DQuick Check:
Use {{ ref('model') }} not ref('model') alone = D [OK]
- Forgetting {{ }} around ref()
- Assuming ref() works without Jinja
- Removing quotes from model name
customers and orders. You want to create a new model customer_orders that joins these two. Which is the best way to use ref() to ensure correct dependencies and flexible naming?Solution
Step 1: Use ref() with correct Jinja syntax for both models
To link models and ensure dbt knows dependencies, use{{ ref('model_name') }}for bothcustomersandorders.Step 2: Avoid hardcoding table names or missing Jinja syntax
Options A and C hardcode names or use quotes incorrectly. select c.customer_id, o.order_id from ref('customers') c join ref('orders') o on c.customer_id = o.customer_id misses curly braces, so it won't compile.Final Answer:
select c.customer_id, o.order_id from {{ ref('customers') }} c join {{ ref('orders') }} o on c.customer_id = o.customer_id -> Option BQuick Check:
Use {{ ref('model') }} for all dependencies = B [OK]
- Hardcoding table names instead of using ref()
- Forgetting curly braces around ref()
- Using quotes incorrectly around model names
