Mysql时间范围查询不走索引问题

文章目录
  1. 1. 问题发现
  2. 2. 解决办法
  3. 3. 总结

问题发现

产线有个表,数据大概有3千多万,表上有针对create_time添加索引。偶然有个业务方传输的数据有问题,需要修一下一段时间范围的数据,时间范围大概是4个月左右。

这样我就写了一个SQL,这样的:

1
select count(*) from file_info where create_time > '2022-04-01 00:00:00' and create_time < '2022-08-01 00:00:00' and app_code = 'fxxx';

查询之前看了一下查询计划,长这样:
20230314184806

可以看到上面的图看到没有走索引,但是显示了可能得索引是create_time。

为什么没有走索引呢?上网查资料,找到这个文章:https://blog.csdn.net/wo541075754/article/details/123073297

解决办法

按月份先执行explain,查看时间范围内的数据量大小,走没走索引。按月进行数据修复

1
2
3
4
select count(*) from file_info where create_time > '2022-04-01 00:00:00' and create_time < '2022-05-01 00:00:00' and app_code = 'fxxx';
select count(*) from file_info where create_time > '2022-05-01 00:00:00' and create_time < '2022-06-01 00:00:00' and app_code = 'fxxx';
select count(*) from file_info where create_time > '2022-06-01 00:00:00' and create_time < '2022-07-01 00:00:00' and app_code = 'fxxx';
select count(*) from file_info where create_time > '2022-07-01 00:00:00' and create_time < '2022-08-01 00:00:00' and app_code = 'fxxx';

缩短时间之后的执行计划如下:

20230314185615

打完收工

总结

总结:虽然在create_time字段上添加了索引,但是否会走索引还需要看数据量的情况。如果根据查询条件查询到数据的结果数量小于总数量的五分之一,则会走索引,否则会走全表扫描。

因此,在进行范围查询时,比如>、< 、>=、<=等,如果数据量过大的话where语句的条件虽然添加了索引,但也有可能会进行全表扫描。所以,在查询时查询的范围要考虑进行限制或其他方式进行拆分。