Home  >  Article  >  Database  >  MySQL的loose index scan_MySQL

MySQL的loose index scan_MySQL

WBOY
WBOYOriginal
2016-05-27 13:46:20867browse

众所周知,InnoDB采用IOT(index organization table)即所谓的索引组织表,而叶子节点也就存放了所有的数据,这就意味着,数据总是按照某种顺序存储的。所以问题来了,如果是这样一个语句,执行起来应该是怎么样的呢?语句如下:

 

select count(distinct a) from table1;

 

列a上有一个索引,那么按照简单的想法来讲,如何扫描呢?很简单,一条一条的扫描,这样一来,其实做了一次索引全扫描,效率很差。这种扫描方式会扫描到很多很多的重复的索引,这样说的话优化的办法也是很容易想到的:跳过重复的索引就可以了。于是网上能搜到这样的一个优化的办法:

 

select count(*) from (select distinct a from table1) t;

 

从已经搜索到的资料看,这样的执行计划中的extra就从using index变成了using index for group-by。

 

但是,但是,但是,好在我们现在已经没有使用5.1的版本了,大家基本上都是5.5以上了,这些现代版本,已经实现了loose index scan:

MySQL的loose index scan_MySQL

很好很好,就不需要再用这种奇技淫巧去优化SQL了。

 

文档里关于group by这里写的有点意思,说是最大众化的办法就是进行全表扫描并且创建一个临时表,这样执行计划就会难看的要命了,肯定有ALL和using temporary table了。

Statement:
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