I have this DBI script that queries a MySQL database. I prepare all my queries before I execute them. It takes about 30 min to run this script using DBD::mysql and 20+ hours for DBD::CSV. With DBD::MySQL it seems to crunch about 5000 rows at once then rests for 5 minutes. I'm not sure what it's doing.

I read in the book "Programming the Perl DBI" that I can use the attribute "mysql_store_result" and store it in memory. Any other ideas to speed up this query?

The tables are the following sizes:
sec = 21,000 rows @ 2.3 MB
bt = 1274 rows @ 675 KB
stats = 426 rows @ 95 KB
my $database = "db"; my $db_server = "localhost"; my $user = "user"; my $password = "pass"; my $dbh = DBI->connect("DBI:mysql:$database:$db_server",$user,$passwor +d); my $sth_sec = $dbh->prepare("SELECT KEY, TEST, INSTANCE, ID, HID FROM +sec") or die "Can't prepare SQL statement: $DBI::errstr\n"; my $sth_bt = $dbh->prepare("SELECT NAME, STATE FROM bt WHERE ID =? AND + HID=?") or die "Can't prepare SQL statement: $DBI::errstr\n"; my $sth_stats = $dbh->prepare("SELECT CCS, kbps, MBytes FROM stats WHE +RE ID=? AND HID=? & INSTANCE=?") or die "Can't prepare SQL statement: $DBI::errstr\n"; $sth_sec->execute() or die "Can't execute SQL statement: $DBI::errstr\n"; open(TRAFFICSTATS,'>',"NoThroughput.csv") || die("Cannot Open File"); { my $old_sel = select(TRAFFICSTATS); $| = 1; # Auto-flush. select($old_sel); } print TRAFFICSTATS "KEY,ID,HID,NAME,STATE,CCS,kbps,MBytes\n"; while ( my @row = $sth_sec->fetchrow_array ) { my ( $keyId, $testCarrier, $instanceId, $id, $hId ) = @row; $sth_bt->execute( $id, $hId ); @row = $sth_bt->fetchrow_array; my ( $name, $state ) = @row; $sth_stats->execute($id, $hId, $instanceId ); @row = $sth_stats->fetchrow_array; my ( $CCS, $kbps, $mBytes ) = @row; print TRAFFICSTATS "$keyId,$id,$hId,$name,$state,$CCS,$kbps,$mBytes +\n"; } close TRAFFICSTATS;

In reply to Speed up DBD::MySQL Queries by awohld

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.