Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: DBI execute_array

by samtregar (Abbot)
on Feb 11, 2009 at 16:53 UTC ( #743109=note: print w/ replies, xml ) Need Help??


in reply to DBI execute_array

What database are you using? If it's MySQL I'm pretty sure a multi-value insert will be faster. For example, you could insert in blocks of 100:

my $sql = "INSERT INTO temp_selectedTitles values ". join(", ", "(?) +" x 100); my $sth = $dbh->prepare($sql); for (my $i = 0; $i <= $#books; $i+=100) { $sth->execute(@books[$i..$i+99]); }

Of course if you don't have an exact number of blocks then you have to clean up what's left with another statement, either one at a time or custom fitted. I use this technique all the time and it always beats going row by row.

I think execute_array() is mostly abandoned at this point - I can't remember the last time I saw it used outside the DBI docs.

-sam


Comment on Re: DBI execute_array
Download Code
Re^2: DBI execute_array
by DamianKaelGreen (Acolyte) on Apr 23, 2014 at 17:20 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://743109]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-08-29 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls