Here's another way to do it, but using bind_columns
and fetch which is the fastest way
to fetch rows from the database, according to the DBI docs,
and confirmed by recent benchmarks:
my $statement = q{
SELECT column
FROM table
};
my $sth = $dbh->prepare($statement);
$sth->execute;
my %hash;
$sth->bind_columns(\my $column);
++$hash{$column} while $sth->fetch;
Although, it's really too bad you didn't use a database
for the counting, what you want is possible with regular SQL:
my $statement = q{
SELECT column
, COUNT(*)
FROM table1
GROUP BY column
};
my $sth = $dbh->prepare($statement);
$sth->execute;
my %hash;
$sth->bind_columns(\my($column, $count));
$hash{$column} = $count while $sth->fetch;
If you have a large amount of information to walk through,
it could be significantly faster to offload this sort of work
to the database. It can do all sorts of internal optimizations,
such as counting it's index, that you can't match with pure
perl.