×î½üÓиöÒµÎñÐèÒªÄܹ»´æ´¢Json²¢×öһЩ¼òµ¥µÄÒµÎñÂß¼´¦Àí¡£ÒµÎñÕÒµ½ÎÒ˵jsonµÄÊý¾Ý·ÖÎöºÜÄÑÓÃmysql 5.6 ,ÕâÑùµÄ´¿´âÐдæÀ´´¦ÀíÄѶȺܴó£¬ÎÊÎÒÓÐûɶ°ì·¨¡£
ÎÒµÚÒ»Ïëµ½µÄÊÇmongodb£¬µÚ¶þÏëµ½µÄ¾ÍÊÇmysql 5.7 ¡£ È»ºóÒ»²é£¬°¥Ñ½£¬ÒѾGAÁË¡£ÖÚËùÖÜÖªµÄ£¬mongodbµÄÒýÇæ²ãµÄÎȶ¨ÐÔÒ»Ö±ÊǶ̰壬¶øinnodb¾¹ý10ÄêµÄÑéÖ¤£¬ÒѾÊǷdz£Îȶ¨µÄ¶«Î÷ÁË¡£
ËùÒÔ¾ÍÔÚÏ룬ÊDz»ÊÇ¿ÉÒÔÊÔÊÔmysql 5.7 £¬ ¼òµ¥³¢ÊÔ£¬·¢ÏÖmysql 5.7 + DRDS £¬ÍêÈ«²»´ømongoDBÍæ°¡¡£¡£¡£
¿´¿´Îҵļòµ¥¹¦ÄܲâÊÔ£º
create table json_test ( uid int auto_increment,data json,primary key(uid))engine=innodb;
½¨¿â
mysql> insert into json_test values (NULL, '{"name":"name1","mobile":"15044447279","amount":400}');
Query OK, 1 row affected (0.01 sec)
mysql> insert into json_test values (NULL, '{"name":"name1","mobile":"15044447279","amount":300}');
Query OK, 1 row affected (0.01 sec)
mysql> insert into json_test values (NULL, '{"name":"name2","mobile":"15044447278","amount":300}');
Query OK, 1 row affected (0.01 sec)
mysql> insert into json_test values (NULL, '{"name":"name3","mobile":"15044447277","amount":300}');
Query OK, 1 row affected (0.01 sec)
²åÈëËÄÌõÓï¾ä
mysql> select data from json_test;
+-----------------------------------------------------------+
| data |
+-----------------------------------------------------------+
| {"name": "name1", "amount": 400, "mobile": "15044447279"} |
| {"name": "name1", "amount": 300, "mobile": "15044447279"} |
| {"name": "name2", "amount": 300, "mobile": "15044447278"} |
| {"name": "name3", "amount": 300, "mobile": "15044447277"} |
+-----------------------------------------------------------+
²éѯÕâËÄÌõjsonÓï¾ä
mysql> select data->"$.name" as name ,sum(data->"$.amount") from json_test group by name;
+---------+-----------------------+
| name | sum(data->"$.amount") |
+---------+-----------------------+
| "name1" | 700 |
| "name2" | 300 |
| "name3" | 300 |
+---------+-----------------------+
×ö¸ögroup by sum ³£¼ûµÄͳ¼Æ²Ù×÷
insert into json_test values (NULL, '{"mobile":"15044447277","amount":300}');
²åÈëÒ»¸ö²»´ønameµÄÊý¾Ý£¬¿´¿´Ë÷Òý¶Ô¿ÕÊý¾ÝµÄ¼æÈÝÐÔÇé¿ö¡£
mysql> ALTER TABLE json_test ADD user_name varchar(128) GENERATED ALWAYS AS (json_extract(data,'$.name')) VIRTUAL;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table json_test add index idx_username (user_name);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
´´½¨ÐéÄâÁв¢½¨Á¢Ë÷Òý
mysql> select user_name,sum(data->"$.amount") from json_test where user_name = '"name1"';
+-----------+-----------------------+
| user_name | sum(data->"$.amount") |
+-----------+-----------------------+
| "name1" | 700 |
+-----------+-----------------------+
²é¿´·ûºÏij¸öuser_nameµÄÊý¾ÝµÄsum¡£
mysql> explain select user_name,sum(data->"$.amount") from json_test where user_name = '"name1"';
+----+-------------+-----------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | json_test | NULL | ref | idx_username | idx_username | 131 | const | 2 | 100.00 | NULL |
+----+-------------+-----------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
È·ÈÏ´øË÷ÒýÊý¾Ý×ßµ½ÁËË÷ÒýÉÏ
===========
È»ºó£¬ÕⶫÎ÷¾¹È»»¹Ö§³ÖÊÂÎñ¡£¡£Õâ¸ö¾ÍÅ£±Æ´óÁË¡£¡£
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
¿ªÆôÊÂÎñ
mysql> select * from json_test;
+-----+-------------------------------------------------------------------+-----------+
| uid | data | user_name |
+-----+-------------------------------------------------------------------+-----------+
| 1 | {"name": "name1", "amount": 400, "mobile": "15044447279"} | "name1" |
| 2 | {"name": "name1", "amount": 300, "mobile": "15044447279"} | "name1" |
| 3 | {"name": "name2", "amount": 300, "mobile": "15044447278"} | "name2" |
| 4 | {"name": "name3", "amount": 300, "mobile": "15044447277"} | "name3" |
| 5 | {"amount": 300, "mobile": "15044447277"} | NULL |
| 6 | {"amount": "300", "name”:”name2”,”mobile": "15044447278"} | NULL |
+-----+-------------------------------------------------------------------+-----------+
²é¿´Ô±í¡£
mysql> insert into json_test (uid,data) values (NULL, '{"name":"name1","mobile":"15044447279","amount":300}');
Query OK, 1 row affected (0.00 sec)
²åÈëÐÂÊý¾Ý
¡¡