Shift work off the read path
Some answers are costly to compute on demand, such as an aggregate over millions of rows. Precomputation does that work ahead of time and stores the result so a read is a simple lookup.
Forms it takes
- Materialized views store the result of a query and refresh on a schedule or on change.
- Denormalized fields keep a precomputed count or total next to the record.
- Rollups summarize raw events into hourly or daily buckets.
- Caches are a lightweight materialization of recent results.
The freshness tradeoff
Precomputed data can be stale until it is refreshed. You choose how to update it.
- On write keeps it fresh but adds cost to every write.
- On schedule is cheap but lags reality between refreshes.
- Incremental updates only the changed part, balancing cost and freshness.
When it pays
Precompute when reads vastly outnumber writes and the computation is heavy. If data changes faster than it is read, the refresh cost can outweigh the savings.
Key idea
Precompute and materialize heavy results so reads are cheap lookups, accepting some staleness chosen by how you refresh.