非正規化とデータ複製の戦略
データを意図的に複製し、JOINよりも非正規化したデータ構造を選び、書き込み時にコピーの整合性を保つことで、読み取りの速いRealtime Databaseデータをモデル化します。
「非正規化とデータ複製の戦略」はCoddyKit上の無料Firebase Auth & Realtime Database Appsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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
よくある質問
「非正規化とデータ複製の戦略」レッスンは無料ですか?
はい。「非正規化とデータ複製の戦略」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Firebase Auth & Realtime Database Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Firebase Auth & Realtime Database Appsコースには全4レッスンが含まれています。
「非正規化とデータ複製の戦略」で何を学びますか?
データを意図的に複製し、JOINよりも非正規化したデータ構造を選び、書き込み時にコピーの整合性を保つことで、読み取りの速いRealtime Databaseデータをモデル化します。 ブラウザで直接実行するハンズオンコードでFirebase Auth & Realtime Database Appsを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ファンアウトによるデータ更新
- トランザクションによるデータ操作
- アトミックカウンターとキュー
- 非正規化とデータ複製の戦略