Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-07 16:28:50
Original
948 people have browsed it

表结构很简单 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` ti

表结构很简单

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
Copy after login

结果有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)
Copy after login

在5.6.19情况下

用php读取内容并显示

首次显示结果需要3.5秒

但是同样的配置

在linux 下 5.5.38下

只需要0.7秒左右

很奇怪的情况

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

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template