RE: [PHP] Fastest way to get table records' number
| From: | Edward Kay | Date: | Wed, 19 Mar 2008 15:48:17 +0000 |
| Subject: | RE: [PHP] Fastest way to get table records' number | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-271799@lists.php.net to get a copy of this message | ||
> > > At 10:35 AM -0400 3/19/08, Nathan Nobbe wrote:
> > > >On Wed, Mar 19, 2008 at 9:42 AM, Andrew Ballard <aballard@gmail.com>
> > wrote:
> > > >
> > > >> That works; I'm just wondering why you went with a count
> on an 'ID'
> > column
> > > >> rather than COUNT(*).
> > > >
> > > >
> > > >ouch, it looks like im horribly wrong :O
> > > >mysql> select count(*) from table;
> > > >+----------+
> > > >| count(*) |
> > > >+----------+
> > > >| 361724 |
> > > >+----------+
> > > >1 row in set (0.90 sec)
> > > >
> > > >mysql> select count(id) from table;
> > > >+------------+
> > > >| count(did) |
> > > >+------------+
> > > >| 361724 |
> > > >+------------+
> > > >1 row in set (4.56 sec)
> > > >
> > > >-nathan
> > >
> > > That surprised me as well.
> > >
> > > I thought that (*) meant "look up everything" and would have figured
> > > that (id) would have been quicker.
> > >
> > >
> >
> > You generally want to explicitly specify column names rather than
> > using SELECT *, because it returns everything even if you don't need
> > it. But for aggregate COUNT, I'm not surprised by the results Nathan
> > got.
> >
> what about SELECT MAX(id) FROM table :)
But that's not the same thing. If any rows have been deleted, then this
would give the wrong answer.
Edward