欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql5.6.19下子查询为什么无法使用索引

程序员文章站 2024-02-29 18:48:40
表结构很简单 create table `oplogs` ( `id` int(10) unsigned not null auto_increment, `...

表结构很简单

create table `oplogs` (
`id` int(10) unsigned not null auto_increment,
`kind` varchar(45) not null default '',
`op` varchar(100) not null,
`user` varchar(25) not null default '',
`ip` varchar(16) not null default '',
`updatetime` timestamp not null default current_timestamp on update current_timestamp,
primary key (`id`)
) engine=myisam auto_increment=34896 default charset=utf8 row_format=fixed

结果有34895条

sql查询语句为:

select a.id,a.kind,a.op,a.user,a.ip,a.updatetime from oplogs as a inner join(select id from oplogs where 1 order by id desc limit 0,20) as b using(id)

在5.6.19情况下

用php读取内容并显示

首次显示结果需要3.5秒

但是同样的配置

在linux 下 5.5.38下

只需要0.7秒左右

很奇怪的情况

然后在5.6下执行了explain 结果如下:

mysql5.6.19下子查询为什么无法使用索引

而同样的数据 explain 结构如下:

mysql5.6.19下子查询为什么无法使用索引

比较结果很明显 主要在于对oplogs进行排序时,rows行变化太大了,一个进行了完整的遍历,第一个使用了索引,造成差距过大,不过原因尚未找到,有哪位知道呢?