Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Good trick! Except there should be an extra set of parenthesis in your join statement in order for the 100 question elements to be passed to the join function as a list :

 my $sql = "INSERT INTO temp_selectedTitles values ". join(", ", ("(?)") x 100);

Also, because the example used only one element in each row, it was not quite clear to me that the above example did not just create a single row with 100 columns (it does not). It was also unclear from the example how you might apply this concept when inserting rows that have multiple columns. After playing around with it a little, I was able to get something like the following to work:

$dbh->do("CREATE TABLE IF NOT EXISTS temp_selectedTitles (title VARCHA +R(128), author VARCHAR(128), location VARCAR(128))"); my $number_of_rows_waiting_to_commit = 0; my @chunk_of_values_for_database = (); foreach my $book (@books) { push (@chunk_of_values_for_database, $book); push (@chunk_of_values_for_database, $authors{$book}); push (@chunk_of_values_for_database, $locations{$book}); $number_of_rows_waiting_to_commit++; if ($number_of_rows_waiting_to_commit >= 100){ my $sql = "INSERT IGNORE INTO $DT_table (title, author, locati +on) VALUES ".join(", ", ("(?, ?, ?)") x $number_of_rows_waiting_to_co +mmit); my $store_1 = $dbh->prepare($sql); $store_1->execute(@chunk_of_values_for_database); $number_of_rows_waiting_to_commit = 0; @chunk_of_values_for_database = (); } } my $sql = "INSERT IGNORE INTO $DT_table (title, author, location) VALU +ES ".join(", ", ("(?, ?, ?)") x $number_of_rows_waiting_to_commit); my $store_2 = $dbh->prepare($sql); $store_2->execute(@chunk_of_values_for_database);

Note that I actually tested this on a table with 6 columns and inserted 10,000 rows at a time instead of 100. It probably will work for more than that, but there may be limits to the size of the $sql string that you pass to the DBI and that in turn gets passed to whatever database you are using. Also, too large of a string could potentially cause a penalty in performance. But from what I've found, this trick does indeed speed up the process tremendously.


In reply to Re^2: DBI execute_array by DamianKaelGreen
in thread DBI execute_array by plendid

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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2020-08-14 23:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which rocket would you take to Mars?










      Results (77 votes). Check out past polls.

      Notices?