mysql between and 索引_求助:select between 用到了索引依然很慢

本帖最后由 yisildeai 于 2012-3-23 11:09 编辑

explain SELECT id, resaleAvgPrice, resaleAvgRate FROM stats_estate WHERE statsMonth=date_format(now(), '%Y-%m') AND id BETWEEN 0 AND 49999;

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

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

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

|  1 | SIMPLE      | stats_estate | ref  | PRIMARY,statsMonth_idx | statsMonth_idx | 23      | const | 265696 | Using where |

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

用主键_索引

explain SELECT id, resaleAvgPrice, resaleAvgRate FROM stats_estate  FORCE INDEX(PRIMARY) WHERE id BETWEEN 0 AND 49999  and statsMonth=date_format(now(), '%Y-%m');+----+-------------+--------------+-------+---------------+---------+---------+------+--------+-------------+

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

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

|  1 | SIMPLE      | stats_estate | range | PRIMARY       | PRIMARY | 27      | NULL | 722579 | Using where |

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

索引分布情况:

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

| stats_estate |          0 | PRIMARY        |            1 | id          | A         |      361289 |

| stats_estate |          0 | PRIMARY        |            2 | statsMonth  | A         |     1445158 |

| stats_estate |          1 | statsMonth_idx |            1 | statsMonth  | A         |          18 |

为什么 这个 statsmonth_idx  这个索引 基数这么小.. 用索引扫描 扫描的行数 还少呢... 而 利用主键查询 却是接近于全表扫描呢..  有点不太明白了.. engine 是innodb的... second_index 指向主键 所以索引的不重复字段才这么少吧..那么 为什么 主键扫描却这么慢呢... 应该如何建立索引..才使得此sql 提升速度 请各位哥 帮忙看下...谢谢...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值