Greetings,
I am trying to save resources on my web server and was wondering what will be more efficient when it comes to just counting the number of rows:
$query = "SELECT COUNT(*) as num FROM table'";
$total_rows = mysql_fetch_array(mysql_query($query));
$finalcount = $total_rows['num'];
or
$query = "SELECT COUNT(id) as num FROM table'";
$total_rows = mysql_fetch_array(mysql_query($query));
$finalcount = $total_rows['num'];
or
$count = mysql_num_rows(mysql_query("SELECT id FROM table"));
I was searching on Google but kept getting conflicting answers. In all cases, “id” is a primary auto-incrementing key that cannot be null. In the first two SQL Count examples, they are the same except “*” vs “id”.
I thought the “id” would use less resources because “*” selects all the data in the table, but I heard this is not the case though.
The last example is 1 line and selects just “id” and counts.
Please explain which one is most effective in terms of saving web hosting resources (both PHP and MySQL resources) and/or load time. If you know of an even more efficient method, please post it!
Thanks