Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: DBI Hash of Column name values

by derby (Abbot)
on Aug 09, 2010 at 16:05 UTC ( [id://853862]=note: print w/replies, xml ) Need Help??


in reply to DBI Hash of Column name values

I don't know, bind columns always have a bad smell about them. I prefer the slice alternative where your returned resultset is an array of hashes:

my $rows = $dbh->selectall_arrayref( $sql, { Slice => {} } );
Now I can do this:
foreach my $row ( @$rows ) { print $row->{NAME}, "\n"; }

-derby

Replies are listed 'Best First'.
Re^2: DBI Hash of Column name values
by Tux (Canon) on Aug 09, 2010 at 20:27 UTC

    Still slower than bind:

    { my $t0 = [ gettimeofday ]; my $n = 0; my $rows = $dbh->selectall_arrayref ($sql, { Slice => {} }); foreach my $row (@$rows) { $n++; } printf STDERR "slice: %9.2f recs/sec\n", $n / tv_interval ($t0); } { my $t0 = [ gettimeofday ]; my ($n, %rec) = (0); my $sth = $dbh->prepare ($sql); $sth->execute; $sth->bind_columns (\@rec{@{$sth->{NAME_lc}}}); while ($sth->fetch) { $n++; } printf STDERR "bind: %9.2f recs/sec\n", $n / tv_interval ($t0); } => with postgres on other server: slice: 23842.81 recs/sec bind: 27994.01 recs/sec with postgres on local server: slice: 214963.65 recs/sec bind: 505370.38 recs/sec

    YMMV


    Enjoy, Have FUN! H.Merijn

      Interesting ... YMMV most definitely. Not sure if it's due to a difference in the databases, or the drivers or the data itself ... but using your approach against a 43 col table (mixed data types), I get:

      slice: 14021.59 recs/sec bind: 6750.10 recs/sec

      -derby

        For the local compare, where bind was twice as fast as slice, I used:

        Linux 2.6.31.12-0.2 x86_64 CPU Core(TM) i5 CPU 660 @ 3.33GHz/1199(2) Memory 7802 Mb perl 5.12.1 64bitall/longdouble DBI 1.613 DBD::Pg 2.17.1 Postgres 8.4.4

        Enjoy, Have FUN! H.Merijn
      Note, my solution is using bind as suggested ...
Re^2: DBI Hash of Column name values
by avrono (Novice) on Aug 09, 2010 at 19:59 UTC
    So $rows is a ref to an Array of Hashes ?
        Thanks for all the wisdom ! I am not sure how efficient my final solution is, but I wanted an array of hashes. This works
        my %rec; $sth->bind_columns (\(@rec{@{$sth->{NAME_lc}}})); while ($sth->fetch) { my %res = %rec; push(@result, \%res); } return \@result;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://853862]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-19 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found