mysql模糊查询优化_mysql模糊查询的优化方法--亲自实践

数据有4W多条,不多,但是模糊查询

起来特别慢。

1,尝试过用 select * from (select * from a union all select * from b。。。很多表union) as tempTable where name like "%a%" and person_id like "%1%",发现太慢了。。

2,尝试使用具体的字段,而不是*,即

select id,name,person_id from (select id,name,person_id from a

union all select id,name,person_id from b。。。很多表union) as tempTable where name like "%a%" and person_id like "%1%",虽然快了点儿,但是不是特别快。。

3,尝试把where放在每一个select语句中,不需要汇聚成一个表了,即少了别名:

select id,name,person_id from a where name like "%a%" and person_id like "%1%"

union all select id,name,person_id from b where name like "%a%" and person_id like "%1%", 卧槽,出乎意料地快!解决了问题。

4,mybatis配置文件如下:

模糊查询的总数:

select sum(c) from

(

separator="union">select count(1) as c from ${item}

and person_id like '%${faceData.personId}%'

and name like '%${faceData.name}%'

) as faceCount

模糊查询的详细:

separator="union">(select  from ${item}

person_id like '%${faceData.personId}%'

and name like '%${faceData.name}%'

)

limit #{start},#{limit}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值