php 使用php获取mysql表中记录总数的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/116824/
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
Whats the best way to get total # of records in a mysql table with php?
提问by Peter Bailey
Whats the most efficient way of selecting total number of records from a large table? Currently, Im simply doing
从大表中选择记录总数的最有效方法是什么?目前,我只是在做
$result = mysql_query("SELECT id FROM table");
$total = mysql_num_rows($result)
I was told this was not very efficient or fast, if you have a lot of records in the table.
有人告诉我,如果表中有很多记录,这不是非常有效或快速。
回答by Peter Bailey
You were told correctly. mysql can do this count for you which is much more efficient.
你被正确地告知。mysql 可以为您完成这个计数,效率更高。
$result = mysql_query( "select count(id) as num_rows from table" );
$row = mysql_fetch_object( $result );
$total = $row->num_rows;
回答by Wayne
You should use SQL's built in COUNT function:
您应该使用SQL 的内置 COUNT 函数:
$result = mysql_query("SELECT COUNT(id) FROM table");
回答by skoob
MyISAM tables already store the row count
MyISAM 表已经存储了行数
SELECT COUNT(*) FROM table
on a MyISAM table simply reads that value. It doesn't scan the table or the index(es). So, it's just as fast or faster than reading the value from a different table.
在 MyISAM 表上只是读取该值。它不扫描表或索引。因此,它与从不同的表中读取值一样快或快。
回答by VoxPelli
According to the MySQL documentationthis is most efficient if you're using a MyISAM table (which is the most usual type of tables used):
根据MySQL 文档,如果您使用的是 MyISAM 表(这是最常用的表类型),这是最有效的:
$result = mysql_query("SELECT COUNT(*) FROM table");
Otherwise you should do as Wayne stated and be sure that the counted column is indexed.
否则,您应该按照韦恩所说的去做,并确保对计数列进行索引。
回答by Jonathan
Can I just add, that the most "efficient" way of getting the total number of records, particularly in a large table, is to save the total amount as a number in another table. That way, you don't have to query the entire table everytime you want to get the total.
我可以补充一下,获取记录总数的最“有效”方法,尤其是在大表中,是将总数保存为另一个表中的数字。这样,您不必每次想要获取总数时都查询整个表。
You will however, have to set up some code or Triggers in the database to increase or decrease that number when a row is added/deleted.
但是,您必须在数据库中设置一些代码或触发器以在添加/删除行时增加或减少该数字。
So its not the easiest way, but if your website grows, you should definitely consider doing that.
所以这不是最简单的方法,但如果您的网站增长,您绝对应该考虑这样做。
回答by Leon
Even though I agree to use the built-in functions, I don't really see any performance difference between mysql_num_rows and count(id). For 25000 results, same performance (can say exact.) Just for the record.
即使我同意使用内置函数,我也没有真正看到 mysql_num_rows 和 count(id) 之间的任何性能差异。对于 25000 个结果,相同的性能(可以说是精确的)。仅供记录。
回答by Leon
What about something like this:
这样的事情怎么样:
$result = mysql_query("SELECT COUNT(id) AS total_things from table");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$num_results = $row["total_things"];
回答by Annika Backstrom
Just wanted to note that SHOW TABLE STATUSreturns a Rowscolumn, though I can't speak to its efficiency. Some light Googling turns up reports of slowness in MySQL 4 over two years ago. Might make for interesting time trials.
只是想注意SHOW TABLE STATUS返回一Rows列,尽管我不能谈论它的效率。两年多前,一些轻微的谷歌搜索发现了 MySQL 4 缓慢的报告。可能会进行有趣的计时赛。
Also note the InnoDB caveatregarding inaccurate counts.
回答by Divakarcool
Use aggregate function. Try the below SQL Command
使用聚合函数。试试下面的 SQL 命令
$num= mysql_query("SELECT COUNT(id) FROM $table");
回答by Apostolos
mysqli_query() is deprecated. Better use this:
不推荐使用 mysqli_query()。更好地使用这个:
$result = $dbh->query("SELECT id FROM {table_name}");
$total = $result->num_rows;
Using PDO:
使用 PDO:
$result = $dbh->query("SELECT id FROM {table_name}");
$total = $result->rowCount();
(where '$dbh' = handle of the db connected to)
(其中 '$dbh' = 连接的数据库句柄)

