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/AWildIndependent Mar 26 '20

Oh i see what you are saying.

I pity anyone who actually does that. Insanity lol

2

u/Extract Mar 26 '20

Oh, poor us - guess we'll be busy wiping our tears with all the cash typically generated by creating/maintaining actual scalable products rather than single-use garbage.

But for slightly more in-depth reply, for the few people reading this thread who are actual software engineers and might use this knowledge some day:

Often time, when designing a data schema, you'll come to a point where an object has many fields, that aren't meant to be searched by, just displayed when you view this specific object (e.g, some object-specific information), and sequential write consistency isn't very important - aka, you don't mind if two people writing at the same time overwrite each other (probably because this information is pretty unimportant, as well as not meant to be edited frequently).

When dealing with a scalable product, you should always be prepared for a number of such (logical) fields for each object (table) to grow:

A possible solution would be to add an unindexed column to each table, but not only will this linearly increase maintenance/extension complexity, this will also achieve nothing in case of fields that are themselves objects (or, may become objects in the future).

Another solution is to create links based on the key of each such object, correlating to an object-based DB (like MongoDB), and simply query that DB for all relevant data after you get the object from the SQL DB (for example, you get). In terms of performance, this might work, unless the server that makes those queries is sitting far from the MongoDB cluster (in which case, the transmission delay will more than overshadow the performance gains). Yet, in terms for maintenance/extension, overall code complexity, and even infrastructure maintenance complexity, the losses are in 99.99% of the cases not worth the performance gains.

The final, a most simple solution that also happens to be correct in the vast majority of the cases - add a TEXT (or in specific cases, even a big enough VARCHAR) column for JSON, and either parse it with the SQL native functions, or even with the server code that handles the data-related logic. Very simple, and can be easily scaled, extended, maintained, decent performance (for non-critical data) while suffering none of the drawbacks of having 2 DBs.

1

u/AWildIndependent Mar 26 '20

I still cannot tell what this would solve for someone outside of large websites that need sharding.

I cant even think of a reason you would need to store JSON if you just create a good schema. Of course, this is again for websites that dont require sharding.

I understand scalability is important, however at the same time using a NOSQL DB for something a SQL DB can handle just find with good table schema just seems like bad design, but Im talking from a point of not a ton of experience so im certain I am missing some caveats here

2

u/Extract Mar 26 '20

I cant even think of a reason you would need to store JSON if you just create a good schema.

Like I said, you can't think of a reason because there is no reason why you need to do it.
You only want to do this if you care about stuff like code complexity, maintenance and extensibility which would all suffer if you create a "good" schema with a column for each and every bit of information about a potential object - this becomes more and more true the more info there is on an object (and the bigger (or - higher quality) your business, the more info like this you'll usually provide).
Here is a random item from ebay. Try to estimate how many parameters (one-to-many tables like "Related Products" not included) this simple item has. Now imagine how inefficient it'd be to store every single one of those parameters in a column on its own (talking about those params that don't need to be indexed, especially those unique to this type of product).

1

u/AWildIndependent Mar 26 '20

The eBay example helped it click. Thanks man

1

u/Extract Mar 26 '20

np.
Dont stop learning, and definitely dont believe any Reddit advice unless you can understand it yourself (or determine it is bullshit, like in most cases).

1

u/nishinoran Mar 27 '20

Or just use JSONB if you're in Postgres (I believe MSSQL also had something similar).

1

u/Saladtoes Mar 26 '20

MSSQL supports JSON queries, so it actually works nicely. Only problem is that you cannot index the fields inside the JSON

1

u/Hollowplanet Mar 26 '20

Postgres supports it with indexing.

1

u/AWildIndependent Mar 26 '20 edited Mar 28 '20

Ok im still a little confused. Im only like a 2.5 year dev so lacking in experience here. Why would people store JSON in SQL databases instead of creating a proper schema? Other than massive websites that need sharding what does noSQL solve?

Edit: excellent responses under me

2

u/Saladtoes Mar 26 '20

In my case we have lots of different kinds of data we may store (events, sensor data, alarms, user comments, documents, files, reports... and the list is always growing) and it all relates to an asset inside the asset management platform. The grouping, indexing, and the real relational capabilities available in SQL server are fantastic for the kinds of data manipulation we need to do. At the same time, specific customer requirements change or we want to try a new concept on our platform (we have multiple customers paying for this as kind of a SaaS model). Instead of designing a new model, writing new backend code, exposing new endpoints, and redeploying everywhere, I often will use the JSON field to kind of slide in the new fields I need for some new report or application, and just extend an enumeration of a category field to index and query by. Then I can make a new view that utilizes the JSON field into a more proper looking model. I can test it out by just selecting that view as a JSON array and immediately have a new structured endpoint without deploying new code (besides the view obviously) - just populate the JSON field from the edge device and get to work on the UI (generally a web page or a desktop application). That’s just how I use it though. I love the simplicity and performance I get with have just a single managed database and a serverless application. For our scale it works wonderfully.

2

u/Hollowplanet Mar 28 '20

For the second question, people say its better because schema updates are more simple and storing unstructured data is possible. Someone posted a great writeup a few posts up. I've never used one myself. Some people like it. A lot of other people I've heard regret using it.