[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: : Charles K. Clarkson [CKC], on Friday, November 26, 2004 at : 05:30 (-0600) has on mind: : : : It would be slower and less accurate. To use your : : suggestion would require another query on the database. : : That count might also not return the same count as is : : in "scalar @$rows" if the database is updated between : : queries. : : hm, I did some benchmarks and it seems I'm right, I use : something like: : : ... : $start = new Benchmark; : for (1 .. 1000) { : $sth = $dbh->prepare_cached( : 'SELECT COUNT(sessionid) FROM session' ); : $sth->execute(); : $result = $sth->fetchrow_array(); : $sth->finish; : } : $end = new Benchmark; : : $diff = timediff($end, $start); : print "Time taken was ", timestr($diff, 'all'), " seconds\n\n"; : ... : : and it was definitely faster, than your suggestion. Did : you try some benchmarking?
I timed the two ideas with logic. Here's the method I presented. my $rows = $dbh->selectall_arrayref( 'SELECT sessionid FROM session' ); my $row_count = @$rows; Here's the method you presented. my $rows = $dbh->selectall_arrayref( 'SELECT sessionid FROM session' ); my $row_count = $dbh->do( 'SELECT COUNT(sessionid) FROM session' ); The difference between the two methods is the second statement. my $row_count = @$rows; vs. my $row_count = $dbh->do( 'SELECT COUNT(sessionid) FROM session' ); Do you really believe that DBI will make a second call to the database faster than perl can return the size of an array in memory? Now, let's say someone updates the database between calls, on a busy web site those two queries might accurately report different table sizes. HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>