上周的mysql官网上提交了一个bug,原因为在我们的一个分页sql中出现了全表扫描的情况,这对于我们来说是不可接受的:
+—-+————-+————+——–+———————+———+———+——+——–+—————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra
|+—-+————-+————+——–+———————+———+———+——+——–+—————-+
| 1 | primary | <derived2> | all | null | null | null | null | 9 | |
| 1 | primary | t | eq_ref | primary | primary | 8 | g.id | 1 | |
| 2 | derived | g | all | ind_group_type_time | null | null | null | 652469 | using filesort
|+—-+————-+————+——–+———————+———+———+——+——–+——–
3 rows in set (0.54 sec)
可以看到在内查询中即使加入了force index 也没有上查询走上索引,而是走了全表扫,如果将其单独拿出来执行,而不做关联查询的话,是可以用上覆盖索引:
+—-+————-+——-+——-+———————+———————+——–
| id | select_type | table | type | possible_keys | key | key_len | ref | rows ||
| 1 | simple | g | range | ind_group_type_time | ind_group_type_time | 9 | null | 326235 | using where; using index; using filesort |
+—-+————-+——-+——-+———————+———————+——-
在和官网的valeriy kravchuk多次沟通后,最终认为这是一个bug;最后解决的办法是调整索引,来避免排序的,这样就能够使用覆盖索引索引:
因此大家对这种类型的分页语句一定多加注意,由于排序导致全表扫描的问题;