在 MySQL 中获取表的行数?
谁能告诉我在查找表的行数时,他在 MySQL 中使用哪种方法更好:这样做更好吗
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
SELECT COUNT(*) FROM TABLE_NAME
还是在INFORMATION_SCHEMA
的TABLE
表中查找行数?
or to look up the row count in the table TABLE
in the 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.
谢谢
马丁·奥谢.
推荐答案
在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.
在 InnoDB
中,此查询将一一计算行数,这可能需要一些时间.
In InnoDB
, this query will count rows one-by-one which could take some time.
所以如果你不需要COUNT(*)
的精确值,你可以查询INFORMATION_SCHEMA
.
So if you don't need exact value of COUNT(*)
, you may query INFORMATION_SCHEMA
.
相关文章