in reply to while/push to map, data structure confusion

It is a matter of style perhaps, but here is how I prefer to grab the results out of DBI (I usually boil it down to 4 lines but I added some extra info for newbies that might read this :-)
use DBI; use strict; # declare holder for our record set and some sample sql my @results; my $sql = "select * from msazip where zip = 60604"; # connect to sql server my $dbh = DBI->connect("dbi:ODBC:mysql", "username", "pass", { RaiseEr +ror=>1 } ) or die "$!"; # prepare and send the query my $sth = $dbh->prepare( $sql ) or die "$!"; $sth->execute(); # get the column names if you don't know them already my @col_names = @{ $sth->{NAME}}; # build an array of hashrefs so we can preserve order of record set while( my $hr = $sth->fetchrow_hashref ){ push @results, $hr; } # now use them - in this case just print each row # $rs->{'col_name'} or $results[0]->{'col_name'} foreach my $rs (@results ){ print ( sprintf "%-15s", $rs->{$_} ), " " foreach @col_names; print "\n"; }
To me it is much clearer to simply push the hashrefs into an array ( you can be sure to preserve the order of your result set ) than it is to try to do something with the likes of map or trying to build some complicated structure, keep it simple.

I am thinking something other than how you are storing the result set is really the problem for you and it came from your assertion that using fetchrow_hashref is vastly slower than using the fetchall_arrayref( 150 seconds to fetchrow_hashref -vs- 10 seconds using fetchall_arrayref method. ) I somehow doubted that is really the case. When I see really slow queries, the first place I check is my query and then the database. I am thinking the table is either missing an index or is not indexed to optimize your query. If you run a query on some modern rdbms's where a table is missing an index, some will cache the results and the next time you run it with the same params it will try to optimize it for you. Hence one time through you get a query that takes a long time and the next it seems a lot faster. Perhaps this is really the case. I would look to answer these questions: How is the table indexed? Does it even have an index!! Create a new index based on your query. Don't make storing your result set complicated. Hope this helps.

JamesNC