mysql json 性能_Mysql Json 性能测试(一)-倒排索引聚合值反查类场景

本文通过对比分析MySQL中使用JSON字段单表查询与关联查询的性能,发现对于特定查询条件,JSON字段查询效率低于关联或子查询。测试结果显示,随着数据量增加,JSON查询性能显著下降,主要原因是无法有效利用索引且操作复杂度高。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据结构及数据生成参见: http://www.cnblogs.com/lkpnotice/p/6903183.htm

主档当表main 中每一条记录对应5种type,随机选择random[>=1 & <=5]种 ,每一种type value 为 1-1000 的整数,个数为random数[>=1 <=10]个

因为db 容量的原因:main表记录数 : 28819     ref表记录数:380524

Mysql Json 相关分析参见:http://blog.csdn.net/wilsonliujin/article/details/72513909

main表记录示例:

6f5dfb4f16e0d0dc56fd2bb402d972f9.png

ref表示例:

8e78948cda75a62c5f4e7bcc816199f9.png

---------------------------------入题

验证问题-1:已知 asset_type 和 asset_value  通过json字段单表查main  和  通过关联ref 查main 测试(所需索引都已创建)

conditon asset_type='type1'  asset_value='894'

select count(*) from json_test_main  where JSON_SEARCH(json_str, 'one','894', NULL,'$.type1[*]')  is not null limit 0,10;

74 /28819

select count(*) from json_test_ref where  asset_type='type1'  asset_value='894';

74 /380524

a. 利用json 单表查询

select * from json_test_main  where JSON_SEARCH(json_str, 'one','894', NULL, '$.type1[*]')  is not null limit 0,10;

times: 61ms

select * from json_test_main  where JSON_SEARCH(json_str, 'one','894', NULL, '$.type1[*]')  is not null order by id limit 60,10;

times:268 ms

执行计划

d95f94f1cce867ec40e8cf359a371fad.png

b.利用关联

select main.* from json_test_main main,json_test_ref ref where main.id = ref.ref_id

and asset_type='type1' and asset_value='894' limit 0,10;

time used:34ms

执行计划:

29cbafd74dad92397fcac309155a89e6.png

select main.* from json_test_main main,json_test_ref ref where main.id = ref.ref_id

and asset_type='type1' and asset_value='894' limit 0,10;

time used:32ms

2ce23be02b698103e7126ef841af3ab4.png

c.子查询

select main.* from json_test_main_0001 main where main.id in

(select ref_id from json_test_ref_0001 ref where asset_type='type1' and asset_value='894') limit 0,10;

time = 33ms

执行计划

748ea08b84df081c669710a8acd2e80c.png

select main.* from json_test_main_0001 main where main.id in

(select ref_id from json_test_ref_0001 ref where asset_type='type1' and asset_value='894') order by id desc  limit 60,10;

time used : 33ms

3225880ad6ca7ffb6366aa553e930304.png

结论:

在本次mysql json的性能测试过程中, 在main 表 10000级  ref100000级 记录数时, 测试查询性能mysql json 慢于 关联或子查询。

原因分析如下:

1、mysql json无法有效使用索引,每次查询都是full scan,所以当数据集增大时,性能会严重滑坡;

2、mysql 官方描述对json 多了structure的优化,但是在基于json做查询时这部分代价仍然较高,测试数据集并不十分大,当数据量多时,json操作复杂度的累计会更为严重;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值