in reply to DBI: Better way to find number of rows in a table?

just as a note under mySQL I would use limit rather than count. eg.

my $sth = $dbh->prepare("select * from $table limit 1"); $sth->execute(); print "Theres at least 1 row" if $sth->rows();

That way you don't have to look through the whole table to create the result ( although it's possible that is optimized)

note: untested

Update: mahi's note below shows just why one should be careful about premature optimization. heh.

Replies are listed 'Best First'.
Re: Re: DBI: Better way to find number of rows in a table?
by rnahi (Curate) on Jun 04, 2003 at 09:29 UTC

    Your method will work, but not the way you say.

    In MySQL, COUNT(*) (without a WHERE clause) is optimized so that it will get the result from the description table, without physically counting the records.

    Thus, your query will be slower than using COUNT. ;)

    jeffa's method is the fastest you can get in this case.