Quick revision
The short answer
I would measure first: look at the query plan, missing indexes and N+1 queries before changing the model. Copying the total into orders is usually safe if it is computed in the same transaction as the lines, because a placed order's total should not change. Copying customer_name depends on what the page should show: the name at order time is a snapshot that never changes, while the current name is a cache that needs an owner, an update path and a drift check, and a covering index, materialized view or read model might be better than either.
What to remember
In this answer, we evaluated the proposal in order: measure the query plan and fix missing indexes and N+1 queries first, then treat each copy separately. The total is safe when computed in the same transaction as the lines, the customer name is safe as a snapshot but needs an owner, update path and drift check if it must stay current, and covering indexes, materialized views or read models are alternatives that keep the main tables normalized.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would measure first: look at the query plan, missing indexes and N+1 queries before changing the model. Copying the total into orders is usually safe if it is computed in the same transaction as the lines, because a placed order's total should not change. Copying customer_name depends on what the page should show: the name at order time is a snapshot that never changes, while the current name is a cache that needs an owner, an update path and a drift check, and a covering index, materialized view or read model might be better than either.
Why this question is asked
Copying columns to avoid joins is the most common performance proposal in any team, and sometimes it is right. The risk is that it is chosen before anyone looks at why the query is slow, and that copies get added without anyone deciding what they mean. Interviewers ask this scenario to see whether we evaluate the proposal like an engineer: data first, then semantics, then the cost of keeping copies correct. In this answer, we will diagnose the slow page, evaluate each of the two copies separately, look at the alternatives, and explain how we would keep any copy honest.
1Measuring Before Changing the Model
5000 requests per second is a lot, but a list page that joins four tables can serve it if each request reads only the rows it shows. So the first step is EXPLAIN (ANALYZE, BUFFERS) on the real query with realistic parameters, and a look at the application logs for how many queries one page view issues.
- Plan shows scans or N+1?YesFix indexes and queries firstNoJoins are already efficient
- Still too slow at 5000 rps?YesConsider a copy or a read modelNoStop, no model change
- Snapshot or current value?YesWrite once at order timeNoNeeds owner, update path, drift check
3 steps in order. Play them, or step through them one at a time.
Here the plan shows the most common culprit: the join to order_lines has no index on order_id, so every page view scans millions of lines. Other usual findings are an ORM loading each order's customer and lines with separate queries (the N+1 problem, one query for the list and one more per row), or a sort that cannot use an index. Fixing those can take the page from seconds to a few milliseconds without changing a single column. Notice also that the addresses join may not be needed at all on a list page.
2Copying the Total: Usually Safe
An order's total is computed from its lines. After the order is placed, the lines do not change (a refund or amendment is a new document, not an edit), so the total is effectively a fact about the placed order. If we compute it in the same transaction that inserts the lines, the copy can never disagree with them.
The catch is any code path that edits lines after placement, such as an admin tool or a batch fix. If that exists, it must recompute the total in the same transaction, or we need a check constraint, trigger or reconciliation job to catch differences. A total stored on the order is also what an invoice should show, so many systems store it anyway.
3Copying the Customer Name: First Decide What It Means
This copy forces a product question: should the order list show the customer's name as it was when they ordered, or as it is today? For invoices and legal documents the answer is usually the name at order time, which makes it a snapshot: we write it once and never update it, exactly like the total. That is safe.
If the page should show the current name, the copy is a cache, and every rename must reach it. With a large customer that means updating thousands of order rows on each rename, inside the customer's transaction (slow, locks many rows) or asynchronously by event (fast, briefly stale). Either way, someone must own that update path and check for drift. A customer service tool usually wants the current name, so the honest answer is often: show the snapshot on the order and join to customers when the current name matters, or keep a separate read model.
4Alternatives That Avoid Copies in the Main Tables
Before adding columns to orders we can consider designs that keep the system of record clean.
| Option | Freshness | Write cost | Good when |
|---|---|---|---|
| Missing indexes and fixing N+1 | Always current | None | Always the first step |
| Covering index on orders | Always current | Slightly slower writes | Query reads few columns |
| Snapshot columns at order time | Fixed at order time | None after placement | Total, name on invoice |
| Materialized view, refreshed | Minutes old | Refresh job | Reporting-like lists |
| Read model in cache or search | Seconds old | Event consumer | Very high read volume |
A covering index includes the extra columns a query needs so the database can answer from the index alone. A materialized view stores the result of the joined query and is refreshed on a schedule (in PostgreSQL with REFRESH MATERIALIZED VIEW CONCURRENTLY so readers are not blocked). A read model is a separate store, shaped exactly like the page, updated from order and customer events. Each has a different freshness and cost, and the page's real requirement decides.
5Keeping Any Copy Honest
If we do add a cached copy, it needs three things written down: an owner (the team that fixes it when it is wrong), an update path (same transaction, trigger or event consumer) and a reconciliation check that compares copy and source on a schedule.
Notice that this check only makes sense for a cache. For a snapshot, differences are expected, which is one more reason to decide the semantics before writing the column.
6Follow-up questions
How would you detect drift between the copy and the source?
With a scheduled reconciliation query that joins the copy to its source and counts rows where they differ, using IS DISTINCT FROM so nulls compare correctly. For big tables I check a rolling window or a random sample each run, publish the count as a metric, and alert when it is above zero for longer than the agreed freshness delay. For copies maintained by events, I also monitor consumer lag, since drift often starts with a stuck consumer.
Would you use a database trigger to keep customer_name in sync?
Only if the copy must be current, the fan-out is small and the team is comfortable owning triggers. A trigger keeps the update in the same transaction, but a rename of a customer with 50,000 orders then updates 50,000 rows and locks them, and the logic is invisible to people reading the application code. An event consumer is usually the safer choice for a list page that can tolerate a few seconds of staleness.
What if the teammate's change is already in production?
Then I would classify each copied column as snapshot or cache, add the missing update path or drift check for the caches, and document the decision. Removing a column that works is less urgent than making sure the ones we keep are correct.
7Conclusion
In this answer, we evaluated the proposal in order: measure the query plan and fix missing indexes and N+1 queries first, then treat each copy separately. The total is safe when computed in the same transaction as the lines, the customer name is safe as a snapshot but needs an owner, update path and drift check if it must stay current, and covering indexes, materialized views or read models are alternatives that keep the main tables normalized.
Open with
I would not say yes or no straight away. First I would look at the actual query with EXPLAIN ANALYZE and at how many queries one page issues, because at 5000 requests per second a list page with four joins is usually slow for a fixable reason: a missing index on order_lines order_id, an N+1 pattern from the ORM, or a join to addresses the list does not even need.
Then, if they want more
If after that it is still too slow, I would evaluate the two copies separately. The total is usually safe: a placed order's total should not change, so I compute it in the same transaction that writes the lines, and many systems store it anyway for invoices.
I would just check that no admin tool edits lines afterwards without recomputing it. Customer name forces a product question. If we want the name at order time, which is right for invoices, it is a snapshot written once and never touched.
If we want the current name, it is a cache, and every rename has to reach thousands of orders through a trigger or an event, with an owner and a reconciliation query that counts mismatches and alerts.
Before committing to that, I would compare alternatives: a covering index, a materialized view refreshed every few minutes, or a read model in a cache or search index if the read volume really justifies it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An order list page joins orders, customers, addresses and order_lines to show the...": https://scrivio-interview-library.netlify.app/databases/data-modeling/7b94a45ed9/