简体   繁体   English

使用perl批量插入mysql数据库表

[英]Bulk inserting to mysql DB table using perl

I am using a simple perl script to pupulate millions of rows in a mysql DB table. 我使用一个简单的perl脚本来在mysql数据库表中填充数百万行。 I'm using perl DBI and DBD::mysql with the script. 我在脚本中使用perl DBI和DBD :: mysql。 Example code below 示例代码如下

my $dbh = DBI->connect(<DB INFO>);
my $sth;
my $insert_com = "INSERT INTO TAB1 VALUES(?,?,?,?,?)";
for (1..50000000){

   $sth = $dbh->prepare($insert_com);
   $sth->execute(<val1>,<val2>,<val3>,<val4>,<val5>);

}

As per the above code, I think a commit is sent for each iteration of the loop. 根据上面的代码,我认为为循环的每次迭代发送一个提交。 The question I have is, whether it's possible to send a commit every n iteration ? 我的问题是,是否可以每n次迭代发送一次提交? ie to commit after inserting n number of rows to the table. 即在向表中插入n行后提交。 If its possible, can someone please tell me how. 如果可能,有人可以告诉我如何。 Thanks in advance. 提前致谢。 Cheers ... 干杯......

You have to set then "AutoCommit to zero: 你必须设置“AutoCommit为零:

$dbh = DBI->connect($dsn, $user, $password,
                      { RaiseError => 1, AutoCommit => 0 });

and call all n rows $dbh->commit() 并调用所有n行$dbh->commit()

See DBI Documentation for more details. 有关详细信息,请参阅DBI文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM