0Pricing
Firebase Auth & Realtime Database Apps · บทเรียน

กลยุทธ์การทำข้อมูลไม่เป็นบรรทัดฐานและการทำซ้ำข้อมูล

ออกแบบข้อมูล Realtime Database ให้การอ่านรวดเร็วด้วยการทำข้อมูลซ้ำอย่างมีจุดประสงค์ เลือกโครงสร้างข้อมูลแบบไม่เป็นบรรทัดฐานแทนการเชื่อมตาราง และทำให้สำเนาสอดคล้องกันขณะเขียนข้อมูล

กลยุทธ์การทำข้อมูลไม่เป็นบรรทัดฐานและการทำซ้ำข้อมูล เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Firebase Auth & Realtime Database Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “กลยุทธ์การทำข้อมูลไม่เป็นบรรทัดฐานและการทำซ้ำข้อมูล”

ออกแบบข้อมูล Realtime Database ให้การอ่านรวดเร็วด้วยการทำข้อมูลซ้ำอย่างมีจุดประสงค์ เลือกโครงสร้างข้อมูลแบบไม่เป็นบรรทัดฐานแทนการเชื่อมตาราง และทำให้สำเนาสอดคล้องกันขณะเขียนข้อมูล คุณปฏิบัติ Firebase Auth & Realtime Database Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Firebase Auth & Realtime Database Apps หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Firebase Auth & Realtime Database Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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