r/ProgrammerHumor • • Mar 25 '20

Meme Its a win-win situation

Post image
55.1k Upvotes

529 comments sorted by

View all comments

Show parent comments

1

u/karmahorse1 Mar 26 '20 edited Mar 26 '20

I don't know his usage case, but generally speaking if you hear someone is storing stringified JSON data in SQL, theres a good chance they're doing something wrong.

If he's just storing small blobs of data to pass directly onto the client that's fine (though he should probably be encoding it first).

But if he's using it to represent a parent-child relationship, he needs to move that data out into a separate table and join on it instead, as storing data in JSON blobs takes up a lot more space and won't allow you to index on, or partially select any of the subfields.

If he doesn't want to put the data in a separate table as it doesn't have a strongly defined schema attached to it, then SQL probably isn't the right solution altogether.

As you say, Redis can be a good second option in the cases where you want to combine both object based and relational based storage formats. Even if it requires multiple queries, it'll still likely be faster than having SQL read large amounts of JSON off of disk.

1

u/Extract Mar 26 '20

But.. you do realize not all data that has a parent child relationship needs to be indexed, nor operated on in any way other than being displayed as additional data when a SINGLE object is queried?

Of course, parent-child relations are generally one-to-many tables, but very often a single row will have a lot of not-that-important data which only needs to be displayed when that object is queried directly, and never searched by (example - non-technical description of products).

Sure, indexed data deserves its own columns (and sometimes tables), but we were talking about data that could be placed either into MongoDB or a JSON string - relation data was never in this question to begin with.

And sure - JSON strings "waste" space, while blobs "waste" performance with encoding/decoding - however, if you're storing data in a JSON string/blob that's important enough for the space or lost performance to matter, you should've stored it in its own column/table to begin with.