Firebase Auth & Realtime Database Apps · 课时

反规范化与数据复制策略

通过有意复制数据、选择反规范化的数据结构而不是连接查询,并在写入时保持副本一致,为 Realtime Database 建模以实现快速读取。

第 4 / 4 课13 个步骤

反规范化与数据复制策略 是 CoddyKit 上的免费 Firebase Auth & Realtime Database Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Firebase Auth & Realtime Database Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

NoSQL Thinking

Realtime Database has no joins. Instead of normalizing data like a relational schema, you shape data around how you read it. This often means storing the same value in more than one place.

This deliberate redundancy is called denormalization.

The Cost of Joins

In a normalized model, showing a post with its author name would require reading the post, then reading the user node separately for every post. That is many round-trips and slow lists.

Duplicating for Reads

Instead, copy the small bits you display alongside the post. Now one read renders the whole feed item.

{
  "posts": {
    "p1": {
      "text": "Hello",
      "authorId": "u9",
      "authorName": "Alice",
      "authorAvatar": "a9.png"
    }
  }
}

What to Duplicate

Duplicate only the fields you actually display in lists, not entire records.

  • Names, avatars, titles: good candidates
  • Large or sensitive fields: keep them in one place
  • Rarely-changing data: safest to copy

The Consistency Trade-Off

The cost of duplication is keeping copies in sync. If Alice renames herself, every copy of authorName must update. You trade write complexity for read speed.

Multi-Path Updates Keep Copies in Sync

Update all copies atomically with a single multi-path write so no copy is left stale.

import { getDatabase, ref, update } from 'firebase/database';

const updates = {};
updates['/users/u9/name'] = 'Alice B.';
updates['/posts/p1/authorName'] = 'Alice B.';
await update(ref(getDatabase()), updates);

Index Tables

Another denormalization pattern is the index node: a lookup mapping that lets you find related items without scanning. Here we map a user to their post IDs.

{
  "userPosts": {
    "u9": { "p1": true, "p7": true }
  }
}

Avoiding Deep Nesting

Reading a node downloads everything beneath it. Keep your tree shallow so a read does not pull in unrelated children. Split large nested structures into sibling top-level nodes.

When NOT to Denormalize

Denormalization is not always right. Avoid it when:

  • The duplicated field changes very frequently
  • There are many copies to keep consistent
  • The data is large or rarely read together

In those cases, store once and read separately.

Validating Duplicated Data

Use Security Rules .validate to keep duplicated fields trustworthy, for example ensuring an authorName copy is always a non-empty string.

{
  "posts": {
    "$id": {
      "authorName": { ".validate": "newData.isString() && newData.val().length > 0" }
    }
  }
}

Designing for Your Queries

The golden rule: structure data around your most common reads. Write the queries your app needs first, then shape (and duplicate) data so each one is a single, shallow read.

Quick Check

Test your understanding of denormalization.

Recap

You can now model NoSQL data for speed.

  • Denormalize by duplicating displayed fields
  • Keep copies in sync with multi-path updates
  • Use index nodes for relationships
  • Keep the tree shallow to avoid over-fetching
  • Structure data around your common queries
免费开始

用 AI 导师学习 Firebase Auth & Realtime Database Apps — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
11
课程
44

常见问题解答

「反规范化与数据复制策略」课时是免费的吗?

是的 — 「反规范化与数据复制策略」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Firebase Auth & Realtime Database Apps 课程的其余内容,请升级到 CoddyKit PRO。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。

「反规范化与数据复制策略」这节课中我会学到什么?

通过有意复制数据、选择反规范化的数据结构而不是连接查询,并在写入时保持副本一致,为 Realtime Database 建模以实现快速读取。 你通过在浏览器中直接运行的动手代码来练习 Firebase Auth & Realtime Database Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Firebase Auth & Realtime Database Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Firebase Auth & Realtime Database Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「反规范化与数据复制策略」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Firebase Auth & Realtime Database Apps 课中编写并运行代码吗?

能。每节 Firebase Auth & Realtime Database Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 扇出式数据更新
  2. 事务性数据操作
  3. 原子计数器与队列
  4. 反规范化与数据复制策略
← 返回 Firebase Auth & Realtime Database Apps