Naming conventions at scale in dbt - Time & Space Complexity
When working with many dbt models, naming conventions help organize and find things quickly.
We want to understand how the effort to manage names grows as the number of models increases.
Analyze the time complexity of the following dbt naming convention check.
{% for model in graph.nodes.values() %}
{% if not model.name.startswith('stg_') and model.resource_type == 'model' %}
{{ exceptions.raise_compiler_error('Model name must start with stg_') }}
{% endif %}
{% endfor %}
This code loops over all models and checks if their names follow the 'stg_' prefix rule.
Look for repeated actions in the code.
- Primary operation: Looping over all models in the project.
- How many times: Once for each model, so as many times as there are models.
As the number of models grows, the checks grow too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of models.
Time Complexity: O(n)
This means the time to check naming grows in a straight line as you add more models.
[X] Wrong: "Checking names only once is enough, no matter how many models there are."
[OK] Correct: Each model needs its own check, so the total work grows with the number of models.
Understanding how checks scale helps you design dbt projects that stay manageable as they grow.
"What if we grouped models by folder and checked naming only once per folder? How would the time complexity change?"
Practice
stg_, int_, and fct_ in dbt projects?Solution
Step 1: Understand the purpose of prefixes in dbt
Prefixes likestg_(staging),int_(intermediate), andfct_(fact) indicate the role of each model.Step 2: Recognize how prefixes help in large projects
Using these prefixes organizes models logically, making it easier to find and maintain them.Final Answer:
They help organize models by their role, making the project easier to understand. -> Option BQuick Check:
Consistent prefixes = better organization [OK]
- Thinking prefixes affect performance
- Assuming prefixes create database indexes
- Believing prefixes skip tests
Solution
Step 1: Recall dbt naming style guidelines
dbt recommends lowercase letters and underscores to separate words for readability.Step 2: Evaluate each option
stg_customer_data uses lowercase and underscores, matching the recommended style.Final Answer:
stg_customer_data -> Option DQuick Check:
Lowercase + underscores = correct naming [OK]
- Using uppercase letters
- Using camelCase instead of underscores
- Starting with uppercase letters
stg_orders
int_customer_info
fct_sales_summary
stg_products
Solution
Step 1: Identify prefixes and their meanings
stg_means staging,int_means intermediate,fct_means fact table.Step 2: Match the prefix to the fact table
Onlyfct_sales_summarystarts withfct_, indicating a fact table.Final Answer:
fct_sales_summary -> Option CQuick Check:
Prefixfct_= fact table [OK]
- Confusing staging and fact prefixes
- Ignoring prefix meanings
- Choosing intermediate models as fact tables
fct_customer_orders but it contains only raw data loading logic without aggregations. What is the likely issue?Solution
Step 1: Understand prefix meanings
fct_is for fact tables, which usually contain aggregated or transactional data, not raw loads.Step 2: Match model content to prefix
Since the model only loads raw data,stg_(staging) prefix is more appropriate.Final Answer:
The model name prefixfct_is incorrect for raw data; it should bestg_. -> Option AQuick Check:
Raw data = stg_ prefix [OK]
- Assuming any prefix works for any model
- Thinking raw data belongs in fact tables
- Renaming without checking model logic
Solution
Step 1: Identify best practices for naming at scale
Consistent prefixes and lowercase with underscores improve clarity and readability.Step 2: Recognize importance of documentation
Documenting naming rules helps new team members understand conventions and dependencies quickly.Final Answer:
Use consistent prefixes likestg_,int_,fct_, keep names lowercase with underscores, and document the naming rules. -> Option AQuick Check:
Consistency + documentation = easier onboarding [OK]
- Ignoring documentation
- Using inconsistent naming styles
- Choosing unclear or numeric-only prefixes
Ready to prove it?
Pass the test and this pattern moves unknown
