> Note that if you're processing large amounts of data, making those anonymous hashes is slower.
Yep. I have had that be an occasional performance bottleneck. Solved it thusly (IIRC):
$sth = $dbh->prepare("Select * FROM datatable");
$sth->execute;
my %map;
{
my @col = @{ $sth->{NAME_lc} };
for (0..$#col) { $map{$col[$_]} = $_ }
}
while( my $row = $sth->fetchrow_arrayref ) {
# I want the cols named ID, name, city, in that order
printf "%d: %s from %s\n"
,$row->{$map{id}}
,$row->{$map{name}}
,$row->{$map{city}};
}
In this way, you use the fastest fetchrow (fetchrow_arrayref) on each iteration, which seems to be much faster.
Granted, specifying the field order in the SELECT and then using constants to reference field order (or just passing the list to printf) is faster still. But, if (like me) someone else updates the SQL that your code is calling (like when you just call stored procedures), the code above just keeps working, whereas using numerical constants could break.
I second the cry of YMMV, but this has always worked well for me.
|