I'm using a very large table with over 1 million rows of info in my database. I need to continuosly have an accurate count of a certain field and wanted to know which is the fastest and easiest way to get this info from the table.

Which would take the least effort to do this, count() or num_rows.

I know this may be a fairly simple question, I just need to have it clarified in my head. Thanks.

<?
$result=mysql_query("select count(something) from table where username ='$username' and something ='$something'");
while ($row = mysql_fetch_array($result)) {
$amount = $row['count(something)'];
}

$result = mysql_query("SELECT something from table where username ='$username' and something ='$something'");
$num_rows = mysql_num_rows($result);

?>

    For a small number of rows, there may not be a significant difference. But as the number of rows increases I would expect the count() method to be faster, simply because the num_rows method would require that the mysql_query actually return all those rows in the result set to your script, while the count() method will always return just one row.

      Write a Reply...