Creating your own dbt package - Performance & Efficiency
When creating your own dbt package, it's important to understand how the time to run your models grows as you add more models or data.
We want to know how the execution time changes when the package size or data size increases.
Analyze the time complexity of the following dbt package structure.
-- models/my_package/model_a.sql
select * from source_table;
-- models/my_package/model_b.sql
select * from {{ ref('model_a') }} where condition = 'value';
-- models/my_package/model_c.sql
select * from {{ ref('model_b') }} join another_table on key = key;
This package has three models where each model depends on the previous one, building on the data step by step.
Look at what repeats when dbt runs this package.
- Primary operation: Running each model's SQL query once in order.
- How many times: Once per model, so three times here.
As you add more models to your package, dbt runs each one in sequence.
| Input Size (models) | Approx. Operations (queries run) |
|---|---|
| 3 | 3 |
| 10 | 10 |
| 100 | 100 |
Pattern observation: The total work grows directly with the number of models you have.
Time Complexity: O(n)
This means the time to run your package grows linearly with the number of models you include.
[X] Wrong: "Adding more models won't affect run time much because dbt runs them fast."
[OK] Correct: Each model runs a query, so more models mean more queries and longer total run time.
Understanding how your dbt package scales helps you design efficient data workflows and shows you can think about performance as your projects grow.
"What if your models had multiple dependencies and some ran in parallel? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand the role of dbt packages
dbt packages are designed to bundle data models and logic for reuse.Step 2: Identify the main benefit
Sharing and reusing models saves time and keeps projects consistent.Final Answer:
To reuse and share data models easily across projects -> Option AQuick Check:
dbt package purpose = reuse/share models [OK]
- Confusing packages with dashboards
- Thinking packages store raw data
- Believing packages speed up SQL writing only
Solution
Step 1: Recall where packages are declared
dbt usespackages.ymlto list external or local packages.Step 2: Differentiate from other config files
dbt_project.ymlconfigures the project itself, not packages.Final Answer:
packages.yml -> Option DQuick Check:
Package list file = packages.yml [OK]
- Adding packages in dbt_project.yml
- Confusing profiles.yml with packages.yml
- Trying to add packages in SQL files
packages.yml snippet:packages: - local: ../my_dbt_package
What command installs this package into your project?
Solution
Step 1: Identify the command for package installation
dbt usesdbt depsto download and install packages listed inpackages.yml.Step 2: Differentiate from other commands
dbt runruns models,dbt installis not a valid command.Final Answer:
dbt deps -> Option CQuick Check:
Install packages with dbt deps [OK]
- Using dbt install (not a valid command)
- Confusing dbt run with package install
- Trying dbt build instead of dbt deps
dbt deps, you get an error: "Could not find package at path '../my_package'". What is the likely cause?Solution
Step 1: Understand the error message
"Could not find package at path" means dbt cannot locate the folder specified.Step 2: Check the path in packages.yml
Relative paths must be correct and folder must exist for local packages.Final Answer:
The relative path in packages.yml is incorrect or folder missing -> Option AQuick Check:
Path error = wrong folder path [OK]
- Running dbt run before dbt deps
- Editing dbt_project.yml instead of packages.yml
- Deleting unrelated config files
customer_summary from your custom package my_package. Which is the correct way to reference it in your SQL model?Solution
Step 1: Recall how to reference models from packages
Useref('package_name.model_name')to refer to models in packages.Step 2: Check each option's syntax
select * from {{ ref('my_package.customer_summary') }} uses the correct single-argumentrefsyntax with dot notation for packages.Final Answer:
select * from {{ ref('my_package.customer_summary') }} -> Option BQuick Check:
Package model ref = ref('package.model') [OK]
- Using ref('package', 'model') syntax (that's for sources)
- Forgetting the package name in ref
- Using double underscore instead of dot
Ready to prove it?
Pass the test and this pattern moves unknown
