mysql使用什么类型存json数据?
JSON (JavaScriptObject Notation) 是一种轻量级的数据交换格式,主要用于传送数据。JSON采用了独立于语言的文本格式,类似XML,但是比XML简单,易读并且易编写。对机器来说易于解析和生成,并且会减少网络带宽的传输。由于JSON格式可以解耦javascript客户端应用与Restful服务器端的方法调用,因而在互联网应用中被大量使用。
JSON的格式非常简单:名称/键值。之前MySQL版本里面要实现这样的存储,要么用VARCHAR要么用TEXT大文本。 MySQL5.7发布后,专门设计了JSON数据类型以及关于这种类型的检索以及其他函数解析。我们先看看MySQL老版本的JSON存取。
示例表结构:
CREATE TABLE json_test(
id INT,
person_desc TEXT
)ENGINE INNODB;
我们来插入一条记录:
INSERT INTO json_test VALUES (1,'{
"programmers": [{
"firstName": "Brett",
"lastName": "McLaughlin",
"email": "aaaa"
}, {
"firstName": "Jason",
"lastName": "Hunter",
"email": "bbbb"
}, {
"firstName": "Elliotte",
"lastName": "Harold",
"email": "cccc"
}],
"authors": [{
"firstName": "Isaac",
"lastName": "Asimov",
"genre": "sciencefiction"
}, {
"firstName": "Tad",
"lastName": "Williams",
"genre":"fantasy"
}, {
"firstName": "Frank",
"lastName": "Peretti",
"genre": "christianfiction"
}],
"musicians": [{
"firstName": "Eric",
"lastName": "Clapton",
"instrument": "guitar"
}, {
"firstName": "Sergei",
"lastName": "Rachmaninoff",
"instrument": "piano"
}]
}');
那一般我们遇到这样来存储JSON格式的话,只能把这条记录取出来交个应用程序,由应用程
来解析。如此一来,JSON又和特定的应用程序耦合在一起,其便利性的优势大打折扣。
现在到了MySQL5.7,可以支持对JSON进行属性的解析,我们重新修改下表结构:
ALTER TABLE json_test MODIFY person_desc json;
先看看插入的这行JSON数据有哪些KEY:
mysql> SELECT id,json_keys(person_desc) as "keys" FROM json_test\G
*************************** 1. row***************************
id: 1
keys: ["authors", "musicians","programmers"]
1 row in set (0.00 sec)
我们可以看到,里面有三个KEY,分别为authors,musicians,programmers。那现在找一
KEY把对应的值拿出来:
mysql> SELECT json_extract(AUTHORS,'$.lastName[0]') AS 'name', AUTHORS FROM
-> (
-> SELECT id,json_extract(person_desc,'$.authors[0][0]') AS "authors" FROM json_test
->UNION ALL
-> SELECT id,json_extract(person_desc,'$.authors[1][0]') AS "authors" FROM json_test
-> UNION ALL
-> SELECT id,json_extract(person_desc,'$.authors[2][0]') AS "authors" FROM json_test
-> ) AS T1
-> ORDER BY NAME DESC\G
*************************** 1. row***************************
name:"Williams"
AUTHORS: {"genre": "fantasy","lastName": "Williams", "firstName":"Tad"}
*************************** 2. row***************************
name:"Peretti"
AUTHORS: {"genre":"christianfiction", "lastName": "Peretti","firstName":
"Frank"}*************************** 3. row***************************
name:"Asimov"
AUTHORS: {"genre": "sciencefiction","lastName": "Asimov", "firstName":"Isaac"}
3 rows in set (0.00 sec)
现在来把详细的值罗列出来:
mysql> SELECT
->json_extract(AUTHORS,'$.firstName[0]') AS "firstname",
-> json_extract(AUTHORS,'$.lastName[0]')AS "lastname",
-> json_extract(AUTHORS,'$.genre[0]') AS"genre"
-> FROM
-> (
-> SELECT id,json_extract(person_desc,'$.authors[0]')AS "authors" FROM json
_test
-> ) AS T\G
*************************** 1. row***************************
firstname: "Isaac"
lastname:"Asimov"
genre:"sciencefiction"
1 row in set (0.00 sec)
我们进一步来演示把authors 这个KEY对应的所有对象删掉。
mysql> UPDATE json_test
-> SET person_desc =json_remove(person_desc,'$.authors')\G
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查找下对应的KEY,发现已经被删除掉了。
mysql> SELECT json_contains_path(person_desc,'all','$.authors')as authors_exists FROM
json_test\G
*************************** 1. row***************************
authors_exists: 0
1 row in set (0.00 sec)
总结下,虽然MySQL5.7开始支持JSON数据类型,但是我建议如果要使用的话,最好是把这的值取出来,然后在应用程序段来计算。毕竟数据库是用来处理结构化数据的,大量的未预先定义schema的json解析,会拖累数据库的性能。
2020-04-20 · MySQL开源数据库领先者
我们知道,JSON是一种轻量级的数据交互的格式,大部分NO SQL数据库的存储都用JSON。MySQL从5.7开始支持JSON格式的数据存储,并且新增了很多JSON相关函数。MySQL 8.0 又带来了一个新的把JSON转换为TABLE的函数JSON_TABLE,实现了JSON到表的转换。
举例一
我们看下简单的例子:
简单定义一个两级JSON 对象
mysql> set @ytt='{"name":[{"a":"ytt","b":"action"}, {"a":"dble","b":"shard"},{"a":"mysql","b":"oracle"}]}';Query OK, 0 rows affected (0.00 sec)
第一级:
mysql> select json_keys(@ytt);+-----------------+| json_keys(@ytt) |+-----------------+| ["name"] |+-----------------+1 row in set (0.00 sec)
第二级:
mysql> select json_keys(@ytt,'$.name[0]');+-----------------------------+| json_keys(@ytt,'$.name[0]') |+-----------------------------+| ["a", "b"] |+-----------------------------+1 row in set (0.00 sec)
我们使用MySQL 8.0 的JSON_TABLE 来转换 @ytt。
mysql> select * from json_table(@ytt,'$.name[*]' columns (f1 varchar(10) path '$.a', f2 varchar(10) path '$.b')) as tt;
+-------+--------+
| f1 | f2 |
+-------+--------+
| ytt | action |
| dble | shard |
| mysql | oracle |
+-------+--------+
3 rows in set (0.00 sec)
- set @json_str1 = ' { "query_block": { "select_id": 1, "cost_info": { "query_cost": "1.00" }, "table": { "table_name": "bigtable", "access_type": "const", "possible_keys": [ "id" ], "key": "id", "used_key_parts": [ "id" ], "key_length": "8", "ref": [ "const" ], "rows_examined_per_scan": 1, "rows_produced_per_join": 1, "filtered": "100.00", "cost_info": { "read_cost": "0.00", "eval_cost": "0.20", "prefix_cost": "0.00", "data_read_per_join": "176" }, "used_columns": [ "id", "log_time", "str1", "str2" ] } }}';
- mysql> select json_keys(@json_str1) as 'first_object';+-----------------+| first_object |+-----------------+| ["query_block"] |+-----------------+1 row in set (0.00 sec)
- mysql> select json_keys(@json_str1,'$.query_block') as 'second_object';+-------------------------------------+| second_object |+-------------------------------------+| ["table", "cost_info", "select_id"] |+-------------------------------------+1 row in set (0.00 sec)
- mysql> select json_keys(@json_str1,'$.query_block.table') as 'third_object'\G*************************** 1. row ***************************third_object: ["key","ref","filtered","cost_info","key_length","table_name","access_type","used_columns","possible_keys","used_key_parts","rows_examined_per_scan","rows_produced_per_join"]1 row in set (0.01 sec)
- mysql> select json_extract(@json_str1,'$.query_block.table.cost_info') as 'forth_object'\G*************************** 1. row ***************************forth_object: {"eval_cost":"0.20","read_cost":"0.00","prefix_cost":"0.00","data_read_per_join":"176"}1 row in set (0.00 sec)
SELECT * FROM JSON_TABLE(@json_str1,
"$.query_block"
COLUMNS(
rowid FOR ORDINALITY,
NESTED PATH '$.table'
COLUMNS (
a1_1 varchar(100) PATH '$.key',
a1_2 varchar(100) PATH '$.ref[0]',
a1_3 varchar(100) PATH '$.filtered',
nested path '$.cost_info'
columns (
a2_1 varchar(100) PATH '$.eval_cost' ,
a2_2 varchar(100) PATH '$.read_cost',
a2_3 varchar(100) PATH '$.prefix_cost',
a2_4 varchar(100) PATH '$.data_read_per_join'
),
a3 varchar(100) PATH '$.key_length',
a4 varchar(100) PATH '$.table_name',
a5 varchar(100) PATH '$.access_type',
a6 varchar(100) PATH '$.used_key_parts[0]',
a7 varchar(100) PATH '$.rows_examined_per_scan',
a8 varchar(100) PATH '$.rows_produced_per_join',
a9 varchar(100) PATH '$.key'
),
NESTED PATH '$.cost_info'
columns (
b1_1 varchar(100) path '$.query_cost'
),
c INT path "$.select_id"
)
) AS tt;
+-------+------+-------+--------+------+------+------+------+------+----------+-------+------+------+------+------+------+------+
| rowid | a1_1 | a1_2 | a1_3 | a2_1 | a2_2 | a2_3 | a2_4 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | b1_1 | c |
+-------+------+-------+--------+------+------+------+------+------+----------+-------+------+------+------+------+------+------+
| 1 | id | const | 100.00 | 0.20 | 0.00 | 0.00 | 176 | 8 | bigtable | const | id | 1 | 1 | id | NULL | 1 |
| 1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1.00 | 1 |
+-------+------+-------+--------+------+------+------+------+------+----------+-------+------+------+------+------+------+------+
2 rows in set (0.00 sec)
举例二
再来一个复杂点的例子,用的是EXPLAIN 的JSON结果集。
JSON 串 @json_str1。
第一级:
第二级:
第三级:
第四级:
那我们把这个JSON 串转换为表。
当然,JSON_table 函数还有其他的用法,我这里不一一列举了,详细的参考手册。
广告 您可能关注的内容 |