为什么 InnoDB 对 COUNT(*) 进行全扫描?
在包含 500 万行的表中,SELECT count(*) FROM table
在 MyISAM 中是即时的,但在 InnoDB 中则需要几秒钟。
为什么会这样?为什么他们没有像 MyISAM 那样优化 InnoDB 中的计数?
谢谢。
In a table with 5 millions rows, a SELECT count(*) FROM table
would be instant in MyISAM but would take several seconds in InnoDB.
Why is this that way? Why haven't they optimise count in InnoDB like MyISAM?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是实现上的差异。 InnoDB 支持事务,因此它必须根据表的事务一致视图来计算行数。由于 MyISAM 不支持 ACID 属性,因此如果插入一行,则会为每个人插入该行,因此它只能更新存储引擎中保留的计数。
It's a difference in implementation. InnoDB supports transactions and therefore it has to count the rows based on your transactionally consistent view of the table(s). Since MyISAM doesn't support ACID properties, if a row is inserted, it's inserted for everyone and therefore it can just update a count it keeps within the storage engine.
MyIsam 的行数存储在某处,因此查询几乎是即时的,InnoDB 必须扫描表才能获得完整的计数。
如果您有条件计数(例如:“SELECT COUNT(*) WHERE CUSTOMER =4”),则两者都必须进行扫描,在这种情况下没有太大区别
来源:
http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/
MyIsam has the number of rows stored somewhere and so the query is almost instant, InnoDB has to scan the table to get the full count.
If you have a conditional count ( Ex: "SELECT COUNT(*) WHERE CUSTOMER =4" ) both have to do the scan and there is not much difference in this case
Source:
http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/