Doc blocks for reusable descriptions in dbt - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
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?"
Practice
doc blocks in dbt?Solution
Step 1: Understand what doc blocks do
Doc blocks let you write descriptions once and reuse them in multiple places.Step 2: Identify the main purpose
This saves time and keeps documentation consistent across your project.Final Answer:
To write descriptions once and reuse them across models -> Option AQuick Check:
Doc blocks = reusable descriptions [OK]
- Thinking doc blocks create tables
- Confusing doc blocks with SQL performance tools
- Using doc blocks to define variables
customer_description in dbt?Solution
Step 1: Recall the correct keyword for doc blocks
The correct keyword to define doc blocks isdocs:.Step 2: Match the syntax
docs: customer_description: "Description text here" usesdocs:followed by the doc block name and description, which is correct.Final Answer:
docs:\n customer_description: "Description text here" -> Option AQuick Check:
Define doc blocks with docs: [OK]
- Using 'doc:' instead of 'docs:'
- Adding extra underscores in keyword
- Misnaming the block keyword
docs: sales_desc: "This model contains sales data aggregated by month."What will be the output of this Jinja code in a model description?
{{ doc("sales_desc") }}Solution
Step 1: Understand what
This Jinja function inserts the text from the doc block named{{ doc("sales_desc") }}doessales_desc.Step 2: Match the doc block content
The doc blocksales_desccontains the description "This model contains sales data aggregated by month." so that text will be output.Final Answer:
This model contains sales data aggregated by month. -> Option DQuick Check:
doc("name") outputs doc block text [OK]
- Expecting the literal string instead of description
- Confusing doc block name with output
- Assuming an error if doc block exists
docs: product_info: "Details about product sales."But when you use
{{ doc("product_info") }} in your model, it shows an error. What is the likely cause?Solution
Step 1: Check common causes of doc block errors
One common cause is a mismatch between the doc block name and the name used indoc().Step 2: Verify usage rules
Doc blocks are defined in YAML files, not inside SQL files, and descriptions can have spaces. Runningdbt compileis standard but not usually the cause of this error.Final Answer:
The doc block name is misspelled in thedoc()call -> Option BQuick Check:
Name mismatch causes doc block errors [OK]
- Defining doc blocks inside SQL files
- Assuming spaces cause errors
- Skipping dbt compile step
Solution
Step 1: Define reusable description in docs block
Place the description underdocs:in a YAML file likeschema.ymlwith a name likecustomer_history.Step 2: Reuse description with doc() function
Use{{ doc("customer_history") }}in the description field of any model to reuse the text.Final Answer:
Define the doc block under docs: in schema.yml, then use {{ doc("customer_history") }} in model descriptions -> Option CQuick Check:
docs: + doc() = reusable descriptions [OK]
- Writing descriptions repeatedly in SQL files
- Using macros instead of doc blocks for descriptions
- Confusing ref() with doc()
