MySQL获取innodb表的行数
我有一个使用 innodb 的表。我知道该表大约有 8900 万行。使用
SELECT COUNT(*) FROM table;
运行大约需要五分钟。我知道 innodb 没有针对无条件 COUNT(*) 查询进行优化。如何重构查询以更快地进行计数?只需添加 WHERE 1=1 即可,还是我需要查询特定字段?
我知道我可以使用 SHOW TABLE STATUS 获取大致的行数,但我想获取 PHP 脚本中的值,并且使用该方法似乎有很多需要挖掘的内容。
I have a table using innodb. I know the table has roughly 89 million rows. Using
SELECT COUNT(*) FROM table;
takes about five minutes to run. I know that innodb is not optimized for unconditional COUNT(*) queries. How can I restructure the query to give me a count faster? Would just adding WHERE 1=1 work, or do I need to query a specific field?
I know I can get the approximate number of rows using SHOW TABLE STATUS, but I want to get the value in a PHP script, and it seems like there is a lot to dig through using that method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您对估计的数字感到满意,并且只是不想混乱从 PHP 运行
SHOW TABLE STATUS
,您可以使用 information_schema DB:If you are OK with the estimated number and just don't want to mess with running
SHOW TABLE STATUS
from PHP, you can use the information_schema DB:如果您对大概的记录数没问题,则可以使用“
explain
”的输出。代码的简化版本是
If you are ok with approximate number of records, you can use output of "
explain
".Simplified verion of the code is
如果该表被频繁读取且不经常更新,您可能需要考虑创建一个通过 对表进行更改时触发。
If the table is read frequently and updated infrequently, you may want to consider creating a statistics table that is updated via triggers when making changes to the table.
mysql_num_rows 可能对您有用。
mysql_num_rows may be useful to you.