查询背景有一个表tmp_test_course大概有10万条记录,然后有个json字段叫outline,存了一对多关系(保存了多个编码,例如jy1577683381775)
我们需要在这10万条数据中检索特定类型的数据,目标总数据量:2931条
SELECT COUNT(*) FROM tmp_test_course WHERE `type`=5 AND del=2 AND is_leaf=1
文章插图
我们在限定为上面类型的同时,还得包含下面任意一个编码(也就是OR查询)
jy1577683381775jy1577683380808jy1577683379178jy1577683378676jy1577683377617jy1577683376672jy1577683375903jy1578385720787jy1499916986208jy1499917112460jy1499917093400jy1499917335579jy1499917334770jy1499917333339jy1499917331557jy1499917330833jy1499917329615jy1499917328496jy1576922006950jy1499916993558jy1499916992308jy1499917003454jy1499917002952
下面分别列出4种方式查询outline字段,给出相应的查询时间和扫描行数一、like查询耗时248毫秒
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函数查询使用函数JSON_SEARCH,更多函数请查看MySQL官方文档
可以看到,查询耗时196毫秒,速度稍微快了一点
SELECT * FROM tmp_test_course WHERE `type`=5 AND del=2 AND is_leaf=1AND (JSON_SEARCH(outline, 'one', 'jy1577683381775') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1577683380808') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1577683379178') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1577683378676') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1577683377617') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1577683376672') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1577683375903') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1578385720787') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499916986208') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917112460') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917093400') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917335579') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917334770') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917333339') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917331557') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917330833') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917329615') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917328496') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1576922006950') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499916993558') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499916992308') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917003454') IS NOT NULL ORJSON_SEARCH(outline, 'one', 'jy1499917002952') IS NOT NULL)
文章插图
EXPLAIN分析结果如下,还是全表扫描
文章插图
三、联合索引查询下面为该表建立一个联合索引(本来想建一个type-del-is_leaf-outline的索引,但是outline字段太长限制,所以只加type-del-is_leaf的联合索引
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快
推荐阅读
- 如何判定网站是否被搜索引擎降权
- 搜索引擎抓取信息的原理
- 什么是搜索引擎营销网站推广?
- 为什么搜索引擎喜欢h1标签
- 什么是搜索引擎友好的内容
- MySQL千万级大表优化,看这一篇就忘不掉了
- 一条MySQL报警的分析思路
- MySQL 8.0 InnoDB无锁化设计的日志系统
- 搜索引擎简史
- 解析影响搜索引擎蜘蛛访问网站的原因