引言

文章开篇前,先问大家一个问题:delete in子查询,是否会走索引呢?很多伙伴第一感觉就是:会走索引。最近我们有个生产问题,就跟它有关。本文将跟大家一起探讨这个问题,并附上优化方案。

问题复现

mysql版本是5.7,假设当前有两张表accountold_account,表结构如下:

create table `old_account` (
  `id` int(11) not null auto_increment comment '主键id',
  `name` varchar(255) default null comment '账户名',
  `balance` int(11) default null comment '余额',
  `create_time` datetime not null comment '创建时间',
  `update_time` datetime not null on update current_timestamp comment '更新时间',
  primary key (`id`),
  key `idx_name` (`name`) using btree
) engine=innodb auto_increment=1570068 default charset=utf8 row_format=redundant comment='老的账户表';
create table `account` (
  `id` int(11) not null auto_increment comment '主键id',
  `name` varchar(255) default null comment '账户名',
  `balance` int(11) default null comment '余额',
  `create_time` datetime not null comment '创建时间',
  `update_time` datetime not null on update current_timestamp comment '更新时间',
  primary key (`id`),
  key `idx_name` (`name`) using btree
) engine=innodb auto_increment=1570068 default charset=utf8 row_format=redundant comment='账户表';

执行的sql如下:

delete from account where name in (select name from old_account);

我们explain执行计划走一波,

explain结果可以发现:先全表扫描 account,然后逐行执行子查询判断条件是否满足;显然,这个执行计划和我们预期不符合,因为并没有走索引

但是如果把delete换成select,就会走索引。如下:

为什么select in子查询会走索引,delete in子查询却不会走索引呢?

原因分析

select in子查询语句跟delete in子查询语句的不同点到底在哪里呢?

我们执行以下sql看看

explain select * from account where name in (select name from old_account);
show warnings;

show warnings 可以查看优化后,最终执行的sql

结果如下:

select `test2`.`account`.`id` as `id`,`test2`.`account`.`name` as `name`,`test2`.`account`.`balance` as `balance`,`test2`.`account`.`create_time` as `create_time`,`test2`.`account`.`update_time` as `update_time` from `test2`.`account` 
semi join (`test2`.`old_account`)
where (`test2`.`account`.`name` = `test2`.`old_account`.`name`)

可以发现,实际执行的时候,mysql对select in子查询做了优化,把子查询改成join的方式,所以可以走索引。但是很遗憾,对于delete in子查询,mysql却没有对它做这个优化。

优化方案

那如何优化这个问题呢?通过上面的分析,显然可以把delete in子查询改为join的方式。我们改为join的方式后,再explain看下:

可以发现,改用join的方式是可以走索引的,完美解决了这个问题。

实际上,对于update或者delete子查询的语句,mysql官网也是推荐join的方式优化

其实呢,给表加别名,也可以解决这个问题哦,如下:

explain delete a from account as a where a.name in (select name from old_account)

为什么加个别名就可以走索引了呢?

what?为啥加个别名,delete in子查询又行了,又走索引了?

我们回过头来看看explain的执行计划,可以发现extra那一栏,有个loosescan

loosescan是什么呢? 其实它是一种策略,是semi join子查询的一种执行策略。

因为子查询改为join,是可以让delete in子查询走索引;加别名呢,会走loosescan策略,而loosescan策略,本质上就是semi join子查询的一种执行策略。

因此,加别名就可以让delete in子查询走索引啦!

总结

本博文分析了delete in子查询不走索引的原因,并附上解决方案。delete in在日常开发,是非常常见的,平时大家工作中,需要注意一下。同时呢,建议大家工作的时候,写sql的时候,尽量养成一个好习惯,先用explain分析一下sql,更多关于delete in子查询索引的资料请关注www.887551.com其它相关文章!