As of MySQL 5.7.8, 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:
从MySQL5.7.8开始,mysql支持RFC 7159定义的原生JSON数据类型,能够有效访问JSON文档中的数据。相比于存储于字符串,json类型存储有以下优点:
Automatic validation of JSON documents stored in JSON columns. Invalid documents produce an error.
自动校验json文档是否正确,不合法的值将返回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.
优化存储格式。存储在json列中的json文档会被转换成内部格式,使得更快访问文档里面的元素。当服务器需要读取以二进制格式存储的JSON值时,不需要从文本中解析。二进制格式结构化能时服务器直接通过键值或者数组索引查找对应值,而不需要访问之前或之后的所有值。
The space required to store a JSON document is roughly the same as for LONGBLOB or LONGTEXT; see Section 11.7, “Data Type Storage Requirements”, for more information. It is important to keep in mind that the size of any JSON document stored in a JSON column is limited to the value of the max_allowed_packet system variable.
JSON文档占用的空间与LONGBLOB或者LONGTEXT基本等同。JSON列的大小受限于max_allowed_packet变量的值。
A JSON column cannot have a non-NULL default value.
JSON类型不能有非空默认值。
JSON columns, like columns of other binary types, are not indexed directly; instead, you can create an index on a generated column that extracts a scalar value from the JSON column.
JSON字段跟其它二进制类型一样,不能直接创建索引;但是可以通过在生成的列上创建一个索引,生成的列指向JSON里的标量值。