查询背景
有一个表tmp_test_course大概有10万条记录,然后有个json字段叫outline,存了一对多关系(保存了多个编码,例如jy1577683381775)
我们需要在这10万条数据中检索特定类型的数据,目标总数据量:2931
条
1
|
select count (*) from tmp_test_course where `type`=5 and del=2 and is_leaf=1
|
我们在限定为上面类型的同时,还得包含下面任意一个编码(也就是or查询)
jy1577683381775
jy1577683380808
jy1577683379178
jy1577683378676
jy1577683377617
jy1577683376672
jy1577683375903
jy1578385720787
jy1499916986208
jy1499917112460
jy1499917093400
jy1499917335579
jy1499917334770
jy1499917333339
jy1499917331557
jy1499917330833
jy1499917329615
jy1499917328496
jy1576922006950
jy1499916993558
jy1499916992308
jy1499917003454
jy1499917002952
下面分别列出4种方式查询outline字段,给出相应的查询时间和扫描行数
一、like查询
耗时248毫秒
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
select * from tmp_test_course
where `type`=5 and del=2 and is_leaf=1
and (
outline like '%jy1577683381775%'
or outline like '%jy1577683380808%'
or outline like '%jy1577683379178%'
or outline like '%jy1577683378676%'
or outline like '%jy1577683377617%'
or outline like '%jy1577683376672%'
or outline like '%jy1577683375903%'
or outline like '%jy1578385720787%'
or outline like '%jy1499916986208%'
or outline like '%jy1499917112460%'
or outline like '%jy1499917093400%'
or outline like '%jy1499917335579%'
or outline like '%jy1499917334770%'
or outline like '%jy1499917333339%'
or outline like '%jy1499917331557%'
or outline like '%jy1499917330833%'
or outline like '%jy1499917329615%'
or outline like '%jy1499917328496%'
or outline like '%jy1576922006950%'
or outline like '%jy1499916993558%'
or outline like '%jy1499916992308%'
or outline like '%jy1499917003454%'
or outline like '%jy1499917002952%'
)
|
explain分析结果如下,全表扫描
二、json函数查询
耗时196毫秒,速度稍微快了一点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
select * from tmp_test_course
where `type`=5 and del=2 and is_leaf=1
and
(
json_search(outline, 'one' , 'jy1577683381775' ) is not null or
json_search(outline, 'one' , 'jy1577683380808' ) is not null or
json_search(outline, 'one' , 'jy1577683379178' ) is not null or
json_search(outline, 'one' , 'jy1577683378676' ) is not null or
json_search(outline, 'one' , 'jy1577683377617' ) is not null or
json_search(outline, 'one' , 'jy1577683376672' ) is not null or
json_search(outline, 'one' , 'jy1577683375903' ) is not null or
json_search(outline, 'one' , 'jy1578385720787' ) is not null or
json_search(outline, 'one' , 'jy1499916986208' ) is not null or
json_search(outline, 'one' , 'jy1499917112460' ) is not null or
json_search(outline, 'one' , 'jy1499917093400' ) is not null or
json_search(outline, 'one' , 'jy1499917335579' ) is not null or
json_search(outline, 'one' , 'jy1499917334770' ) is not null or
json_search(outline, 'one' , 'jy1499917333339' ) is not null or
json_search(outline, 'one' , 'jy1499917331557' ) is not null or
json_search(outline, 'one' , 'jy1499917330833' ) is not null or
json_search(outline, 'one' , 'jy1499917329615' ) is not null or
json_search(outline, 'one' , 'jy1499917328496' ) is not null or
json_search(outline, 'one' , 'jy1576922006950' ) is not null or
json_search(outline, 'one' , 'jy1499916993558' ) is not null or
json_search(outline, 'one' , 'jy1499916992308' ) is not null or
json_search(outline, 'one' , 'jy1499917003454' ) is not null or
json_search(outline, 'one' , 'jy1499917002952' ) is not null
)
|
explain分析结果如下,还是全表扫描
三、联合索引查询
下面为该表建立一个联合索引(本来想建一个type-del-is_leaf-outline的索引,但是outline字段太长限制,所以只加type-del-is_leaf的联合索引
1
|
alter table tmp_test_course add key `type-del-is_leaf` (`type`,`del`,`is_leaf`)
|
加入索引后再执行like和json查询,明显提速。
like执行用了136毫秒,json查询用了82.6毫秒,由此可见针对json类型使用json函数查询比like快
explain分析结果如下,两者查询扫描的行数都限定在了2931行
四、全文索引查询
因为全文索引只支持char、varchar和text,我们需要把json字段定义改一下
1
|
alter table tmp_test_course modify `outline` varchar (1024) not null default '[]'
|
添加全文索引
1
|
alter table tmp_test_course add fulltext index outline (outline);
|
现在再来用全文索引进行检索
1
2
3
4
|
select * from tmp_test_course
where `type`=5 and del=2 and is_leaf=1
and
match(outline) against ( 'jy1577683381775 jy1577683380808 jy1577683379178 jy1577683378676 jy1577683377617 jy1577683376672 jy1577683375903 jy1578385720787 jy1499916986208 jy1499917112460 jy1499917093400 jy1499917335579 jy1499917334770 jy1499917333339 jy1499917331557 jy1499917330833 jy1499917329615 jy1499917328496 jy1576922006950 jy1499916993558 jy1499916992308 jy1499917003454 jy1499917002952' )
|
耗时11.6毫秒,速度提升极其明显,可见全文索引的牛逼。
explain分析结果如下,显示只扫描了一行
结论
以下是4种情况的执行结果
全文索引: 11.6ms
联合索引:82.6ms(json)、136ms(like)
json函数查询:196ms
like查询: 248ms
结论:全文索引 > 联合索引 > json函数查询 > like查询
数据量越大,全文索引速度越明显,就10万的量,查询速度大概比直接查询快了20倍左右,如果是百万或千万级别的表,提升差距会更加大,所以有条件还是老老实实用全文索引吧
到此这篇关于mysql全文索引、联合索引、like查询、json查询速度哪个快的文章就介绍到这了,更多相关mysql 全文索引 联合索引 like查询 json查询内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/chenqionghe/p/12367268.html