Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the more performant option in Firestore, an array of objects or a subcollection?

I'm thinking about the best data model of my app using Firestore. I don't know which option is more performant/scalable? This one:

[Collection] Estimates -> 
    [Document] Estimate1 = items: [{name: 'Design', price: 200}, {name: 'Development', price: 200}]

Or this one:

[Collection] Estimates -> 
    [Document] Estimate1 ->
        [Collection] Items ->
            [Document] Item 1 = {name: 'Design', price: 200}
            [Document] Item 2 = {name: 'Development', price: 200}

On the other hand, other than perfomance issues, I have another consideration before I choose a data model. That is that I'm going to need to copy all contents of the 'estimate' node to an 'invoice' node and I don't know if I can do this in both data models.

like image 572
monjo Avatar asked Dec 10 '17 08:12

monjo


People also ask

How big can an array be in firestore?

The array [1, 2, 3] has elements equal to the first three elements of [1, 2, 3, 1] but is shorter in length. Up to 1,048,487 bytes (1 MiB - 89 bytes). Only the first 1,500 bytes are considered by queries.

What is Subcollection firestore?

A subcollection is a collection associated with a specific document. Note: You can query across subcollections with the same collection ID by using Collection Group Queries.

Is firestore fast?

Cloud Firestore also features richer, faster queries and scales further than the Realtime Database. Realtime Database is Firebase's original database. It's an efficient, low-latency solution for mobile apps that require synced states across clients in realtime.


1 Answers

This documentation will show you some of the limits of the FireStore Document model.

https://firebase.google.com/docs/firestore/quotas

In answer to your question(s):

It depends! It depends on how many items you will have in your estimate. A Document can hold 1 MB of data, so it is limited in scalability.

[Collection] Estimates -> 
    [Document] Estimate1 ->
        [Collection] Items ->
            [Document] Item 1 = {name: 'Design', price: 200}
            [Document] Item 2 = {name: 'Development', price: 200}

If you go with the above option, it will definitely be more scalable - you can have practically as many documents in the collection as anyone would ever want.

Both options will work, and you can copy the data to an invoice either way.

It may, however, be cheaper to just use one document, because firestore charges you based on each write and read. So for example, if you write all the data at the same time, in one write, to one document, that will be cheaper than writing all the data to several different documents, even if you use the WriteBatch to write them all at the same time.

Based on the model and guessing on what it is you are building, I would venture to guess each estimate won't have more than 100 items in it, so I would go with one doc for the estimate.

like image 146
Jeff Padgett Avatar answered Sep 27 '22 21:09

Jeff Padgett