Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use blob or text for JSON in MySQL?

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.

like image 499
Leandro Garcia Avatar asked Apr 18 '12 02:04

Leandro Garcia


People also ask

Can we store JSON in BLOB MySQL?

Note that any database will accept JSON documents as a single string blob. However, MySQL and PostgreSQL support validated JSON data in real key/value pairs rather than a basic string.

What is the difference between TEXT and BLOB in MySQL?

What are the differences between the BLOB and TEXT datatypes in MySQL? BLOB stands for Binary Large Objects and as its name suggests, it can be used for storing binary data while TEXT is used for storing large number of strings.

Is it a good idea to use JSON in MySQL?

MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column: Automatic validation of JSON documents stored in JSON columns.


1 Answers

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:

  • Automatic validation of JSON documents stored in JSON columns. Invalid documents produce an error.
  • Optimized storage format. JSON documents stored in JSON columns are converted to an internal format that permits quick read access to document elements. When the server later must read a JSON value stored in this binary format, the value need not be parsed from a text representation. The binary format is structured to enable the server to look up subobjects or nested values directly by key or array index without reading all values before or after them in the document.

So, as the MySQL documentation states, the JSON data type should be used and not the text.

like image 72
cs04iz1 Avatar answered Sep 21 '22 10:09

cs04iz1