Reputation: 3228
I am planning to store a json_encoded
string on my database. I can't precisely tell the length its going to be, but I'm pretty sure it will be long. My concern is which field type I am going to use for this, is it blob
or text
?
I prefer the one where I can save space as much as possible over fast searching, in any case I have other column where I should just index.
Upvotes: 78
Views: 82224
Reputation: 1815
As stated in the documentation of MySQL, since 5.7.8 a native JSON data type is supported.
The JSON data type provides these advantages over storing JSON-format strings in a string column:
So, as the MySQL documentation states, the JSON data type should be used and not the text.
Upvotes: 73
Reputation: 60594
blob
is usually for things like images, binaries etc. text
should be good enough for your case, or you can use longtext
which has even bigger space capacity if that's really a concern.
Searching-wise, since you are storing json_encode
'd stuff, you'll still need to call json_decode
on it anyway for it to be useful in your application, I don't think choice of datatype matters in this case.
A better way is to normalize your database design instead of storing related stuff in one big string of json.
Upvotes: 51