Why Speed Matters
Most pipeline code is written against a sample: 1,000 rows pulled into memory, checked by eye, then shipped. The same code then meets the full table, perhaps 50 million rows, and a job that finished in 2 seconds is still running the next morning. The code is identical to what passed on the sample; the input grew, and the work the code does grew faster than the input did.
Counting steps instead of seconds
Growth is easier to reason about when you count a function's steps instead of timing it. Seconds depend on the laptop and on how busy the server is. Steps depend only on the code and the size of its input, which is called n. A step is a small, fixed piece of work: a comparison, an addition, a lookup by key.
The function below sums a list and also counts how many times its loop body runs. That counter is the step count.
def sum_rows(values):
total = 0
steps = 0
for value in values:
total = total + value
steps = steps + 1
print("rows:", len(values), "steps:", steps)
return total
sum_rows(list(range(10)))
sum_rows(list(range(20)))
sum_rows(list(range(40)))Output:
rows: 10 steps: 10 rows: 20 steps: 20 rows: 40 steps: 40
Every extra row added exactly 1 step. How the step count changes as n grows is the growth rate, and Big O notation is its shorthand. O(n), read "order n", means the steps grow in proportion to n.
Constant, linear, quadratic
Most code at this stage lands in one of 3 classes. Constant time, O(1), does the same work at any n. Linear time, O(n), does work in proportion to n. Quadratic time, O(n²), does work in proportion to n times n, which usually means each row gets compared against many other rows.
| rows (n) | O(1) steps | O(n) steps | O(n²) steps |
|---|---|---|---|
| 10 | 1 | 10 | 100 |
| 1,000 | 1 | 1,000 | 1,000,000 |
| 1,000,000 | 1 | 1,000,000 | 1,000,000,000,000 |
Turn steps into time with a round assumption: plain Python loops get through roughly 10 million simple steps per second. At a million rows the linear job needs about 0.1 seconds. The quadratic job needs 100,000 seconds, a little under 28 hours, on the same rows and the same machine.
In a pipeline, the stage with the worst growth rate sets the pace for everything after it.
- Raw events (source, 1,000,000 rows)
- Clean fields (transform, O(n))
- Drop duplicates (transform, O(n²))
- Add artist names (transform, O(n))
- Daily report (consumer, dashboard); freshness: ready by 06:00
- Raw events -> Clean fields
- Clean fields -> Drop duplicates
- Drop duplicates -> Add artist names
- Add artist names -> Daily report
A quicker way to sense a growth rate is to ask a single question of any code: what happens to the work when the input doubles?
- 1x
- 2x
- 4x
A deduplication script takes 2 seconds on 10,000 rows. Its work is quadratic. Roughly how long will it take on 100,000 rows?
- About 20 seconds, because 10 times the rows means 10 times the work
- About 200 seconds, because 10 times the rows means 100 times the work
- About 2 seconds, because the script does the same work at any size
- About 2,000 seconds, because 10 times the rows means 1,000 times the work
About this lesson
This section is part of the Complexity: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.