Re: Fastest way to get table records' number

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



First, I swore I deleted that message before I sent it.  Rethought the whole 
count(*) thing and re-read the OP that said count(*) was faster.  Oh well.. 
hah..

Second.. about mysql_num_rows()...   at that point, the database has 
collected the data and sent it to the PHP script.   If you're pulling the 
data anyway, that's cool, but if all you want is a count then you're better 
off letting the database do the heavy lifting and only returning a single 
column, single row with the count.

Say for instance, you have a table with 1000 rows:


SELECT * FROM sometable

This will grab all the data, send it all to PHP.


SELECT count(*) FROM sometable

Sends a single row to PHP.  Leaving the heavy lifting to the DB server.  This 
is a good thing if you don't need any of the data, just a count.


-TG

----- Original Message -----
From: It Maq <itmaqurfe@xxxxxxxxx>
To: PHP General list <php-general@xxxxxxxxxxxxx>
Date: Wed, 19 Mar 2008 10:38:16 -0700 (PDT)
Subject: Re:  Fastest way to get table records' number

> Hi,
> 
> did you try mysql_num_rows ?
> 
> --- Andrew Ballard <aballard@xxxxxxxxx> wrote:
> 
> > On Wed, Mar 19, 2008 at 1:04 PM, TG
> > <tg-php@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> > >
> > >  It seems that count(*) pulls all the data from
> > the row then performs a count
> > >  increment whereas count(did) only pulls the 'did'
> > column.
> > 
> > Again, I don't believe COUNT(*) pulls any data. If
> > there is a row, it
> > simply counts it. The row could be full of NULLS (if
> > allowed by your
> > schema - yikes) and it will still be counted. I'd
> > guess that COUNT(1)
> > does the same thing. COUNT(did) does only examine
> > the `did` column,
> > but NULL values are excluded from the count.


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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux