在 MySQL 中获取表的行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1018336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:32:53  来源:igfitidea点击:

Getting row count for a table in MySQL?

sqlmysqlcountrow

提问by Quassnoi

Can anyone tell me which is he better appraoch in MySQL whin it comes to finding the row count of a table: is it better to do

任何人都可以告诉我在查找表的行数时,他在 MySQL 中哪个更好的方法:这样做更好吗?

SELECT COUNT(*) FROM TABLE_NAME

or to look up the row count in the table TABLEin the INFORMATION_SCHEMA?

或查找表TABLE中的行数INFORMATION_SCHEMA

This would be a frequent operation for calculating pagination counts for pages?

这将是计算页面分页计数的频繁操作?

I should add that tables will only have a few thousand rows at most.

我应该补充一点,表最多只有几千行。

Thanks

谢谢

Martin O'Shea.

马丁·奥谢。

回答by Quassnoi

In MyISAM, this query:

MyISAM,这个查询:

SELECT  COUNT(*)
FROM    TABLE_NAME

is instant, since it's kept in the table metadata, so it's almost free to issue this query and it will always get the correct result.

是即时的,因为它保存在表元数据中,所以几乎可以自由地发出这个查询并且它总是会得到正确的结果。

In InnoDB, this query will count rows one-by-one which could take some time.

在 中InnoDB,此查询将一一计算行数,这可能需要一些时间。

So if you don't need exact value of COUNT(*), you may query INFORMATION_SCHEMA.

因此,如果您不需要 的确切值COUNT(*),您可以查询INFORMATION_SCHEMA

回答by Jason Shah

I'd also consider using SQL_CALC_FOUND_ROWSand SELECT FOUND_ROWS()if you find COUNT(*)too slow.

我还考虑使用SQL_CALC_FOUND_ROWSSELECT FOUND_ROWS()如果你发现COUNT(*)太慢。