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.
In reply to Re: Creating a hashes from AoHs
by radiantmatrix
in thread Creating a hashes from AoHs
by bradcathey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |