One model per source table rule in dbt - Time & Space Complexity
When using dbt, we often create one model for each source table. This helps keep things clear and organized.
We want to understand how the time to run these models grows as the number of source tables increases.
Analyze the time complexity of this dbt project structure.
-- models/customer.sql
select * from {{ source('sales', 'customer') }}
-- models/orders.sql
select * from {{ source('sales', 'orders') }}
-- models/products.sql
select * from {{ source('inventory', 'products') }}
-- models/payments.sql
select * from {{ source('finance', 'payments') }}
This project has one model for each source table, each simply selecting from its source.
Each model runs a query that reads one source table.
- Primary operation: Running a query on a source table.
- How many times: Once per source table (one model per source).
As the number of source tables grows, the number of models grows the same way.
| Number of Source Tables (n) | Number of Models Run |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The total work grows directly with the number of source tables.
Time Complexity: O(n)
This means the total time to run all models grows linearly with the number of source tables.
[X] Wrong: "Adding more source tables won't affect total run time much because each model runs independently."
[OK] Correct: Even if models run separately, the total time adds up because each model processes its source table once.
Understanding how your dbt project scales with more source tables shows you can plan for growth and keep your data pipeline efficient.
"What if we combined multiple source tables into fewer models? How would that change the time complexity?"
Practice
one model per source table rule in dbt?Solution
Step 1: Understand the rule and its benefits
The rule encourages creating one dbt model for each source table to keep things simple and clear. This approach helps with clarity, debugging, and teamwork by avoiding complex combined models.Final Answer:
To keep models simple and easy to understand -> Option DQuick Check:
One model per source table = simplicity [OK]
- Thinking it means combining tables into one model
- Believing it reduces the number of models
- Assuming it avoids using source tables
customers?Solution
Step 1: Match model name to source table and reference it
Following the rule, the model file should be named after the source table, herecustomers.sql. The model should select data from the source table to represent it clearly.Final Answer:
Create a model file namedcustomers.sqlwith a SELECT from the source table -> Option BQuick Check:
Model name = source table name + SELECT from source [OK]
- Naming model file unrelated to source table
- Not selecting from the source table
- Combining multiple tables in one model
orders:
select * from {{ source('sales_db', 'orders') }}
What will be the output if the source table has 100 rows?Solution
Step 1: Understand the SQL query and predict output
The query selects all columns and rows from the source tableorders. Since it selects all rows without filtering, the output will have the same 100 rows as the source.Final Answer:
The model will return 100 rows identical to the source table -> Option AQuick Check:
Select * from source = same rows [OK]
- Thinking it returns zero rows
- Assuming aggregation happens automatically
- Expecting syntax errors from correct source() usage
products:
select * from source('inventory', 'products')
But dbt throws an error. What is the problem?Solution
Step 1: Check dbt source function syntax and identify issue
The source function must be wrapped in double curly braces:{{ source(...) }}. The model usessource(...)without{{ }}, causing a syntax error.Final Answer:
Missing double curly braces around the source function -> Option CQuick Check:
dbt functions need {{ }} [OK]
- Forgetting {{ }} around source()
- Assuming select * is disallowed
- Ignoring model file naming conventions
customers and orders. According to the one model per source table rule, how should you organize your dbt models if you want to create a report showing total orders per customer?Solution
Step 1: Follow one model per source table for base models and build report model
Create one model forcustomersand one forordersseparately to keep base models simple. Create a third model that joins these base models to calculate total orders per customer, keeping transformations clear and modular.Final Answer:
Create separate base models forcustomersandorders, then create a third model joining them for the report -> Option AQuick Check:
Base models per source + separate report model [OK]
- Combining sources in one base model
- Skipping base models and joining sources directly
- Ignoring one source table
Ready to prove it?
Pass the test and this pattern moves unknown
