MySQL5.7 JSON类型列创建索引查询一例
发表于:2025-02-22 作者:千家信息网编辑
千家信息网最后更新 2025年02月22日,创建json类型的表test:mysql> CREATE TABLE test(data JSON);Query OK, 0 rows affected (0.47 sec)mysql> insert
千家信息网最后更新 2025年02月22日MySQL5.7 JSON类型列创建索引查询一例创建json类型的表test:
mysql> CREATE TABLE test(data JSON);
Query OK, 0 rows affected (0.47 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["1","2"]}');
Query OK, 1 row affected (0.39 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["2","3"]}');
Query OK, 1 row affected (0.39 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["3","4"]}');
Query OK, 1 row affected (0.39 sec)
mysql> select json_type(data) from test;
+-----------------+
| json_type(data) |
+-----------------+
| OBJECT |
+-----------------+
1 row in set (0.15 sec)
mysql> select * from test;
+---------------------------------------------------+
| data |
+---------------------------------------------------+
| {"sex": "nan", "area": ["1", "2"], "name": "abc"} |
+---------------------------------------------------+
1 row in set (0.10 sec)
mysql> select json_extract(data, '$.name' ) from test;
+-------------------------------+
| json_extract(data, '$.name' ) |
+-------------------------------+
| "abc" |
+-------------------------------+
1 row in set (0.00 sec)
mysql> select json_extract(data, '$.sex' ) from test;
+------------------------------+
| json_extract(data, '$.sex' ) |
+------------------------------+
| "nan" |
+------------------------------+
1 row in set (0.00 sec)
mysql> select json_extract(data, '$.area' ) from test;
+-------------------------------+
| json_extract(data, '$.area' ) |
+-------------------------------+
| ["1", "2"] |
+-------------------------------+
1 row in set (0.00 sec)
在data列上,对"area"建立虚拟列
mysql> ALTER TABLE test ADD data_idx varchar(128) GENERATED ALWAYS AS (json_extract(data,'$.area')) VIRTUAL;
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0
如果要在JSON列上进行检索,需要对检索的key创建虚拟列,然后再虚拟列上创建索引。
mysql> alter table test add index idx_data(data_idx);
Query OK, 0 rows affected (0.67 sec)
Records: 0 Duplicates: 0 Warnings: 0
where条件需要使用虚拟列来进行检索,执行计划如下:
mysql> explain select * from test where data_idx='["3", "4"]';
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test | NULL | ref | idx_data | idx_data | 387 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.04 sec)
发现走了索引
mysql> CREATE TABLE test(data JSON);
Query OK, 0 rows affected (0.47 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["1","2"]}');
Query OK, 1 row affected (0.39 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["2","3"]}');
Query OK, 1 row affected (0.39 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["3","4"]}');
Query OK, 1 row affected (0.39 sec)
mysql> select json_type(data) from test;
+-----------------+
| json_type(data) |
+-----------------+
| OBJECT |
+-----------------+
1 row in set (0.15 sec)
mysql> select * from test;
+---------------------------------------------------+
| data |
+---------------------------------------------------+
| {"sex": "nan", "area": ["1", "2"], "name": "abc"} |
+---------------------------------------------------+
1 row in set (0.10 sec)
mysql> select json_extract(data, '$.name' ) from test;
+-------------------------------+
| json_extract(data, '$.name' ) |
+-------------------------------+
| "abc" |
+-------------------------------+
1 row in set (0.00 sec)
mysql> select json_extract(data, '$.sex' ) from test;
+------------------------------+
| json_extract(data, '$.sex' ) |
+------------------------------+
| "nan" |
+------------------------------+
1 row in set (0.00 sec)
mysql> select json_extract(data, '$.area' ) from test;
+-------------------------------+
| json_extract(data, '$.area' ) |
+-------------------------------+
| ["1", "2"] |
+-------------------------------+
1 row in set (0.00 sec)
在data列上,对"area"建立虚拟列
mysql> ALTER TABLE test ADD data_idx varchar(128) GENERATED ALWAYS AS (json_extract(data,'$.area')) VIRTUAL;
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0
如果要在JSON列上进行检索,需要对检索的key创建虚拟列,然后再虚拟列上创建索引。
mysql> alter table test add index idx_data(data_idx);
Query OK, 0 rows affected (0.67 sec)
Records: 0 Duplicates: 0 Warnings: 0
where条件需要使用虚拟列来进行检索,执行计划如下:
mysql> explain select * from test where data_idx='["3", "4"]';
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test | NULL | ref | idx_data | idx_data | 387 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.04 sec)
发现走了索引
检索
索引
类型
条件
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
wps表格汇总同名称数据库
国家中小学生网络安全
乌兰察布的服务器
wps列数据重复输入数据库
服务器维护与管理是什么东西
手机断断续续连接不上服务器
幼儿园网络安全培训标题
歌尔软件开发岗面试题
微信服务器安全狗怎么关闭
已有数据库创建表
计算机软件开发五年大专学校
网络安全基础 公开课
珠海大唐网络技术有限公司
如何建立中心数据库
软件开发员职业测评系统
我的世界猎魔传说服务器宣传片
长春软件开发 选择吉网传媒
wincc 客户端服务器
绿园区正规网络技术咨询口碑推荐
计算机类包括计算机网络技术吗
瀚高数据库默认路径
视易点歌服务器每年要多少钱
网络安全的工作目标有什么
数据库系统概论难学嘛
新闻的点击量与数据库结合
宝可梦世界服务器怎么去空岛
设置网络安全专岗
cmd启动数据库密令
留学生软件开发
2022网络安全知识展板