Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You may use the INSERT extended syntax for inserting several rows at once. In addition, if your table has several indexes (besides the primary key) you may use the (MySQL specific) command "DISABLE/ENABLE KEYS".

Here is a framework for your insertion.

$dbh->do(qq{ALTER TABLE $table DISABLE KEYS}); my $base_query = qq{INSERT INTO $table VALUES }; my $query = $base_query; my $count = 0; my $max_rows = 10000; # adjust it to your needs while ($line = <FILE>) { my @values = something_from($line); $query .= "," if $count++; $query .= "(" . join(",", map { $dbh->quote($_) } @values ) . ")"; if ($count > $max_rows) { $dbh->do($query) or die "something wrong ($DBI::errstr)"; $query = $base_query; $count = 0; } } $dbh->do($query) if $count; $dbh->do(qq{ALTER TABLE $table ENABLE KEYS});

See the manual for INSERT and for ALTER TABLE, and Speeding up the DBI for some more on related subjects.

 _  _ _  _  
(_|| | |(_|><
 _|   

In reply to Re: DBI: speed up insertions in generating database by gmax
in thread DBI: speed up insertions in generating database by punch_card_don

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-23 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found