随着 nosql 数据库的兴起,json 作为一种轻量级的数据交换格式受到了广泛的关注。为了满足现代应用程序的需求,mysql 8引入了原生的 json 数据类型,提供了一系列强大的 json 函数来处理和查询 json 数据。本文将深入探讨 mysql 8 中json 类型的特性、函数、索引以及实际应用场景。
1. 引言
在早期的 mysql 版本中,开发者通常将 json 数据以字符串的形式存储在数据库中,这导致了查询效率低下和数据处理复杂。为了解决这个问题,mysql 8 引入了原生的 json 数据类型,允许我们以结构化的方式存储和查询 json 数据。
2. json 数据类型特性
- 验证:当插入或更新 json 列时,mysql 会自动验证数据的 json 格式,确保数据的完整性。
- 优化存储:json 数据类型以二进制格式存储,相比纯文本存储更加高效。
- 灵活性:json 列可以存储数组、对象、嵌套结构等,为数据的表示提供了极大的灵活性。
3. json 函数
mysql 8 提供了一系列内置函数来操作和查询 json 数据:
- 提取数据:使用 json_extract() 函数可以从 json 文档中提取指定的数据片段。
- 修改数据:json_insert()、json_replace() 和 json_remove() 函数允许你向 json 文档中添加、替换或删除数据。
- 创建 json:json_array() 和 json_object() 函数用于创建 json 数组和对象。
- 查询功能:json_length()、json_keys()、json_valid() 等函数,用于获取 json 数据的长度、键或验证 json 数据的有效性。
- 其他函数,如 json_quote(), json_unquote(), json_contains(), json_contains_path(), json_array_append(), json_array_insert() 等等。
下面的例子看看每个函数的具体使用方法:
首先,我们创建一个名为 json_example 的表,并插入一条 json 数据:
create table json_example ( id int auto_increment primary key, data json ); insert into json_example (data) values ( '{ "name": "john doe", "age": 30, "address": { "street": "123 main st", "city": "anytown", "zip": "12345" }, "phonenumbers": ["123-456-7890", "987-654-3210"], "isactive": true }' );
表 json_example 中有一条包含 json 数据的记录。接下来,我们将使用不同的 json 函数来查询和修改这个数据.
3.1 json_extract()
- 提取 json 数据中的特定部分
-- 提取 name 字段的值 select json_extract(data, '$.name') as extracted_name from json_example; -- 结果: "john doe" -- 提取 address 对象的 city 字段的值 select json_extract(data, '$.address.city') as city from json_example; -- 结果: "anytown"
3.2 json_insert()
- 向 json 数据中插入新的部分,如果路径已存在则不会替换。
-- 在 phonenumbers 数组后面插入一个新的电话号码 update json_example set data = json_insert(data, '$.phonenumbers[2]', '555-123-4567'); -- 此时 phonenumbers 变为 ["123-456-7890", "987-654-3210", "555-123-4567"]
3.3 json_replace()
- 替换 json 数据中的部分,如果路径不存在则不会添加。
-- 替换 name 字段的值 update json_example set data = json_replace(data, '$.name', 'jane smith'); -- 此时 name 变为 "jane smith"
3.4 json_remove()
- 从 json 数据中移除指定的部分。
-- 移除 phonenumbers 数组中的第一个电话号码 update json_example set data = json_remove(data, '$.phonenumbers[0]'); -- 此时 phonenumbers 变为 ["987-654-3210", "555-123-4567"]
3.5 json_array() 和 json_object()
- 创建 json 数组和对象
-- 创建一个新的 json 数组 select json_array('a', 1, true); -- 结果: ["a", 1, true] -- 创建一个新的 json 对象 select json_object('key1', 'value1', 'key2', 2); -- 结果: {"key1": "value1", "key2": 2} json_length() - 获取 json 文档或数组的长度。 sql -- 获取 phonenumbers 数组的长度 select json_length(data->'$.phonenumbers') as phone_numbers_length from json_example; -- 结果: 2 (因为 phonenumbers 数组现在有两个元素)
3.6 json_keys()
- 获取 json 对象的所有键
-- 获取 json 对象的所有键 select json_keys(data) as object_keys from json_example; -- 结果: ["name", "age", "address", "phonenumbers", "isactive"]
3.7 json_valid()
- 验证 json 数据的有效性。
-- 验证 data 列是否包含有效的 json select json_valid(data) as is_valid_json from json_example; -- 结果: 1 (表示 true,因为 data 列包含有效的 json)
3.8 json_quote() 和 json_unquote()
- 将字符串转换为 json 格式的字符串,以及反向操作。
假设json_example 表中存在这样一条数据
insert into json_example (data) values ( '{ "name": "john", "interests": ["reading", "music"], "friends": [ {"name": "alice", "age": 28}, {"name": "bob", "age": 32} ] }' );
现在我们将使用上述函数对这条数据进行操作:
-- 使用 json_quote 将普通字符串转换为 json 字符串 select json_quote('hello, world!') as quoted_string; -- 结果: ""hello, world!"" -- 使用 json_unquote 将 json 字符串转换回普通字符串 select json_unquote('"hello, world!"') as unquoted_string; -- 结果: hello, world!
请注意,在实际的数据列上使用这些函数时,你通常会对已存储的 json 值或要插入的值进行操作。
3.9 json_contains()
- 检查 json 文档是否包含指定的值。
-- 检查 interests 数组是否包含 "reading" select json_contains(data->'$.interests', '"reading"') as contains_reading from json_example; -- 结果: 1 (表示 true,因为 interests 数组包含 "reading")
注意,因为 json 中的字符串是被双引号包围的,所以我们在查询时也需要对搜索的字符串值加上双引号。
3.9 json_contains_path()
- 检查 json 文档是否包含指定的路径。
-- 检查是否存在 friends 数组中的对象的 name 路径 select json_contains_path(data, 'one', '$.friends[*].name') as contains_path from json_example; -- 结果: 1 (表示 true,因为存在该路径)
3.10 json_array_append()
- 向 json 数组追加元素。
-- 向 interests 数组追加 "traveling" update json_example set data = json_set(data, '$.interests[2]', 'traveling'); -- 注意:这里使用了 json_set,因为 json_array_append 需要指定路径到具体数组 -- 在 mysql 8.0.17 及更高版本中,可以使用 json_array_append 正确地追加元素 -- 例如: json_array_append(data, '$.interests', 'traveling')
注意:上面的例子中使用了 json_set 而不是 json_array_append,因为在 mysql 8.0.17 之前,json_array_append 的语法有些不同,它要求指定路径到一个具体的数组元素。从 8.0.17 开始,json_array_append 可以正确地追加到数组末尾。
正确的 json_array_append 用法如下:
-- 向 interests 数组追加 "traveling"(适用于 mysql 8.0.17 及更高版本) update json_example set data = json_array_append(data, '$.interests', 'traveling');
3.11 json_array_insert()
- 在 json 数组的指定位置插入元素。
-- 在 interests 数组的第一个位置插入 "gaming" update json_example set data = json_array_insert(data, '$.interests[0]', 'gaming'); -- 结果: interests 数组现在是 ["gaming", "reading", "music", "traveling"]
4. json 索引
为了提高查询性能,mysql 8 支持为 json 列创建索引。但由于 json 数据的灵活性,直接对整个 json 文档创建索引并不高效。因此,mysql 引入了虚拟列(virtual columns)的概念。
- 虚拟列:虚拟列允许你根据 json 列中的值生成一个新的列,并为这个新列创建索引。这样,当你根据 json 数据中的某个字段进行查询时,mysql 可以使用索引来加速查询。(关于虚拟列我将在之后的文章详解)
- 创建索引:通过结合使用 json_extract() 函数和虚拟列,你可以轻松地为 json 数据中的特定字段创建索引。
基于上面的json_example 表,我们来看下为json字段创建索引
4.1 添加虚拟列
我们将添加一个名为 first_interest 的虚拟列,该列将存储 interests 数组的第一个元素。
alter table json_example add first_interest varchar(255) generated always as (json_unquote(json_extract(data, '$.interests[0]'))) virtual;
在这里,我们使用了 json_extract() 来获取 interests 数组的第一个元素,并用 json_unquote() 去除引号,因为 json_extract() 返回的是 json 格式的字符串。
4.2 为虚拟列创建索引
create index idx_first_interest on json_example(first_interest);
现在,我们为 first_interest 列创建了一个索引,这将加速基于该列的查询。
4.3 查询优化
现在,我们可以基于 first_interest 列进行查询,并利用索引来加速查询过程。
select * from json_example where first_interest = 'reading';
由于我们为 first_interest 创建了索引,这个查询将会更加高效。但是,请注意,这种方法仅适用于查询 interests 数组的第一个元素。如果你需要查询数组中的其他元素,你可能需要采用其他策略,比如使用全文搜索、倒排索引或者将 json 数据规范化到关系型结构中。
5. 实际应用场景
- 配置文件存储:应用程序的配置信息通常以 json 格式表示。使用 mysql 8 的 json 数据类型,你可以轻松地将这些配置信息存储在数据库中,并使用 json 函数进行查询和修改。
- 日志记录:日志条目通常以结构化的格式存储,json 是一个理想的选择。通过将日志数据存储在 json 列中,你可以轻松地分析和查询日志数据。
- 与前端集成:使用 json 与后端进行数据交换。使用 mysql 8 的 json 支持,你可以简化数据库与前端之间的数据交互。
6. 注意事项
- 性能:虽然 mysql 8 提供了对 json 的支持,但与传统的关系型数据相比,json 查询可能仍然不够高效。
- 数据验证:虽然 mysql 会验证 json 数据的格式,但它不会验证数据的业务规则或完整性。
- 复杂性:json 数据的结构可能比传统的关系型数据更复杂,这可能会增加查询和维护的难度。
7. 结语
mysql 8 的 json 数据类型为存储和查询 json 数据提供了强大的支持。通过内置的 json 函数和虚拟列索引,开发者可以高效地处理 json 数据,满足现代应用程序的需求。如果你正在开发需要存储和查询 json 数据的应用程序,不妨考虑使用 mysql 8 的 json 功能来简化你的工作。
到此这篇关于mysql json类型的功能与应用的文章就介绍到这了,更多相关mysql json类型内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论