创建表
create table news (uid int auto_increment, data json, primary key(uid))engine innodb;
插入数据
insert into news values (NULL,‘{"name":"tester","mail":"[email protected]","address":"Shangahai"}‘)
插入数据不是Json格式,会报以下错
insert into news values(NULL,‘test‘); Error Code: 3140.Invalid JSON text: "Invalid value." at position 1 in value (or column) ‘test‘
新内置函数,高效地处理JSON字符,不需要遍历所有数据查找。
select json_extract(data, ‘$.name‘),json_extract(data,‘$.address‘) from news where json_extract(data, ‘$.name‘) = ‘David‘;
虚拟列功能,通过传统的B+树索引即可实现对JSON格式部分属性的快速查询
加虚拟列
ALTER TABLE user ADD user_name varchar(50) GENERATED ALWAYS AS (json_extract(data,‘$.name‘)) VIRTUAL;
加索引
alter table user add index idx_username (user_name);
和普通列查询一样
select * from user where user_name=‘"David"‘
时间: 2024-10-11 05:38:55