Issue
So I am currently learning Firebase and NoSQL databases. I have a doubt about which to use when I have lots of referenced objects in other objects. I have come up with a solution to hold id
of the referenced object instead of holding the whole object and retrieving it by id
on demand but started wondering which way is more efficient and should be used.
First example is holding id
of an object:
"job": {
"id": 1,
"name": "name",
"orderId": 1
}
When using this method, whenever I get job
object, I also get order
object and attach it to the job.
Another solution is to hold whole object:
"job": {
"id": 1,
"name": "name",
"order": {
"id": 1,
"date": "2022-01-28"
}
}
In this way, there are no problems except that database has huge objects.
Which is better and more efficient to use? Or maybe there is a third way?
Solution
None of these solutions is better than another. You should use one, or the other according to your app’s use case. I’m not entirely sure if you are using Cloud Firestore or the Realtime Database, but you should always measure a structure versus the other. How can you do that? Simply by adding some dummy data and performing the needed queries. In this way, you’ll know which one is more expensive than the other.
Besides that, if you’re having a SQL background, duplicating data is normal with NoSQL databases. This technique is called denormalization. If you consider at some point in time to try using Cloud Firestore, then please check my answer from the following post:
Answered By – Alex Mamo
Answer Checked By – Mary Flores (BugsFixing Volunteer)