数据结构及数据生成参见: 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表记录示例:
ref表示例:
---------------------------------入题
验证问题-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
执行计划
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
执行计划:
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
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
执行计划
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
结论:
在本次mysql json的性能测试过程中, 在main 表 10000级 ref100000级 记录数时, 测试查询性能mysql json 慢于 关联或子查询。
原因分析如下:
1、mysql json无法有效使用索引,每次查询都是full scan,所以当数据集增大时,性能会严重滑坡;
2、mysql 官方描述对json 多了structure的优化,但是在基于json做查询时这部分代价仍然较高,测试数据集并不十分大,当数据量多时,json操作复杂度的累计会更为严重;