RE: [PHP] counting number of records in a MySQL table; how do I get
| From: | John W. Holmes | Date: | Thu, 03 Oct 2002 16:35:19 +0000 |
| Subject: | RE: [PHP] counting number of records in a MySQL table; how do I get | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-118768@lists.php.net to get a copy of this message | ||
[snip]
> 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?
Yes, it all depends on what you are doing with the results. If you are
selecting everything, and using everything in your page, then, yeah, use
mysql_num_rows. A second query of the same thing just for COUNT(*) would
be inefficient.
> 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?
The two query method is efficient when you have large tables and you are
only going to use a subset of the results. Like in showing X records out
of Y.
I could do a little benchmarking script to see what kind of time
differences we are talking about. Maybe it's so minor that it doesn't
matter...
I think we're arguing with each other even though we agree on
everything. :)
---John Holmes...