Some thoughts:
  1. prepare_cached() the initial query
  2. Loop over the initial query. (Using IN instead of = ...)
  3. Use an array slice in the second execute() statement. (@assign[0..11] instead of $assign[0], $assign[1], etc.)

Essentially, it would boil down to this:

my @audit_params = keys %$audits; my $audit_params_string = join ',', ('?') x @audit_params; my $audit_sql = <<__END_SQL__; SELECT AuditID, Referrals.1, Referrals.2, Referrals.3, Referrals.4, Referrals.5, Referrals.6, Referrals.7, Referrals.8, Referrals.9, Referrals.10, Referrals.11, Referrals.12, Referrals.13, Referrals.14, States.1 FROM Referrals INNER JOIN States On (Referrals.10 = States.1 +) WHERE AuditID IN ($audit_params_string) __END_SQL__ my $audit_sth = $dbh->prepare_cached($audit_sql) || die "Cannot prepar +e() '$audit_sql'\n"; $audit_sth->execute(@audit_params) || die "Cannot execute() '$audit_sq +l' with '@audit_params'\n"; my $insert_sql = <<__END_SQL__; INSERT INTO report_printing_table (1,2,3,4,5,6,7,8,9,10,11,12,13) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) __END_SQL__ while (my @assign = $audit_sth->fetchrow_array) { my $audit_id = shift @assign; my $insert_sth = $dbh->prepare_cached($insert_sql) || die "Cannot +prepare() '$insert_sql'\n"; $insert_sth->execute($audit_id, @assign[0..11]) || die "Cannot exe +cute '$insert_sql' with '$audit @assign[0..11]'\n"; $insert_sth->finish; } $audit_sth->finish;

FYI: Code is completely untested.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.


In reply to Re: Optimization/Efficiency Question by dragonchild
in thread Optimization/Efficiency Question by Grygonos

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.