Fine on test data, broken at scale
Recognise code that passes small tests but fails on production volumes: N+1 queries from ORM calls inside loops, unbounded queries with no pagination, reading whole files or tables into memory, quadratic list lookups in loops, and regexes prone to catastrophic backtracking. Explain why the agent cannot see this coming, and which check (query log, profile, large fixture) exposes it.
Builds on: What the agent cannot see
Green on three rows, down at 200,000
You asked the agent for an endpoint that lists orders with customer names. It came back in minutes with clean code, and the tests are green against a fixture of three orders. Two weeks after release, the page takes 40 seconds and database CPU sits at 100%. A nightly job that reuses the same loop gets killed for running out of memory. Nothing in the diff looked wrong. What did you miss?
Cost that only shows up with volume
Production data and its volumes are not in the agent's context. It writes the most direct code that does the job, and its only feedback is a tiny fixture. At that size, one query per row, a full table load or a list scan per item all finish in a few milliseconds. Green tests are a proxy signal: they show correctness at n=3, not cost at n=1,000,000. **Later cost:** timeouts, memory kills and database load that grow with your data.
Five tells and the check for each
- **ORM call or query inside a loop** (`o.customer.name`, `repo.find…` per item). This is N+1. *Later cost:* one extra query per row. *Check:* the query log; count queries per request.
- **No `LIMIT` or pagination** on a growing table. *Later cost:* every request loads more as the table grows. *Check:* the query log, plus the real row count.
- **Whole file or table in memory** (`f.read()`, `io.ReadAll`, `fetchall()`). *Later cost:* the process is killed once the data outgrows RAM. *Check:* a memory profile on a large fixture.
- **`in`/`includes`/`contains` on a list inside a loop.** This is quadratic. *Later cost:* 10× the data means about 100× the time. *Check:* a large fixture plus a profile.
- **Nested quantifiers** like `(a+)+`. *Later cost:* one input can hang a thread. *Check:* a long near-miss input with a timeout.
Two tells in one innocent loop
`Order.objects.all()` has no limit, so every order is loaded into memory on every request. `o.customer` is a foreign key fetched by lazy loading, which costs one extra query per order. Three fixture orders make 4 queries. 200,000 orders make 200,001. **BETTER:** `Order.objects.select_related("customer")` (one join) plus pagination. Pin the count with Django's `assertNumQueries` so it can't creep back.
// Python
--- a/shop/views.py
+++ b/shop/views.py
@@ -10,1 +10,7 @@ def order_list(request):
- return JsonResponse({"rows": []})
+ rows = []
+ for o in Order.objects.all():
+ rows.append({
+ "id": o.id,
+ "customer": o.customer.name,
+ })
+ return JsonResponse({"rows": rows})
A validator that hangs on one input
The agent adds `/^(\w+\s?)*$/` to check display names in TypeScript. Tests with `"Ada Lovelace"` pass instantly. **Tell:** a repeated group that can split the same text many ways, because `\s?` may match nothing. Thirty letters plus `!` give the backtracking engine about 2²⁹ splits to try. That blocks Node's event loop, the single thread that serves every request. **BETTER:** `/^(?:\w+\s)*\w*$/` accepts the same strings but matches each in only one way. Go's `regexp` and Rust's `regex` run in linear time; JS, Python and Java engines do not.
Ask: how many rows in production?
Small tests cannot show costs that grow with data, and the agent never sees your volumes. For every loop, query, file read or regex over real data, ask how it behaves at production size. Then prove it with the query log, a profile or a large fixture.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.