There are many advantages to the JSON
field compared to TEXT
:
-
Validation - The data is automatically validated. If the JSON is invalid, the record will not be entered and the operation will produce an error
-
Efficient Access - The storage format is optimized. JSON documents saved in columns of type
JSON
are actually converted into an internal format that allows quick reading of document elements. When the server needs to read a JSON value stored in this binary format, the value does not need to be converted from a text representation.
-
Performance - You can improve query performance by creating indexes within the JSON columns. This is possible through the secondary indexes in virtual columns .
-
Convenience - It's much easier to retrieve values in the queries through the JSON_EXTRACT
or with the new coluna->caminho
syntax:
SELECT nome, conta->"$.saldo" FROM clientes WHERE conta->"$.atrasada" = true;
So now it's much easier to manipulate the data and store those complex values in a column. It can be said that MySQL supports a non-relational (NoSQL) data structure within a relational structure. Incredible!
However, as the article says MySQL 5.7 brings sexy back with JSON , note that:
NoSQL specialized databases (document-oriented banks, key-value banks, and graph-based banks) remain better options for your specific use cases, but adding this type of data may allow you to reduce complexity of its technology stack.