Why are you even doing the copy? It seems like a waste:

foreach (@fields) { my $sth = $dbh->prepare( 'INSERT INTO temp_sheet (' .join(',', keys %$_) .') VALUES ('. .join(',', map {'?'} keys %$_) .')' ); $sth->execute(values %$_); }

Of course, there are still issues with that in broad strokes, because you're preparing a statement for each record, and you're relying on keys and values returning the same order. While, AFAIK, they do, I'm not sure it's a promise future versions of Perl will keep.

So, I'd refactor a touch:

# list your headers my @heading = qw[description billing_code user_id project_id bad_proj bad_bill bad_user]; # now prepare a statement *once* my $sth = $dbh-<prepare('INSERT INTO temp_sheet (' .join(',', @heading) .') VALUES ('. .join(',', map {'?'} @heading) .')' ); # now insert foreach my $row (@fields) { $sth->execute( map { $row{$_} } @heading ); }

That should be a lot faster. You could also lower your maintenance requirements by determining @heading from the DB with:

my @heading; { my $sth = $dbh->prepare('SELECT TOP 1 * FROM temp_sheet'); $sth->execute; @heading = @{ $sth->{NAME_lc} }; }

It's worth mentioning that the SELECT TOP 1 syntax might differ from DB to DB. In some cases, it is SELECT * FROM table LIMIT 1, and there might be others. It's a factor to consider.

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

In reply to Re: Creating a hashes from AoHs by radiantmatrix
in thread Creating a hashes from AoHs by bradcathey

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.