Post's publication: Save ids and retrieve info querying the database, or directly saving that infos together with the id?

StackOverflow https://stackoverflow.com/questions/22302728

Domanda

I am creating a network that allows users to insert new posts. Together with the post's content A i need to save the place B in which the post was written. I am just wondering, should i save just the id C of the place B, and so querying the database looking for a place B with id C and retrieve data D (for example name, coordinates, etc.) from there each time i have to return the post A, or is better to store the data D in the post A, and not only the Id C, when the post is uploaded, so that i don't have to query the database each time i need to return a post?

Assuming i can easily get a place by his id, with a query which is totally scalable, and i want the best balance between memory usage and CPU usage.

Note also that the data D i'd retrieve using place's id are not a few, like all the names of the place in several languages. This is why i am not sure of storing so much data for each post, if not the answer would be obvious.

Thank you very much

È stato utile?

Soluzione

It depends on your queries and the change frequency of D. For example, if none of the data D is ever displayed in a list view, denormalization probably doesn't make sense in the first place. If the data D is modified very often, keeping the data in sync will be tedious and expensive, so again, that would indicate de-normalization is not your best option here.

If you want to show a list of posts and some details about the location, your join/subquery becomes a bit more complex, involving a $in. Still, those queries are relatively simple and usually very fast because you're hitting the _id index. In this case, I'd probably go for the normalized version where your posts only contain the id of the place.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top