mysql中某个字段存在json,且json数据过大,严重影响查询效率。
处理办法:将此字段提出来,新建一个字段,将此json中的字段除大文件(类似于图片和音视频的base64格式)都转移,base64字段数据置为空。
将执行结果转移到新的字段,去掉base64字段的冗余。
原数据格式:
![](https://img-blog.csdnimg.cn/abf704b9bb7b40afa6da2ad83e565a94.png)
(新建字段)
ALTER table `patrol_item_result_copy1` ADD result MEDIUMTEXT COMMENT '执行结果-新';
-- 将patrol_item_result_copy1表的execute_result字段复制到表中的result字段
(先测试100条数据)
update patrol_item_result_copy1 a set a.result = a.execute_result limit 100;
(因为存在base64,json的格式也不规范,先规范下)
UPDATE patrol_item_result_copy1 SET result = REPLACE (result,'\\','');
UPDATE patrol_item_result_copy1 SET result = REPLACE (result,'/','');
UPDATE patrol_item_result_copy1 SET result = REPLACE (result,'"{','{');
UPDATE patrol_item_result_copy1 SET result = REPLACE (result,'}"','}');
这一步处理之后的数据:
![](https://img-blog.csdnimg.cn/88765c04a97840a29013e5206d557d21.png)
(查看json中的转义后的数据,是否正确)
SELECT json_extract(result,'$.result.mes.base64Code') from patrol_item_result_copy1 limit 100;
![](https://img-blog.csdnimg.cn/1432c18ac66e4454b8fa2eab46354baa.png)
-- UPDATE patrol_item_result_copy1 SET result = json_remove(result, '$.result.mes.base64Code'); -- 删除base64
UPDATE patrol_item_result_copy1 SET result = json_set(result, '$.result.mes.base64Code',''); -- 将base64内容置为空
最后的处理结果:
![](https://img-blog.csdnimg.cn/84268ad11c3f4923b2b16f0df27f0b5b.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)