Doc blocks for reusable descriptions in dbt - Time & Space Complexity
We want to understand how the time to process reusable doc blocks grows as we add more descriptions.
How does the execution time change when using doc blocks in dbt?
Analyze the time complexity of the following dbt doc block usage.
-- docs/my_description.sql
{% docs user_description %}
This is a reusable description for the user model.
{% enddocs %}
-- models/user.sql
select * from raw.users
-- {{ doc('user_description') }}
This code defines a reusable description once and references it in a model.
Look for repeated actions when dbt processes doc blocks.
- Primary operation: Replacing each doc block reference with its stored description.
- How many times: Once per reference in the models during compilation.
As the number of doc block references grows, the time to replace them grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 replacements |
| 100 | 100 replacements |
| 1000 | 1000 replacements |
Pattern observation: More references mean more replacements, growing in a straight line.
Time Complexity: O(n)
This means the time to process doc blocks grows directly with the number of references.
[X] Wrong: "Using doc blocks makes compilation time constant no matter how many references there are."
[OK] Correct: Each reference still needs to be replaced, so more references mean more work.
Understanding how reusable components affect processing time helps you write efficient dbt projects and explain your reasoning clearly.
"What if we cached the doc block replacements so each description is replaced only once? How would the time complexity change?"