LIKE 模糊查询优化
1.使用LOCATE 代替 LIKE
2.使用instr
<select id="selectByNameOrAddress" parameterType="java.lang.String" resultMap="BaseResultMap">
select id,name,address,uniform_credit_code,create_user_id,name_pinyin,address_pinyin
from t_company
where LOCATE(#{serchValue},name)>0 or locate(#{serchValue},address)>0
</select>
3.使用全文索引 FULL TEXT布尔全文检索IN BOOLEAN MODE 及 默认为自然语言全文检索 IN NATURAL LANGUAGE MODE
<select id="selectByNamePinyinOrAddressPinyin" parameterType="java.lang.String" resultMap="BaseResultMap">
select
id,name,address,uniform_credit_code,create_user_id,name_pinyin,address_pinyin
from t_company
where match(name_pinyin,address_pinyin)against(concat(#{searchValue},'*') in boolean mode)
</select>

+必须包含 -必须不包含 >提高该词的相关性,查询靠前 <降低该词的相关性,查询的结果靠后 * 通配符
4.聚合函数 count()
1)给id加唯一索引(最快 全部命中)
2)使用explain select * from table;统计条数(最快 缺少命中)
本文介绍如何优化数据库中LIKE模糊查询的效率,包括使用LOCATE和INSTR函数替代LIKE,利用全文索引进行布尔全文检索,以及通过聚合函数COUNT()进行优化。适用于需要提升大量模糊查询性能的场景。
1519

被折叠的 条评论
为什么被折叠?



