How to Denormalize Data in Firestore for Better Performance
To denormalize data in Firestore, you duplicate related data across documents instead of using references. This reduces the number of reads and joins needed, improving performance by storing all needed data together in one place.
Syntax
Denormalization in Firestore means copying data fields from one document into another. This is done by writing the related data directly into the target document instead of storing only a reference.
Key parts:
docRef.set(data): Write data including duplicated fields.docRef.update(fields): Update duplicated fields when source data changes.- Manual syncing is needed to keep duplicated data consistent.
javascript
const userRef = firestore.collection('users').doc('user123'); const postRef = firestore.collection('posts').doc('post456'); // Denormalize user name into post document postRef.set({ title: 'My Post', content: 'Hello world', authorId: 'user123', authorName: 'Alice' // duplicated field });
Example
This example shows how to denormalize a user's name into a post document to avoid extra reads when displaying posts.
javascript
import { getFirestore, doc, setDoc, updateDoc } from 'firebase/firestore'; const firestore = getFirestore(); async function createPostWithDenormalizedUser() { const userRef = doc(firestore, 'users', 'user123'); const postRef = doc(firestore, 'posts', 'post456'); // Simulate fetching user data const userData = { name: 'Alice' }; // Create post with duplicated user name await setDoc(postRef, { title: 'My Post', content: 'Hello world', authorId: 'user123', authorName: userData.name // denormalized field }); console.log('Post created with denormalized user name'); } createPostWithDenormalizedUser();
Output
Post created with denormalized user name
Common Pitfalls
Denormalization requires manual updates to keep duplicated data consistent. Common mistakes include:
- Not updating duplicated fields when the original data changes, causing stale data.
- Duplicating too much data, increasing write costs and complexity.
- Ignoring Firestore transaction limits when updating multiple documents.
Always plan how to sync duplicated data, for example using Cloud Functions triggered on updates.
javascript
/* Wrong: Not updating duplicated data */ await setDoc(postRef, { authorName: 'Alice' }); // Later user changes name to 'Alicia' but post is not updated /* Right: Update duplicated field when user changes name */ await updateDoc(postRef, { authorName: 'Alicia' });
Quick Reference
- Denormalize by copying needed fields into related documents.
- Update duplicated data manually or with automation.
- Use denormalization to reduce reads and improve query speed.
- Balance duplication with write costs and data consistency.
Key Takeaways
Denormalize by duplicating related data fields into documents to reduce reads.
Manually update duplicated data to keep it consistent across documents.
Use denormalization to improve Firestore query performance by avoiding joins.
Avoid excessive duplication to control write costs and complexity.
Automate updates with Cloud Functions to maintain data integrity.