mysql删除和更新不走索引

假设a表index字段设置索引,修改删除都需要关联表b的index字段,得出下列三个sql

// 修改
update a set status=1 where index in (select index from b where b.id = 1)
// 删除
delete from a where index in (select index from b where b.id = 1)

当表数据过大时,发现并竟然全表扫描, 使用select同样复制走了索引,经过查阅资料,此处应当使用join方法连接:

// 修改
update a join b on a.index=b.index
set a.status=1
where b.id=1
// 删除
delete a from a
join b on a.index = b.index
where b.id = 1

修改后查看索引使用,完美解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值