MySQL查询优化问题

存在batch_table和detail_table两张表,现batch_table表有900k数据,detail_table表有1G数据。如何提升联表查询速度。

create batch_table(
id varchar(200),
name varchar(200),
send_status varchar(2) on comment '01-发送成功,02-发送失败,03-暂存,04-其他'
);

create detail_table(
id varchar(200),
detail_name varchar(200),
batch_id varchar(200),
org_id varchar(200),
com_no varchar(200) comment '单位编号',
valid_status varchar(1) comment '1-有效, 2-无效'
);

查询语句

select
b.name, d.detail_name,d.org_id
from 
batch_table b left join detail_batch d 
on b.id = d.batch_id
<where>
d.valid_status = '1' and b.send_status not in ('03','04') 
<if test="orgId!=null">
	and org_id = #{orgId}
</if>
<if test="detailName !=null">
	and detail_name like concat('%',#{detailName},'%')
</if>
<if test="comNo !=null">
	and com_no = #{comNo}
</if>
</where> 

comNo 为必填。给comNo加索引的情况下,执行这个查询语句用时34s。加了索引,用时大概60ms。但如果detailName不为空,则索引失效。暂存数据大概有500w条,在不删数据的情况下,如何提高该联表查询的速度。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值