Macros for reusable SQL logic in dbt - Time & Space Complexity
When using macros in dbt, we want to know how the time to run SQL changes as data grows.
We ask: How does reusing SQL logic with macros affect execution time?
Analyze the time complexity of this dbt macro usage.
{% macro filter_active_users() %}
WHERE status = 'active'
{% endmacro %}
SELECT *
FROM users
{{ filter_active_users() }}
This macro adds a reusable filter condition to SQL queries.
Look for repeated actions that affect runtime.
- Primary operation: The database scans the users table rows.
- How many times: Once per query execution, regardless of macro use.
Execution time depends on how many rows the database must check.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 row checks |
| 100 | 100 row checks |
| 1000 | 1000 row checks |
Pattern observation: Time grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the query time grows linearly with the number of rows in the table.
[X] Wrong: "Using macros makes the query run faster because it reuses code."
[OK] Correct: Macros only help write cleaner code; they don't change how many rows the database processes.
Understanding how macros affect query time shows you know the difference between code reuse and actual data processing cost.
"What if the macro included a join to another large table? How would the time complexity change?"
Practice
macros in dbt?Solution
Step 1: Understand what macros do
Macros are reusable pieces of SQL code that help avoid repetition.Step 2: Identify the main benefit
Using macros makes SQL cleaner and easier to maintain by reusing code.Final Answer:
To reuse SQL code and make it easier to maintain -> Option CQuick Check:
Macros = reusable SQL code [OK]
- Thinking macros store data
- Confusing macros with visualization tools
- Assuming macros run Python code
Solution
Step 1: Recall dbt macro syntax
dbt macros use Jinja templating with {% macro %} and {% endmacro %} tags.Step 2: Match syntax to options
{% macro my_macro() %} SELECT * FROM table {% endmacro %} correctly uses Jinja syntax for defining a macro.Final Answer:
{% macro my_macro() %} SELECT * FROM table {% endmacro %} -> Option AQuick Check:
dbt macros use {% macro %} ... {% endmacro %} [OK]
- Using SQL CREATE MACRO syntax (not dbt)
- Writing Python function syntax
- Using curly braces without Jinja tags
{% macro select_active_users() %}
SELECT * FROM users WHERE active = true
{% endmacro %}What will be the output SQL of this dbt model?
SELECT id, name FROM ({{ select_active_users() }})Solution
Step 1: Understand macro expansion
The macro returns the SQL: SELECT * FROM users WHERE active = true.Step 2: Substitute macro call in model SQL
The model SQL becomes: SELECT id, name FROM (SELECT * FROM users WHERE active = true)Final Answer:
SELECT id, name FROM (SELECT * FROM users WHERE active = true) -> Option DQuick Check:
Macro expands SQL inside parentheses [OK]
- Forgetting parentheses around macro SQL
- Thinking macro returns table name only
- Using macro call without {{ }}
{% macro get_orders() %}
SELECT * FROM orders
Solution
Step 1: Check macro syntax
Macros must start with {% macro %} and end with {% endmacro %} tags.Step 2: Identify incorrect closing tag
The macro incorrectly uses instead of {% endmacro %}.Final Answer:
Missing {% endmacro %} tag to close the macro -> Option BQuick Check:
Macro must end with {% endmacro %} [OK]
- Using HTML-like closing tags
- Thinking underscores are invalid
- Believing SELECT needs quotes in macros
Solution
Step 1: Understand macro parameters usage
Parameters must be inserted with {{ }} to inject SQL identifiers.Step 2: Check each option for correct syntax
{% macro count_distinct(table, column) %} SELECT COUNT(DISTINCT {{ column }}) FROM {{ table }} {% endmacro %} correctly uses {{ column }} and {{ table }} without quotes, so SQL identifiers are injected properly.Final Answer:
{% macro count_distinct(table, column) %} SELECT COUNT(DISTINCT {{ column }}) FROM {{ table }} {% endmacro %} -> Option AQuick Check:
Use {{ }} to insert parameters as SQL code [OK]
- Putting parameters inside quotes (makes them strings)
- Using $variable syntax (not Jinja)
- Not wrapping parameters with {{ }}
Ready to prove it?
Pass the test and this pattern moves unknown
