Home > Back-end >  Multiple queries for childs or one big during initialization?
Multiple queries for childs or one big during initialization?

Time:09-28

I am setting up my Firebase Realtime Database with Google App Script and would like to have opinions/known facts on best practices for calling data. Multiple small queries (for children) or one big one (getting one big object) during initialization?

So far my data was stored on a google spreadsheet, it is not particularly big (that is why I am in doubt actually). Let's say a year from now it will have the equivalent of 600 rows and 60 columns of data.

I read that in Firebase it is better to replicate data x times to create short flat paths -ready to use for functions- rather than one big tree with too many nodes, and that makes sense.

But technically, my question is: is it better to call once all the whole database data during initialization and then use its child inside functions (var dataForFunction = database.node1.nodeA.dataX)? Or is it better to make new firebase queries each time a function runs to be returned only relevant data? Obviously making one query per function would mean making a lot of little queries anyway, and that is my doubt.

CodePudding user response:

Multiple small queries (for children) or one big one (getting one big object) during initialization?

As long as you don't use all the data that exists in that one big object, then performing separate queries will be the solution to go ahead with.

I read that in Firebase it is better to replicate data x times to create short flat paths -ready to use for functions- rather than one big tree with too many nodes, and that makes sense.

So you're referring to the Realtime Database. This database is composed of nodes, while Cloud Firestore is composed of collections and documents. I think it is worth taking it also into consideration. So creating a flat database structure is a practice that doesn't apply only to the Realtime Database, but in general to NoSQL databases. It's a widely used technique, that allows us to read (download) only the data that we need, and nothing more.

my question is: is it better to call once all the whole database data during initialization and then use its child inside functions (var dataForFunction = database.node1.nodeA.dataX) ? Or is it better to make new firebase queries each time a function runs to be returned only relevant data?

Imagine someone opens your web app. You download all data while initialization, so it can be ready to use across your entire application, and suddenly the user closes the app. So you end up spending a lot of resources with no benefits whatsoever. So the best practice is to load only the data that you want to display. Always load the data progressively. That's it.

  • Related