当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL优化GROUP BY(松散索引扫描与紧凑索引扫描)

MySQL优化GROUP BY(松散索引扫描与紧凑索引扫描)

2017年12月12日  | 移动技术网IT编程  | 我要评论

满足group by子句的最一般的方法是扫描整个表并创建一个新的临时表,表中每个组的所有行应为连续的,然后使用该临时表来找到组并应用累积函数(如果有)。在某些情况中,mysql能够做得更好,即通过索引访问而不用创建临时表。
       为group by使用索引的最重要的前提条件是所有group by列引用同一索引的属性,并且索引按顺序保存其关键字。是否用索引访问来代替临时表的使用还取决于在查询中使用了哪部分索引、为该部分指定的条件,以及选择的累积函数。
       由于group by 实际上也同样会进行排序操作,而且与order by 相比,group by 主要只是多了排序之后的分组操作。当然,如果在分组的时候还使用了其他的一些聚合函数,那么还需要一些聚合函数的计算。所以,在group by 的实现过程中,与 order by 一样也可以利用到索引。在mysql 中,group by 的实现同样有多种(三种)方式,其中有两种方式会利用现有的索引信息来完成 group by,另外一种为完全无法使用索引的场景下使用。下面我们分别针对这三种实现方式做一个分析。

1、使用松散索引扫描(loose index scan)实现 group by

对“松散索引扫描”的定义,本人看了很多网上的介绍,都不甚明白。在此逻列如下:
定义1:松散索引扫描,实际上就是当 mysql 完全利用索引扫描来实现 group by 的时候,并不需要扫描所有满足条件的索引键即可完成操作得出结果。
定义2:优化group by最有效的办法是当可以直接使用索引来完全获取需要group的字段。使用这个访问方法时,mysql使用对关键字排序的索引的类型(比如btree索引)。这使得索引中用于group的字段不必完全涵盖where条件中索引对应的key。由于只包含索引中关键字的一部分,因此称为松散的索引扫描。
意思是索引中用于group的字段,没必要包含多列索引的全部字段。例如:有一个索引idx(c1,c2,c3),那么group by c1、group by c1,c2这样c1或c1、c2都只是索引idx的一部分。要注意的是,索引中用于group的字段必须符合索引的“最左前缀”原则。group by c1,c3是不会使用松散的索引扫描的
例如:
explain
select group_id,gmt_create
from group_message
where user_id>1
group by group_id,gmt_create;
本人理解“定义2”的例子说明
有一个索引idx(c1,c2,c3)
select c1, c2 from t1 where c1 < const group by c1, c2;
索引中用于group的字段为c1,c2
不必完全涵盖where条件中索引对应的key(where条件中索引,即为c1;c1对应的key,即为idx)
索引中用于group的字段(c1,c2)只包含索引中关键字(c1,c2,c3)的一部分,因此称为松散的索引扫描。
要利用到松散索引扫描实现group by,需要至少满足以下几个条件:
◆ 查询针对一个单表
◆ group by 条件字段必须在同一个索引中最前面的连续位置;
group by包括索引的第1个连续部分(如果对于group by,查询有一个distinct子句,则所有distinct的属性指向索引开头)。
◆ 在使用group by 的同时,如果有聚合函数,只能使用 max 和 min 这两个聚合函数,并且它们均指向相同的列。
◆ 如果引用(where条件中)到了该索引中group by 条件之外的字段条件的时候,必须以常量形式存在,但min()或max() 函数的参数例外;
   或者说:索引的任何其它部分(除了那些来自查询中引用的group by)必须为常数(也就是说,必须按常量数量来引用它们),但min()或max() 函数的参数例外。
补充:如果sql中有where语句,且select中引用了该索引中group by 条件之外的字段条件的时候,where中这些字段要以常量形式存在。
◆ 如果查询中有where条件,则条件必须为索引,不能包含非索引的字段

松散索引扫描
explain
select group_id,user_id
from group_message
where group_id between 1 and 4
group by group_id,user_id;
松散索引扫描
explain
select group_id,user_id
from group_message
where user_id>1 and group_id=1
group by group_id,user_id;
非松散索引扫描
explain
select group_id,user_id
from group_message
where abc=1
group by group_id,user_id;
非松散索引扫描
explain
select group_id,user_id
from group_message
where user_id>1 and abc=1
group by group_id,user_id;
松散索引扫描,此类查询的explain输出显示extra列的using index for group-by

下面的查询提供该类的几个例子,假定表t1(c1,c2,c3,c4)有一个索引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;

由于上述原因,不能用该快速选择方法执行下面的查询:

1、除了min()或max(),还有其它累积函数,例如:
     select c1, sum(c2) from t1 group by c1;
2、group by子句中的域不引用索引开头,如下所示:
     select c1,c2 from t1 group by c2, c3;
3、查询引用了group by部分后面的关键字的一部分,并且没有等于常量的等式,例如:
     select c1,c3 from t1 group by c1, c2;
这个例子中,引用到了c3(c3必须为组合索引中的一个),因为group by 中没有c3。并且没有等于常量的等式。所以不能使用松散索引扫描
可以这样改一下:select c1,c3 from t1 where c3='a' group by c1, c2
下面这个例子不能使用松散索引扫描
select c1,c3 from t1 where c3='a' group by c1, c2
为什么松散索引扫描的效率会很高?
答:因为在没有where 子句,也就是必须经过全索引扫描的时候, 松散索引扫描需要读取的键值数量与分组的组数量一样多,也就是说比实际存在的键值数目要少很多。而在where 子句包含范围判断式或者等值表达式的时候, 松散索引扫描查找满足范围条件的每个组的第1 个关键字,并且再次读取尽可能最少数量的关键字。

2、使用紧凑索引扫描(tight index scan)实现 group by

紧凑索引扫描实现 group by 和松散索引扫描的区别主要在于:
紧凑索引扫描需要在扫描索引的时候,读取所有满足条件的索引键,然后再根据读取出的数据来完成 group by 操作得到相应结果。
这时候的执行计划的 extra 信息中已经没有“using index for group-by”了,但并不是说 mysql 的 group by 操作并不是通过索引完成的,只不过是需要访问 where 条件所限定的所有索引键信息之后才能得出结果。这就是通过紧凑索引扫描来实现 group by 的执行计划输出信息。
在 mysql 中,mysql query optimizer 首先会选择尝试通过松散索引扫描来实现 group by 操作,当发现某些情况无法满足松散索引扫描实现 group by 的要求之后,才会尝试通过紧凑索引扫描来实现。
当 group by 条件字段并不连续或者不是索引前缀部分的时候,mysql query optimizer 无法使用松散索引扫描。
这时检查where 中的条件字段是否有索引的前缀部分,如果有此前缀部分,且该部分是一个常量,且与group by 后的字段组合起来成为一个连续的索引。这时按紧凑索引扫描。

select max(gmt_create)
from group_message
where group_id = 2
group by user_id

需读取group_id=2的所有数据,然后在读取的数据中完成group by操作得到结果。(这里group by 字段并不是一个连续索引,正好where 中group_id正好弥补缺失的索引键,又恰好是一个常量,因此使用紧凑索引扫描)
group_id user_id 这个顺序是可以使用该索引。如果连接的顺序不符合索引的“最左前缀”原则,则不使用紧凑索引扫描。

以下例子使用紧凑索引扫描

group by中有一个差距,但已经由条件user_id = 1覆盖。
explain
select group_id,gmt_create
from group_message
where user_id = 1 group by group_id,gmt_create

group by不以关键字的第1个元素开始,但是有一个条件提供该元素的常量
explain
select group_id,gmt_create
from group_message
where group_id = 1 group by user_id,gmt_create

下面的例子都不使用紧凑索引扫描
user_id,gmt_create 连接起来并不符合索引“最左前缀”原则
explain
select group_id,gmt_create
from group_message
where user_id = 1 group by gmt_create
group_id,gmt_create 连接起来并不符合索引“最左前缀”原则
explain
select gmt_create
from group_message
where group_id=1 group by gmt_create;

 3、使用临时表实现 group by

mysql query optimizer 发现仅仅通过索引扫描并不能直接得到 group by 的结果之后,他就不得不选择通过使用临时表然后再排序的方式来实现 group by了。在这样示例中即是这样的情况。 group_id 并不是一个常量条件,而是一个范围,而且 group by 字段为 user_id。所以 mysql 无法根据索引的顺序来帮助 group by 的实现,只能先通过索引范围扫描得到需要的数据,然后将数据存入临时表,然后再进行排序和分组操作来完成 group by。
explain
select group_id
from group_message
where group_id between 1 and 4
group by user_id;
示例数据库文件

-- --------------------------------------------------------
-- host:             127.0.0.1
-- server version:        5.1.57-community - mysql community server (gpl)
-- server os:          win32
-- heidisql version:       7.0.0.4156
-- date/time:          2012-08-20 16:52:10
-- --------------------------------------------------------

/*!40101 set @old_character_set_client=@@character_set_client */;
/*!40101 set names utf8 */;
/*!40014 set foreign_key_checks=0 */;

-- dumping structure for table test.group_message
drop table if exists `group_message`;
create table if not exists `group_message` (
 `id` int(10) unsigned not null auto_increment,
 `group_id` int(10) unsigned default null,
 `user_id` int(10) unsigned default null,
 `gmt_create` timestamp not null default current_timestamp,
 `abc` int(11) not null default '0',
 primary key (`id`),
 key `group_id_user_id_gmt_create` (`group_id`,`user_id`,`gmt_create`)
) engine=myisam auto_increment=27 default charset=utf8;

-- dumping data for table test.group_message: 0 rows
delete from `group_message`;
/*!40000 alter table `group_message` disable keys */;
insert into `group_message` (`id`, `group_id`, `user_id`, `gmt_create`, `abc`) values
	(1, 1, 1, '2012-08-20 09:25:35', 1),
	(2, 2, 1, '2012-08-20 09:25:39', 1),
	(3, 2, 2, '2012-08-20 09:25:47', 1),
	(4, 3, 1, '2012-08-20 09:25:50', 2),
	(5, 3, 2, '2012-08-20 09:25:52', 2),
	(6, 3, 3, '2012-08-20 09:25:54', 0),
	(7, 4, 1, '2012-08-20 09:25:57', 0),
	(8, 4, 2, '2012-08-20 09:26:00', 0),
	(9, 4, 3, '2012-08-20 09:26:02', 0),
	(10, 4, 4, '2012-08-20 09:26:06', 0),
	(11, 5, 1, '2012-08-20 09:26:09', 0),
	(12, 5, 2, '2012-08-20 09:26:12', 0),
	(13, 5, 3, '2012-08-20 09:26:13', 0),
	(14, 5, 4, '2012-08-20 09:26:15', 0),
	(15, 5, 5, '2012-08-20 09:26:17', 0),
	(16, 6, 1, '2012-08-20 09:26:20', 0),
	(17, 7, 1, '2012-08-20 09:26:23', 0),
	(18, 7, 2, '2012-08-20 09:26:28', 0),
	(19, 8, 1, '2012-08-20 09:26:32', 0),
	(20, 8, 2, '2012-08-20 09:26:35', 0),
	(21, 9, 1, '2012-08-20 09:26:37', 0),
	(22, 9, 2, '2012-08-20 09:26:40', 0),
	(23, 10, 1, '2012-08-20 09:26:42', 0),
	(24, 10, 2, '2012-08-20 09:26:44', 0),
	(25, 10, 3, '2012-08-20 09:26:51', 0),
	(26, 11, 1, '2012-08-20 09:26:54', 0);
/*!40000 alter table `group_message` enable keys */;
/*!40014 set foreign_key_checks=1 */;
/*!40101 set character_set_client=@old_character_set_client */;

参考文献
1、mysql如何优化group by
2、详解mysql分组查询group by实现原理
3、松散的索引扫描(loose index scan)
4、mysql学习笔记

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网