1

我正在使用一个简单的 perl 脚本来填充 mysql DB 表中的数百万行。我在脚本中使用 perl DBI 和 DBD::mysql。下面的示例代码

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>);

}

根据上面的代码,我认为每次循环迭代都会发送一个提交。我的问题是,是否可以每 n 次迭代发送一次提交?即在向表中插入 n 行后提交。如果可能的话,有人可以告诉我怎么做。提前致谢。干杯...

4

1 回答 1

3

您必须将“自动提交为零:

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

并调用所有 n 行$dbh->commit()

有关详细信息,请参阅DBI 文档。

于 2014-09-10T09:07:25.740 回答