Anon Li
Anon Li

Reputation: 611

Firestore document full of key/values or multiple documents - which is faster/better for speed?

If I'm trying to store key/value pairs of emails:phones, would it be better to have one document full of the emails:phones fields, or multiple documents, where the document.id would be the email and each document has one phone listed?

Example 1:

 collectionA -> documentA -> "email1": (number) 1234567890,  
                             "email2": (number) 1234567899,  
                             ...

Example 2:

 collectionA -> documentA -> "email1": (number) 1234567890,   
                documentB -> "email1": (number) 1234567899   
                ...   

what if I have a dataset of 100? 1000? multiple thousands?

I think Example 2 would be better because Example 1 requires loading the whole document before accessing the desired key/value, right?

Edit: this is based on me knowing the email but not knowing the phone number.

Upvotes: 0

Views: 25

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317352

It's only possible to determine which one is "better" if you examine specific use cases. Neither one is 100% is fully advantaged over the other.

Problems with #1:

  • Large volumes of entries will eventually fill up the max limit of a document (1MB), at which point writes will fail.
  • If you're only looking for one entry, it requires reading all of the entries, as it's not possible to read a partial document (on mobile clients only, server SDKs can do projections, however).
  • It's not possible to query by number without knowing the email.

Problems with #2:

  • Reading the entire set of N costs N document reads. For very large sets, this can get more expensive than needed, and would likely be slower than reading one document with everything.

You will have to pick which problems are less bad for your expected use cases.

Upvotes: 1

Related Questions