Monks, I'm looking for ways to speed up a subroutine that involves the insertion of many (~300,000) records into a PostgreSQL database. At the moment I'm doing a prepare_cached() with placeholders, then running insert() for each record. Is there a more efficient way to do this, possibly based around bind_param_array and execute_array, or should I look elsewhere for my speed gains?

It looks to me like the databasing is the rate limiting step because when I run the program the cpu usage is about 60% while disk IO is going crazy.

I crave the benefit of your experience.

Code follows:

sub insert_nodes_one { my $sth = $dbh->prepare_cached("INSERT INTO node (taxid, name, tre +e_id, parent, children, rank, common_name) VALUES (?, ?, '4', '-1', 'replace me', ?, ?); ") or die "Cannot prepare: " . $dbh->errstr(); foreach (keys %tax_lookup) { first_pass($_, $sth, \$n, $count); } } sub first_pass{ my $key = $_[0]; my $sth = $_[1]; my $n= $_[2]; my $count= $_[3]; my $name = $tax_lookup{$key}{name}; $name =~ s/'/_/g; my $common_name = $tax_lookup{$key}{common_name}; $common_name = 'none' unless ($common_name); $common_name =~ s/'/_/g; my $parent_taxid = $tax_lookup{$key}{parent_taxid}; my $rank = $tax_lookup{$key}{rank}; if ($tax_lookup{$key}{rank} eq 'no') {$rank = 'none'} my $children_taxid = join ' ', @{$tax_lookup{$key}{children_ta +xid}}; unless ($$n % 100) {print "\r($$n / $count)"} $sth->execute($key, $name, $rank, $common_name); $$n++; }

In reply to Quickest way to insert many rows with DBI 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.