SQL和PHP-哪个更快mysql_num_rows()或'select count()'? [英] SQL & PHP - Which is faster mysql_num_rows() or 'select count()'?

查看:66
本文介绍了SQL和PHP-哪个更快mysql_num_rows()或'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.

此外,对于某些引擎(例如MyISAM),MySQL可以从表

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆