mysql查询数据过多不显示_mysql数据检索过多导致索引没有生效的问题

查询的时候,在数据量过多时出现索引失效的情况,请问是什么原因?

具体情况,mysql版本5.7.7,ad_stat_day表总数据在250W左右,day_time字段上有建立索引

-- 25号至30号总数据

mysql> select count(*) from ad_stat_day where dayTime BETWEEN '2017-10-25' and '2017-10-30';

+----------+

| count(*) |

+----------+

| 107063 |

+----------+

1 row in set

-- 只查id的情况下会走索引

mysql> EXPLAIN select id from ad_stat_day where dayTime BETWEEN '2017-10-25' and '2017-10-30';

+----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+--------------------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+--------------------------+

| 1 | SIMPLE | ad_stat_day | NULL | range | nk_day_time | nk_day_time | 4 | NULL | 189566 | 100 | Using where; Using index |

+----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+--------------------------+

1 row in set

-- 查id,fee就不走索引了

mysql> EXPLAIN select id,fee from ad_stat_day where dayTime BETWEEN '2017-10-25' and '2017-10-30';

+----+-------------+-------------+------------+------+---------------+------+---------+------+--------+----------+-------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+-------------+------------+------+---------------+------+---------+------+--------+----------+-------------+

| 1 | SIMPLE | ad_stat_day | NULL | ALL | nk_day_time | NULL | NULL | NULL | 646016 | 27.18 | Using where |

+----+-------------+-------------+------------+------+---------------+------+---------+------+--------+----------+-------------+

1 row in set

--缩小条件范围,查询id,fee也会走索引

mysql> EXPLAIN select id,fee from ad_stat_day where dayTime BETWEEN '2017-10-27' and '2017-10-30';

+----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+----------------------------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+----------------------------------+

| 1 | SIMPLE | ad_stat_day | NULL | range | nk_day_time | nk_day_time | 4 | NULL | 124092 | 100 | Using index condition; Using MRR |

+----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+----------------------------------+

1 row in set

求大佬告知下mysql是根据什么情况来选择使用索引和不使用索引的?

这次范围扫描数据(189566行)不到表总数的10%,而加上一个字段就成了(646016行)占到表总数的25%来了,这个646016行数据mysql是怎么算出来的?还有mysql有明确的配置来说明超过某个阀值(百分比)就不使用索引了吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值