Blog

A $100K-Class Missing-Index Post-Mortem

small query, expensive failure chain
Financial chart plunging into red on a monitor with somber reflective lighting

A missing index rarely looks dramatic in code review. It is one absent migration beside a query that returns quickly on a developer database.

Under production cardinality, that omission can saturate a connection pool, delay callbacks, and create a six-figure class of operational damage. The useful lesson is not the invented precision of a loss number. It is the failure chain that makes a small SQL mistake expensive.

The Query That Looks Harmless

Imagine a reconciliation dashboard for operations. Its query looks harmless.

select *
from transactions
where merchant_id = ?
  and status = 'PENDING'
order by created_at desc
limit 100;

Staging had small data. Production had years. No composite index for merchant_id, status, created_at.

Under load, database scanned too much, connection pool filled, API latency spiked, payment callbacks delayed, support queue exploded.

Where The Cost Comes From

There is rarely one clean invoice. Cost arrives as support work, partner penalties, delayed settlements, emergency engineering, and lost customer trust. That is why “$100K-class” is a severity label here, not a claim about one undocumented incident.

Nobody cared that query was readable.

What We Missed

We tested controller. We mocked repository. We did not run query against production-like volume. We did not inspect query plan in review. Alert on database saturation was too slow.

Classic chain: one small technical miss plus weak safety net.

Proper Test

@Test
void pendingTransactionsQueryUsesExpectedIndex() {
    jdbc.execute("explain analyze select * from transactions where merchant_id = 'm1' and status = 'PENDING' order by created_at desc limit 100");
    // In real project assert plan shape or keep this as reviewed migration test output.
}

Better: migration checklist requires EXPLAIN for new query on large table.

Architectural Lesson

Operational dashboard should not run heavy ad hoc queries on primary payment database during peak time.

Use read replica, projection table, async materialization, or at least strict indexes and rate limits.

The Review Failure

The author may have omitted the index, but the delivery system still had several chances to catch it. Review did not ask for a query plan. Tests were too mocked. Metrics were late. Staging data had no useful scale.

Make The Cheap Check Early

TDD is not only unit tests. Architecture safety includes realistic data, integration tests, query plans, observability, and rollback.

Small oversight becomes expensive when system has no guardrails. Build guardrails before invoice arrives.