Organizing models in directories in dbt - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When organizing dbt models in directories, it's important to understand how this affects the time it takes to run your project.
We want to know how the number of models and their folder structure impact execution time.
Analyze the time complexity of this dbt project structure snippet.
models/
sales/
orders.sql
customers.sql
marketing/
campaigns.sql
leads.sql
finance/
revenue.sql
expenses.sql
This structure organizes models into folders by domain, each containing multiple SQL files representing models.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: dbt runs each model file once during compilation and execution.
- How many times: Once per model file, so total runs equal the number of model files.
As you add more model files in directories, the total execution time grows roughly in proportion to the number of models.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 models | 10 executions |
| 100 models | 100 executions |
| 1000 models | 1000 executions |
Pattern observation: The time grows linearly as you add more models, regardless of how they are grouped in directories.
Time Complexity: O(n)
This means the total time to run your dbt models grows directly with the number of models you have.
[X] Wrong: "Organizing models into many folders will make dbt run faster because it processes folders separately."
[OK] Correct: dbt runs each model file individually regardless of folder structure, so folders do not reduce total execution time.
Understanding how project structure affects execution helps you design scalable dbt projects and communicate clearly about performance.
What if we added model dependencies that require models to run in sequence? How would that affect the time complexity?
Practice
Solution
Step 1: Understand project organization benefits
Organizing files into folders helps keep things tidy and easy to find.Step 2: Relate to dbt model management
In dbt, directories group models logically, making the project easier to manage.Final Answer:
It keeps the project clean and easier to manage. -> Option AQuick Check:
Organizing models = easier management [OK]
- Thinking folders speed up dbt runs
- Believing folders fix SQL errors
- Assuming folders create dashboards automatically
Solution
Step 1: Understand dbt model referencing
dbt uses dot notation to reference models in subfolders: folder.model_name.Step 2: Check each option
Only SELECT * FROM subfolder.model_name uses correct dot notation with folder before model name.Final Answer:
SELECT * FROM subfolder.model_name -> Option CQuick Check:
Use folder.model_name to reference subfolder models [OK]
- Reversing folder and model name order
- Using hyphens instead of dots
- Ignoring folder name when referencing
models/
sales/
orders.sql
marketing/
campaigns.sqlWhat is the correct way to select all columns from the orders model inside the sales folder?
Solution
Step 1: Identify model location
The orders model is inside the sales folder under models.Step 2: Use correct reference syntax
dbt references models using folder.model_name, so sales.orders is correct.Final Answer:
SELECT * FROM sales.orders -> Option BQuick Check:
Reference subfolder models as folder.model_name [OK]
- Omitting folder name when selecting
- Using full path with 'models.' prefix
- Selecting from wrong folder model
models/finance/revenue.sql. You wrote this SQL:SELECT * FROM revenue
Why might this cause an error when running dbt?
Solution
Step 1: Understand model referencing in subfolders
Models inside subfolders must be referenced with folder prefix, e.g., finance.revenue.Step 2: Identify cause of error
Using just revenue ignores the folder, causing dbt to not find the model.Final Answer:
Because the model should be referenced as finance.revenue. -> Option AQuick Check:
Reference subfolder models with folder prefix [OK]
- Assuming model name alone works in subfolders
- Thinking SQL syntax is wrong
- Believing dbt disallows subfolders
Solution
Step 1: Understand multi-level directory organization
Organizing by year then quarter creates clear hierarchy: year_2024/q1/.Step 2: Check referencing simplicity
dbt references nested folders with dot notation: year_2024.q1.sales is clear and logical.Final Answer:
models/year_2024/q1/sales.sql and reference as year_2024.q1.sales -> Option DQuick Check:
Use nested folders with dot notation references [OK]
- Reversing folder order causing confusing references
- Using flat filenames losing hierarchy clarity
- Ignoring dot notation for nested folders
