> 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.

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law

In reply to Re^10: Query database in Prolog by radiantmatrix
in thread Query database in Prolog by zby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.