Variables and control flow in dbt - Time & Space Complexity
When using variables and control flow in dbt, it's important to see how the steps grow as data or conditions increase.
We want to know how the number of operations changes when the input or conditions change.
Analyze the time complexity of the following dbt code snippet.
{% set data = [1, 2, 3, 4, 5] %}
{% set result = [] %}
{% for item in data %}
{% if item % 2 == 0 %}
{% do result.append(item * 2) %}
{% else %}
{% do result.append(item + 1) %}
{% endif %}
{% endfor %}
This code loops over a list, checks if each number is even or odd, and updates a new list accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over the list
dataonce. - How many times: Once for each item in the list.
As the list gets longer, the number of steps grows in a straight line with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and updates |
| 100 | 100 checks and updates |
| 1000 | 1000 checks and updates |
Pattern observation: Doubling the input doubles the work needed.
Time Complexity: O(n)
This means the time to run grows directly with the number of items in the list.
[X] Wrong: "The if-else inside the loop makes the time grow faster than the list size."
[OK] Correct: The if-else only adds a quick check per item, so it still grows linearly with the list size.
Understanding how variables and control flow affect time helps you write clear and efficient dbt models, a key skill in data projects.
"What if we nested another loop inside the for-loop? How would the time complexity change?"