1

In an SQL database, is there any benefit over storing display only values that you wont be searching by as a JSON object string instead of adding an extra column for each of the object's properties?

EX:

col1|col2|jsonString
--------------------
1   |1   |{prop1:A,prop2:B,prop3:C}

VS

col1|col2|prop1|prop2|prop3
---------------------------
1   |1   |A    |B    |C

Is one more efficient than the other in terms of memory used or anything else? If you could provide sources that would be really appreciated as well. Thanks.

2 Answers 2

3

One benefit of storing information in JSON when you don't intend to search is to avoid schema changes. For example, you have an application that allows a user to store their preferences. Those preferences can grow over time. Different users set different preferences, often leaving some of them as default or empty.

If you were to create a field for each preference, you would have to deal with schema changes whenever a preference has to be added/removed. If you had a JSON field that stored such preferences, you would have much more flexibility in adding/removing preferences.

If your application languages can consume JSON easily, it becomes really easy for the application to ask of preferences for UserID 1 and parse JSON.

Sign up to request clarification or add additional context in comments.

3 Comments

Downsides to storing as JSON are that 1) you will require more storage space because now you have to repeatedly store curly braces, colons, and the name of every property 2) it adds complexity to whatever is saving or displaying the JSON string, because now you have to build, parse, and extract the contents 3) if you ever DO need to search the properties in a query, it becomes slow, painful, and error-prone in SQL. It would have to be a pretty compelling reason for me to choose this option over storing the values separately in SQL.
Hi thanks for replying. How big of an issue are schema changes? How could that adversely affect our db?
Schema changes can be easy or hard, depending on the process implemented and size of databases. Adding a not null column with default value on a large table of millions of records could take time with record locking. If you want fewer touches to the schema, JSON helps.
2

zedfoxus answer is correct. I want to add that if you plan to store whole JSON documents, you might as well look into a non-relational database like MongoDB, that is designed for purposes like this. The advantages stated by zedfoxus will still be present and databases like MongoDB are optimized to store JSON documents.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.