我有一个 innodb 表,表上的查询如下所示。
SELECT *
FROM x
WHERE now() BETWEEN a AND b
我在 (a,b) 上创建了复合索引,查询返回大约 4k 行,而表中的总行数约为 700k。
但是,当我获取执行计划的EXPLAIN
时,我发现查询没有使用预期的索引。因为估计的行
约为360k,远大于实际值。
我知道就像许多帖子(例如 Why the rows returns by "explain" is not equal to count()? )所解释的那样,EXPLAIN
只能得到粗略的估计。但FORCE INDEX
解决方案非常棘手,并且可能在未来带来潜在的性能风险。
有什么方法可以让MySQL得到更准确的估计(当前的大了90倍)?谢谢。
最佳答案
InnoDB 仅保留表的近似行数。 SHOW TABLE STATUS
的文档对此进行了解释:
- Rows
The number of rows. Some storage engines, such as MyISAM, store the exact count. For other storage engines, such as InnoDB, this value is an approximation, and may vary from the actual value by as much as 40 to 50%.
我认为没有任何方法可以让 InnoDB 保持准确的行计数,这不是它的工作原理。
关于MySQL Innodb 由于行估计极其错误而无法使用索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521323/