MySQL 5.7原生JSON格式支持

cc68 9年前

MySQL 5.7原生JSON格式支持:

mysql> create table user ( uid int auto_increment,      -> data json,primary key(uid))engine=innodb;  Query OK, 0 rows affected (0.01 sec)     mysql> insert into user values (NULL,      -> '{"name":"David","mail":"jiangchengyao@gmail.com","address":"Shangahai"}');  Query OK, 1 row affected (0.00 sec)     mysql> insert into user values (NULL,'{"name":"Amy","mail":"amy@gmail.com"}');  Query OK, 1 row affected (0.00 sec)


可以看到我们新建了表user,并且将列data定义为了JSON类型。这意味着我们可以对插入的数据做JSON格式检查,确保其符合JSON格式的约束,如插入一条不合法的JSON数据会报如下错误:
mysql> insert into user values (NULL,"test");
ERROR 3130 (22032): Invalid JSON text: "Invalid value" at position 2 in value (or column) 'test'.
此外,正如前面所说的,MySQL 5.7提供了一系列函数来高效地处理JSON字符,而不是需要遍历所有字符来查找,这不得不说是对MariaDB dynamic column的巨大改进:
mysql> select jsn_extract(data, '$.name'),jsn_extract(data,'$.address') from user;  +-----------------------------+-------------------------------+  | jsn_extract(data, '$.name') | jsn_extract(data,'$.address') |  +-----------------------------+-------------------------------+  | "David" | "Shangahai" |  | "Amy" | NULL |  +-----------------------------+-------------------------------+  2 rows in set (0.00 sec)

当然,最令人的激动的功能应该是MySQL 5.7的虚拟列功能,通过传统的B+树索引即可实现对JSON格式部分属性的快速查询。使用方法是首先创建该虚拟列,然后在该虚拟列上创建索引:
mysql> ALTER TABLE user ADD user_name varchar(128)      -> GENERATED ALWAYS AS (jsn_extract(data,'$.name')) VIRTUAL;  Query OK, 0 rows affected (0.01 sec)  Records: 0 Duplicates: 0 Warnings: 0     mysql> select user_name from user;  +-----------+  | user_name |  +-----------+  | "Amy"     |  | "David"   |  +-----------+  2 rows in set (0.00 sec)     mysql> alter table user add index idx_username (user_name);  Query OK, 2 rows affected (0.01 sec)  Records: 2  Duplicates: 0  Warnings: 0

然后可以通过添加的索引对用户名进行快速的查询,这和普通类型的列查询一样。而通过explain可以验证优化器已经选择了在虚拟列上创建的新索引:
mysql> explain select from user where user_name='"Amy"'\G
**
1. row ***
           id: 1
  select_type: SIMPLE
        table: user
   partitions: NULL
         type: ref
possible_keys: idx_username
          key: idx_username
      key_len: 131
          ref: const
         rows: 1
     filtered: 100.00
        Extra: NULL
1 row in set, 1 warning (0.00 sec)
可以发现MySQL 5.7对于JSON格式堪称完美

摘自:http://www.innomysql.net/article/15319.html



原文  http://justwinit.cn/post/7952/