A3.2.7 · Database design
A3.2.7 · 数据库设计
Evaluating the need for denormalizing databases
评价反规范化数据库的必要性
A live banking system and a weather-analytics dashboard both store data, but the right amount of normalization for one is close to wrong for the other, this statement is about weighing that decision, not just knowing the definitions.
实时银行系统和天气分析仪表盘都存储数据,但对其中一个合适的规范化程度,对另一个几乎是错的,这条陈述讲的是权衡这个决定,而不只是知道定义。
Explainer
讲解
A3.2.5 and A3.2.6 both pushed in one direction: normalize, remove redundancy, split tables apart. This statement asks the opposite question on purpose: when is a fully normalized design actually the WRONG choice? "Evaluate" means weighing both sides and reaching a judgement, not just listing pros and cons.
A3.2.5 和 A3.2.6 都朝同一个方向推进:规范化,消除冗余,把表拆开。这条陈述有意提出相反的问题:什么时候完全规范化的设计其实是错误的选择?"Evaluate(评价)"意味着权衡双方并得出判断,而不只是列出优缺点。
Denormalization is doing A3.2.5 and A3.2.6 in reverse, on purpose. The redundancy those statements taught you to remove gets deliberately reintroduced, not because someone made a mistake, but because a specific query pattern will run faster with the duplicate right there than it would having to fetch it fresh from another table every time.
反规范化(denormalization)是有意地把 A3.2.5 和 A3.2.6 倒过来做。那些陈述教你消除的冗余被刻意重新引入,不是因为谁犯了错,而是因为某种特定的查询模式,在重复的数据就在手边时,会比每次都得从另一张表重新取来要快。
The trade-off, both sides
权衡,两个方面
Real example: a banking system processing live transfers, every balance must always be exactly correct, right now, for every user checking it.
实际例子:一个处理实时转账的银行系统,每个用户查看时,每个余额都必须随时完全正确。
Advantages
优点
- Minimizes data redundancy (each fact stored once, less storage, no risk of two copies disagreeing)
- 把数据冗余降到最低 (每个事实只存一次,占用存储更少,也不会有两个副本互相矛盾的风险)
- Ensures data integrity (insert, update and delete rules stay consistent and enforceable)
- 确保数据完整性 (插入、更新和删除的规则保持一致,并且可以强制执行)
- Supports scalability (new data added without restructuring what already exists)
- 支持可扩展性 (加入新数据时,不必重构已有的内容)
Disadvantages
缺点
- More complex schema (many small tables and relationships are harder to understand and maintain)
- 模式更复杂 (许多小表和关系更难理解和维护)
- Slower reads (queries need joins across several tables, which costs performance)
- 读取更慢 (查询需要跨好几张表做连接,这会消耗性能)
Real example: a weather-analytics dashboard generating historical reports, the data was recorded once and never changes again, it only ever gets read.
实际例子:一个生成历史报表的天气分析仪表盘,数据只记录一次,之后不再改变,只会被读取。
Advantages
优点
- Improved query performance (a value already sitting in the row you need beats fetching it through a join, every time)
- 查询性能更好 (一个值已经就在你需要的行里,每次都胜过通过连接去取)
- Simplified data retrieval (the table's shape ends up matching what the application actually asks for, instead of forcing it to reassemble the answer from several places)
- 简化数据检索 (表的形状最终与应用程序实际所问的相匹配,而不是迫使它从好几个地方重新拼出答案)
- Enhanced scalability for reads (more read traffic doesn't mean more relationships to chase, because there are fewer of them to begin with)
- 增强读取方面的可扩展性 (读取流量增加,并不意味着要追踪更多关系,因为本来要追的就更少)
Disadvantages
缺点
- Data redundancy (the same fact stored more than once, at real storage cost)
- 数据冗余 (同一个事实被存了不止一次,带来真实的存储成本)
- Update risk (if an update misses one of the duplicate copies, the data quietly disagrees with itself)
- 更新风险 (如果某次更新漏掉了其中一个重复副本,数据就会悄悄地自相矛盾)
Where denormalization specifically pays off
反规范化具体在哪里能带来回报
Read-intensive situations
读取密集的情形
- Read-intensive applications: when a system spends nearly all its time being queried and almost none being written to, every join it can skip is time saved on every single request.
- 读取密集型应用:当一个系统几乎所有时间都在被查询、几乎没有写入时,它能跳过的每一次连接,都在每个请求上节省了时间。
- Reporting and analytics: a report that has to sweep across a large dataset already does a lot of work, dragging several tables together for every row of it just adds more.
- 报表与分析:一份必须扫描大型数据集的报表本来就做了很多工作,每一行都要把好几张表拼到一起,只会增加更多的工作。
- Data warehousing: once data has been pulled in from several source systems for analysis, keeping it pre-combined means a cross-source question doesn't have to redo that combining on every query.
- 数据仓库:数据从多个来源系统拉进来用于分析之后,把它预先合并好,意味着一个跨来源的问题不必在每次查询时重做一遍合并。
Finding the actual balance
找到真正的平衡点
Denormalization trades a straightforward, fast-to-write query structure against the real risk of redundant data drifting out of sync. Reaching a genuine evaluation, not just a guess, means working through this in order:
反规范化是在结构直接、写起来快的查询,与冗余数据失去同步的真实风险之间做的取舍。要得出真正的评价,而不只是猜测,需要按顺序做下面几步:
Work out what the system spends most of its time doing: answering read requests, or absorbing a steady stream of updates? Everything else in this process depends on getting this one honestly.
弄清系统大部分时间在做什么:是回答读取请求,还是应对源源不断的更新?这个过程中的其他一切,都取决于你对这一点是否如实判断。
Put the speed gain and the inconsistency risk on the same scale: does one genuinely outweigh the other for this specific system? The honest answer is rarely all-or-nothing, denormalizing just the handful of fields that actually get hammered by reads, while leaving the rest properly normalized, is often exactly right.
把速度的提升和不一致的风险放在同一个尺度上:对这个具体的系统来说,其中一个是否真的盖过了另一个?诚实的答案很少是非此即彼,只对真正被读取反复冲击的少数字段做反规范化,而让其余部分保持规范化,往往恰恰是对的。
Decide, before going ahead, how the risk from step 2 actually gets managed: what checks catch a value going out of sync, who is watching whether the performance win is holding up over time, and what the actual procedure is for updating every duplicate copy together instead of just one.
在动手之前,决定第 2 步中的风险究竟如何管理:用什么检查来发现某个值失去了同步,由谁来关注性能收益是否能长期保持,以及把所有重复的副本一起更新、而不是只更新一个的实际流程是什么。
Worked example
例题讲解
Same three questions, two systems, two different verdicts
同样的三个问题,两个系统,两种不同的结论
Both databases store real, useful data. Walking through the same balancing questions gives each one a genuinely different answer, that's what "evaluate" actually looks like in practice.
两个数据库存储的都是真实有用的数据。用同样的权衡问题走一遍,每个得到的答案却真正不同,这就是"评价"在实践中的样子。
Weather-analytics dashboard
天气分析仪表盘
Historical weather readings from hundreds of stations, joined against station and region details, to generate charts and reports. Data is recorded once and essentially never edited again.
来自数百个气象站的历史天气读数,与站点和地区的详细信息连接起来,生成图表和报表。数据只记录一次,之后基本不会再被编辑。
The performance win is large and the redundancy risk is genuinely low here, a clear case for a flattened, read-optimized design.
这里性能收益很大,而冗余风险确实很低,是采用铺平的、为读取优化的设计的明确例子。
Live ticket-booking system
实时订票系统
A theatre's seat-booking system, where every seat's availability must update instantly and correctly the moment it's booked or released, for every user viewing the seating map at once.
一家剧院的座位预订系统,其中每个座位的可用状态必须在被预订或释放的那一刻立即、正确地更新,让每一位同时查看座位图的用户都能看到。
The redundancy risk (double-booked seats) outweighs a performance gain the system doesn't actually need here.
冗余风险(座位被重复预订)盖过了一个系统其实并不需要的性能收益。
Check your understanding
检查你的理解
Which system is the stronger candidate for denormalization?
哪个系统是反规范化的更强候选?
Common mistake
常见错误
A3.2.7A3.2.7 的这一常见模式RecurringThe command term "evaluate" means weighing both sides and reaching a justified judgement. Candidates often answer with only one side, either listing denormalization's benefits with no mention of the redundancy risk, or the reverse, when the actual mark scheme rewards a genuine balance, tied to the specific scenario given.
反复出现指令词 "evaluate" 的意思是权衡双方并得出有依据的判断。考生常常只写一面,要么只列出反规范化的好处而不提冗余风险,要么反过来,而实际的评分标准奖励的是与所给具体情境相联系的真正平衡。
What a weak answer looks like: "Denormalizing the reporting database would make queries faster." True, but one-sided, no mention of the redundancy risk being weighed against that benefit, and no judgement reached.
较弱的答案是这样的:"对报表数据库做反规范化会让查询更快。"没错,但只说了一面,没有提到要拿冗余风险与这个好处相权衡,也没有得出判断。
What it should say: "Denormalizing would make the reporting queries faster by removing joins, and since this data is read-only once recorded, the usual redundancy risk barely applies here, so the performance benefit clearly outweighs the cost in this specific case." States both sides AND reaches a justified verdict.
应该这样写:"反规范化通过去掉连接,会让报表查询更快;而且这些数据一旦记录就是只读的,通常的冗余风险在这里几乎不适用,所以在这个具体情况下,性能收益明显盖过成本。"陈述了双方,并且得出了有依据的结论。
Revision questions
复习题
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
仿真题风格。先写出你的答案,再显示评分要点并给自己打分。
[2]The weather-analytics dashboard stores each station’s name alongside every reading. Identify two problems this repetition could cause.
[2]天气分析仪表盘把每个气象站的名称和每条读数存放在一起。指出这种重复可能造成的两个问题。
Marking points
评分要点
- Update anomalies: if a station is renamed, every one of its reading rows must be changed or the data disagrees.
- Wasted storage and slower writes from repeating the station name on every reading.
- 更新异常:如果某站点改名,它的每一条读数行都必须修改,否则数据自相矛盾。
- 在每条读数上重复站点名称会浪费存储并拖慢写入。
[3]Explain why the live banking system is kept fully normalized, referring to two specific characteristics of a normalized design.
[3]解释为什么实时银行系统要保持完全规范化,并联系规范化设计的两个具体特点。
Marking points
评分要点
- Balances and transfers change constantly, so an update anomaly from redundant data would produce a wrong balance.
- Normalization stores each fact once, so there is only one place to update and no risk of copies disagreeing.
- Enforced integrity through keys and constraints matters more here than any read-speed gain.
- 余额和转账不断变化,所以冗余数据引起的更新异常会产生错误的余额。
- 规范化让每个事实只存一次,因此只有一处需要更新,不会出现副本互相矛盾。
- 在这里,通过键和约束强制的完整性比任何读取速度提升都更重要。
[6]A national census database stores historical population records that are published once and never edited afterward, but are queried constantly for public statistics. Evaluate the need for denormalizing this database.
[6]一个全国人口普查数据库保存历史人口记录,这些记录一经发布就不再编辑,但会被公众统计查询不断访问。评价对这个数据库做反规范化的必要性。
Marking points
评分要点
- The data is written once and never edited, so the main danger of denormalization, update anomalies, barely applies.
- It is queried constantly for public statistics, so avoiding joins would make those reads faster and simpler.
- Denormalizing costs extra storage, but storage is cheap compared with the query load here.
- The historical records are effectively read-only, similar to a data warehouse, which is the classic case for denormalization.
- Judgement: denormalizing this database is justified; a fully normalized live system would not be.
- A middle path is to keep a normalized master and build denormalized reporting tables or materialized views from it.
- 数据只写一次、从不修改,所以反规范化的主要危险即更新异常几乎不适用。
- 它为公共统计而被不断查询,所以避免连接会让那些读取更快更简单。
- 反规范化要多花存储,但相比这里的查询负载,存储是便宜的。
- 这些历史记录实际上是只读的,类似数据仓库,而那正是反规范化的经典场景。
- 判断:对这个数据库反规范化是合理的;而一个完全规范化的在线系统则不会这样做。
- 折中方案是保留一个规范化的主库,并从中构建反规范化的报表表或物化视图。
Flashcards
闪卡
Active recall for this statement's key terms.
针对本条陈述关键术语的主动回忆。
Keep this statement's practice with you
把本条陈述的练习带在身边
Free account: unlock every revision question and flashcard for A3.2.7, and every other IB CS 2027 statement.
免费账号:解锁 A3.2.7 以及其他所有 IB CS 2027 陈述的全部复习题和闪卡。