The most efficient way to pull data from a database, as per the DBI documentation, is a combination of bind_columns() and fetch(). I'm not quite sure what you're trying to do with undef, but I don't think it will help you any.

If what you want is to pull the set of data from the database, it would be slightly (by not much) faster to do this:

# Error handling assumed. my $sql = " ... "; my $sth = $dbh->prepare_cached( $sql ); $sth->bind_columns( \(my ($foo)) ); my @data; while ($sth->fetch) { push @data, $foo; } $sth->finish;

The reason is that Perl doesn't have to allocate and deallocate return values - the data is placed right into the scalar reference you passed bind_columns(). A similar method can be used for your HoA in the third snippet. You can, alternatively, do:

# Error handling assumed. my $sql = " ... "; my $sth = $dbh->prepare_cached( $sql ); my @stuff; $sth->bind_columns( \(my ($foo), @stuff[0..3]) ); my %data; while ($sth->fetch) { $data{$foo} = [ @stuff ]; } $sth->finish;

Now, the alternatives I'm presenting won't double your throughput. We're talking, at best, maybe 5-10%, if that. Most applications will see a 0-2% improvement. The guarantee is that this isn't slower and it might be faster.

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.


In reply to Re: How to use undef in DBI sql query result collection by dragonchild
in thread How to use undef in DBI sql query result collection by punch_card_don

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.