日期索引 - 长范围搜索所有行,不包括小行
为什么当我切换到 1 年范围时 MySQL 会搜索所有行?
--Table dates
id (int)
date (timestamp)
value (varchar)
PRIMARY(id), date_index(date)
1750 行
正在执行EXPLAIN SELECT * FROM days WHERE date BETWEEN '2011-04-27' AND '2011-04-28'
行列显示18行 .
如果我增加或减少 BETWEEN 范围(例如 1 年),行 列将显示 1750 行。
解释 SELECT * FROM 日期,其中日期位于“2011-04-27”和“2012-04-28”之间
解释 SELECT * FROM 日期,其中日期位于“2010-04-27”和“2011-04-28”之间
Why MySQL search all rows when I switch to a 1 year range?
--Table dates
id (int)
date (timestamp)
value (varchar)
PRIMARY(id), date_index(date)
1750 rows
ExecutingEXPLAIN SELECT * FROM dates WHERE date BETWEEN '2011-04-27' AND '2011-04-28'
The rows column display 18 rows.
If I increase or decrease the BETWEEN range - 1 year for example - the rows column display 1750 rows.
EXPLAIN SELECT * FROM dates WHERE date BETWEEN '2011-04-27' AND '2012-04-28'
EXPLAIN SELECT * FROM dates WHERE date BETWEEN '2010-04-27' AND '2011-04-28'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
优化器根据多种因素(包括数据量/分布)构建查询计划。我最好的猜测是,您没有超过一年的数据,或者使用当年数据的索引不会使用比表总大小少很多的行。
如果这听起来不对,您可以发布以下输出:
我写的这篇文章也展示了优化器如何工作的一些示例:什么才是好的 MySQL 索引?第 2 部分:基数
The optimizer builds the query plan depending on several things including the amount/distribution of the data. My best guess would be that you don't have much more than a year's data or that using the index for the year's worth of data wouldn't use many less rows than the total table size.
If that doesn't sound right can you post up the output of:
This article I wrote shows some examples of how the optimizer works too: What makes a good MySQL index? Part 2: Cardinality