SQL和PHP-哪个更快mysql_num_rows()或'select count()'? [英] SQL & PHP - Which is faster mysql_num_rows() or 'select count()'?
问题描述
我只是想知道哪种方法最有效,如果我只是想获取表中的行数.
I'm just wondering which method is the most effective if I'm literally just wanting to get the number of rows in a table.
$res = mysql_query("SELECT count(*) as `number` FROM `table1`");
$count = mysql_fetch_result($res,0,'number');
或
$res = mysql_query("SELECT `ID` FROM `table1`");
$count = mysql_num_rows($res);
有人对此做过任何体面的测试吗?
Anyone done any decent testing on this?
推荐答案
mysql_query()传输所有结果从MySQL记录到返回之前的php pcrocess(不同于 mysql_unbufferd_query()).仅此一项将使mysql_num_rows()的版本变慢.
mysql_query() transfers all result records from the MySQL into the php pcrocess before it returns (unlike mysql_unbufferd_query()). That alone would make the mysql_num_rows() version slower.
Furthermore for some engines (like MyISAM) MySQL can serve a Count(*) request from the index of the table without hitting the actual data. A SELECT * FROM foo
on the other hand results in a full table scan and MySQL has to read every single dataset.
这篇关于SQL和PHP-哪个更快mysql_num_rows()或'select count()'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!