Normalization बनाम Denormalization
Normalizing duplicate data से बचता है जबकि denormalizing reads तेज़ बनाने के लिए कुछ data duplicate करता है।
In this page:
Normalization बनाम Denormalization
Normalized models हर fact एक जगह रखते हैं, जो updates को simple रखता है लेकिन joins चाहिए। Denormalized models data copy करते हैं ताकि एक single read सब कुछ return करे, जो reads तेज़ करता है लेकिन हर copy update करना ज़रूरी बनाता है।
MongoDB models आमतौर पर read-heavy parts denormalize करते हैं और अक्सर बदलने वाला data normalize करते हैं।
Note:
कम बदलने वाली fields denormalize करें, जैसे एक order line पर एक product name।
उदाहरण: Normalization vs denormalization
test> db.products.insertOne({ _id: "P1", name: "Lamp", price: 45 })
{ acknowledged: true, insertedId: 'P1' }
// Denormalised order line: copies the name and price at the time of purchase
test> db.orders.insertOne({ _id: 1, items: [{ productId: "P1", name: "Lamp", price: 45, qty: 2 }] })
{ acknowledged: true, insertedId: 1 }
test> db.products.updateOne({ _id: "P1" }, { $set: { price: 50 } })
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
test> db.orders.findOne({}, { items: 1, _id: 0 })
{ items: [ { productId: 'P1', name: 'Lamp', price: 45, qty: 2 } ] }
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- लगातार बदलने वाला data denormalize करना
- कभी कुछ denormalize न करना
- सारी copies update करना भूल जाना
चैप्टर सारांश
- Single-place updates के लिए Normalize करें
- Fast reads के लिए Denormalize करें
- Change frequency से balance करें
- ज़रूरत पड़ने पर हर copy update करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: