[snip]
I still think it's more effecient to use two queries in your case. One with
a COUNT(*) to get total records, and one with a LIMIT to just get the 40 you
want. With a smaller table, it may not matter, but with large tables, you
could be using up a lot of resources by selecting everything, but then only
using 40 rows of it.
[/snip]

I see your point John, and I used a bad example. I was just pointing out
that mysql_num_rows() would return the number of rows for any query and
prevents from having to do 2 queries where one would suffice if you need to
also use the data. Consider (some code left out for clarity);

<?php
// do database connection etc. $connection
// select names by state for mailing list
$query = "SELECT name, address, city, state, zip FROM customer WHERE
state='TX'";

// run query and do error checking
if(!($results = mysql_query($query, $connection))){
   print("MySQL reports: " . mysql_error() . "\n");
   exit();
}
// get the number of customers in this state
$x = mysql_num_rows($result);

// print mailing list
while($data = mysql_fetch_object($result)){
   print($data->name . ", " . $data->address . ", " . $data->city . ", " .
$data->state . ", " . $data->zip . "\n");
}
print("There are " . $x . " customers in this state.\n");

// close the database connection
?>

I agree that COUNT is an optimized return, but for a situation where you are
retrieving data from the query for whatever use it is more efficient to use
mysql_num_rows() to return the count of this particular query than it would
be to issue a second query, no?

I guess we could get into some semantical discussion at this point about
efficiency. I am an old school coder, and as such I have been taught to
conserve CPU cycles and round trips to other servers. As technology grows
some of these things could go by the wayside because processing power has
increased multi-fold since I began my foray many years ago. I always seek to
send the fewest queries to the database server where possible... efficient
query design, use of other functions available in the programming language
to provide additional data, or any number of other tricks, tips, and magic
tricks have helped to keep things clean.

So, is mysql_num_rows() less efficient than a second query asking SELECT
COUNT(*)? Is there any way to time the two where doing a query like the
above example?

Thanks!

Jay



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to