One reason it's so slow is that you're loading the entire database table into your program, when you only need 3 lines! You don't need to do that; since you're using MySQL, you can use the LIMIT directive:
select foo from bar limit 3
What do you want that where clause to do? That matches *any* ID: so you don' t need it. Just eliminate the where clause entirely.

And why are you selecting three columns if you only want one? Just say

select data from frog limit 3
Also you should try using bind_columns and fetch, cause it's faster:
my($data); $sth->bind_columns(\$data); while ($sth->fetch) { print $data; }
If you want to select multiple columns, try this:
my $sth = $dbh->prepare(<<SQL); select id, data from frog limit 3 SQL $sth->execute; my($id, $data); $sth->bind_columns(\$id, \$data); while ($sth->fetch) { print $id, "\t", $data, "\n"; }
(By the way, I took out the error-checking partly for brevity, and partly cause I always use RaiseError).

Check out Tricks with DBI.


In reply to Re: Database, DBI Slowness by btrott
in thread [untitled node, ID 41059] by Anonymous Monk

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.