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

简单谈谈MySQL的loose index scan

程序员文章站 2024-02-22 21:57:18
众所周知,innodb采用iot(index organization table)即所谓的索引组织表,而叶子节点也就存放了所有的数据,这就意味着,数据总是按照某种顺序存储...

众所周知,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:

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

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

5.0之后group by在特定条件下可能使用到loose index scan,

create table log_table (
id int not null primary key,
log_machine varchar(20) not null,
log_time datetime not null
) engine=innodb default charset=utf8;
create index ix_log_machine_time on log_table (log_machine, log_time);

1

select max(log_time) from log_table;
select max(log_time) from log_table where log_machine in ('machine 1');

这两条sql都只需一次index seek便可返回,源于索引的有序排序,优化器意识到min/max位于最左/右块,从而避免范围扫描;
extra显示select tables optimized away ;
2

复制代码 代码如下:
select max(log_time) from log_table where log_machine in (‘machine 1','machine 2','machine 3','machine 4');

执行计划type 为range(extra显示using where; using index),即执行索引范围扫描,先读取所有满足log_machine约束的记录,然后对其遍历找出max value;
改进

复制代码 代码如下:
select max(log_time) from log_table where log_machine in (‘machine 1','machine 2','machine 3','machine 4')  group by log_machine order by 1 desc limit 1;

这满足group by选择loose index scan的要求,执行计划的extra显示using index for group-by,执行效果等值于
select max(log_time) from log_table where log_machine in (‘machine 1')
union
select max(log_time) from log_table where log_machine in (‘machine 2')
…..

即对每个log_machine执行loose index scan,rows从原来的82636下降为16(该表总共1,000,000条记录)。

group by何时使用loose index scan?

适用条件:

1  针对单表操作
2  group by使用索引的最左前缀列
3  只支持聚集函数min()/max()
4  where条件出现的列必须为=constant操作 , 没出现在group by中的索引列必须使用constant
5  不支持前缀索引,即部分列索引 ,如index(c1(10))
执行计划的extra应该显示using index for group-by
假定表t1有个索引idx(c1,c2,c3)

select c1, c2 from t1 group by c1, c2;
select distinct c1, c2 from t1;
select c1, min(c2) from t1 group by c1;
select c1, c2 from t1 where c1 < const group by c1, c2;
select max(c3), min(c3), c1, c2 from t1 where c2 > const group by c1, c2;
select c2 from t1 where c1 < const group by c1, c2;
select c1, c2 from t1 where c3 = const group by c1, c2
select c1, c3 from t1 group by c1, c2;--无法使用松散索引

而select c1, c3 from t1  where c3= const group by c1, c2;则可以

紧凑索引扫描tight index scan
group by在无法使用loose index scan,还可以选择tight,若两者都不可选,则只能借助临时表;
扫描索引时,须读取所有满足条件的索引键,要么是全索引扫描,要么是范围索引扫描;
group by的索引列不连续;或者不是从最左前缀开始,但是where条件里出现最左列;

select c1, c2, c3 from t1 where c2 = 'a' group by c1, c3;
select c1, c2, c3 from t1 where c1 = 'a' group by c2, c3;

5.6的改进
事实上,5.6的index condition push down可以弥补loose index scan缺失带来的性能损失。
key(age,zip)

mysql> explain select name from people where age between 18 and 20 and zip in (12345,12346, 12347);
+----+-------------+--------+-------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra    |
+----+-------------+--------+-------+---------------+------+---------+------+-------+-------------+
| 1 | simple   | people | range | age      | age | 4    | null | 90556 | using where |
+----+-------------+--------+-------+---------------+------+---------+------+-------+-------------+
1 row in set (0.01 sec)

根据key_len=4可以推测出sql只用到索引的第一列,即先通过索引查出满足age (18,20)的行记录,然后从server层筛选出满足zip约束的行;
pre-5.6,对于复合索引,只有当引导列使用"="时才有机会在索引扫描时使用到后面的索引列。

mysql> explain select name from people where age=18 and zip in (12345,12346, 12347);
+----+-------------+--------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra    |
+----+-------------+--------+-------+---------------+------+---------+------+------+-------------+
| 1 | simple   | people | range | age      | age | 8    | null |  3 | using where |
+----+-------------+--------+-------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

对比一下查询效率

mysql> select sql_no_cache name from people where age=19 and zip in (12345,12346, 12347);
+----------------------------------+
| name               |
+----------------------------------+
| 888ba838661aff00bbbce114a2a22423 |
+----------------------------------+
1 row in set (0.06 sec)
mysql> select sql_no_cache name from people where age between 18 and 22 and zip in (12345,12346, 12347);
+----------------------------------+
| name               |
+----------------------------------+
| ed4481336eb9adca222fd404fa15658e |
| 888ba838661aff00bbbce114a2a22423 |
+----------------------------------+
2 rows in set (1 min 56.09 sec)

对于第二条sql,可以使用union改写,

mysql> select name from people where age=18 and zip in (12345,12346, 12347)
  -> union all
  -> select name from people where age=19 and zip in (12345,12346, 12347)
  -> union all
  -> select name from people where age=20 and zip in (12345,12346, 12347)
  -> union all
  -> select name from people where age=21 and zip in (12345,12346, 12347)
  -> union all
-> select name from people where age=22 and zip in (12345,12346, 12347);

而mysql5.6引入了index condition pushdown,从优化器层面解决了此类问题。