多字模糊查询优化

tech2025-05-05  5

举个栗子

 查询标题或者 内容包含 A 的数据

传统写法如select * from table1 t where (t.title like '%A%') or (t.neirong like '%A%');

调整之后写法 select * from table1 t where t.title||t.neirong like '%A%';

 

where instr(t.title,'A') > 0 or instr(t.neirong,'A')>0

where instr((t.title||t.neirong),'A') > 0 ; 

 

源sql

and (hi.name like concat('%', #{searchValue}, '%') or hi.depts like concat('%', #{searchValue}, '%') or hi.alias like concat('%', #{searchValue}, '%'))

调整后sql

and LOCATE(#{searchValue},CONCAT(hi.name,hi.alias,hi.depts)) > 0

调整后可根据模糊查询的位置进行排序 优先显示第一例匹配数据,性能....... 缺点...如字段太长......

                IFNULL(a.`comment`,'') `comment`  该字段如不写引号 会发生报错  token COMMENT

com.alibaba.druid.sql.parser.ParserException: syntax error, error in :' '') comment, a.pics, a.add_time AS', expect RPAREN, actual COMMENT pos 104, line 1, column 97, token COMMENT  

最新回复(0)