Precomputing values: what it is and when to do it
Instead of recalculating an expensive result every time someone asks for it, calculate it when the data changes and store it. The average GPA of a million students, follower counts, leaderboards and dashboards, and the trade-offs that come with it.
In one sentence
Precomputing means doing the expensive work at write time (or on a schedule) and storing the result, so every read is a cheap lookup instead of a fresh calculation.
The example: the average GPA of 1M students
A dashboard shows the average GPA of all students. The naive way runs SELECT AVG(gpa) FROM students on every page view. With 1 million rows, every request reads a million rows, and if 50 people open the dashboard at once, that's 50 million rows read for the same number.
The precomputed way keeps a tiny gpa_stats row with count and sum. When a grade is written, it updates those two numbers. Reading the average is sum / count: one row.
-- on every new grade, in the same transaction
INSERT INTO grades (student_id, gpa) VALUES ($1, $2);
UPDATE gpa_stats SET count = count + 1, sum = sum + $2;
-- reading is constant time
SELECT sum / count AS avg_gpa FROM gpa_stats;Where you see it everywhere
| Feature | Precomputed as |
|---|---|
| Followers / likes / views count | A counter column updated on each event (or in batches) |
| Your social feed | Fan-out on write: a new post is pushed into followers' feed lists |
| Leaderboards | A Redis sorted set updated when scores change |
| Dashboards and reports | Materialized views or summary tables refreshed every few minutes |
| "Because you watched…" rows on Netflix | Recommendations computed offline in batch jobs |
| Search | An inverted index built when documents change |
Three ways to precompute
- On write, synchronously: update the aggregate in the same transaction. Always exact; makes writes a bit slower.
- On write, asynchronously: publish an event and let a worker update the aggregate. Fast writes; the value lags slightly.
- On a schedule (batch): recompute every N minutes or nightly (materialized view refresh, cron, Spark job). Simple for heavy aggregates; data is as old as the last run.
When it's worth it
Precompute when
- The result is read much more often than the data changes
- Calculating it is expensive (big scans, joins, ML)
- A slightly old value is acceptable, or you can update it exactly on write
Don't when
- The data changes far more than it's read
- Each user needs a different, unpredictable calculation
- The query is already fast with the right index
Precomputing moves cost from reads to writes and adds data you must keep consistent. In a read-heavy system that's usually a great deal.
Key takeaways
- 01Precomputing turns an expensive read into a cheap lookup by doing the work when data changes.
- 02Keep aggregates like count and sum, and adjust them on insert, update and delete.
- 03Choose synchronous, asynchronous or batch depending on how fresh the value must be.
- 04It pays off when reads vastly outnumber writes.
Check your understanding
1 of 3
What does precomputing mean?
Related concepts
Caching
A cache stores copies of data that are expensive to get (slow query, far away server) so the next request can be answered from somewhere fast and close.
9 min read →Query optimization & indexes
An index is a sorted copy of one or more columns that lets the database jump to the rows it needs instead of reading the whole table.
9 min read →Read-heavy vs write-heavy
A read-heavy part is read far more than it changes (a video page); a write-heavy part receives a constant flood of new data (view counts, analytics). Each one needs different tools.
7 min read →