Perl DBI使用mysql本机多重插入能力插入多行

发布于 2024-12-19 20:02:57 字数 265 浏览 0 评论 0原文

有没有人见过 Perl 的 DBI 类型模块,它可以轻松地利用 MySQL 的多插入语法

插入 TBL (col1, col2, col3) 值 (1,2,3),(4,5,6) ,...

我还没有找到允许我这样做的界面。我发现的唯一的事情就是循环遍历我的数组。与将所有内容放入一行并让 MySQL 处理相比,此方法似乎不太理想。我没有在 IE google 上找到任何文档来阐明如何滚动我自己的代码来做到这一点。

TIA

Has anyone seen a DBI-type module for Perl which capitalizes, easily, on MySQL's multi-insert syntax

insert into TBL (col1, col2, col3) values (1,2,3),(4,5,6),...?

I've not yet found an interface which allows me to do that. The only thing I HAVE found is looping through my array. This method seems a lot less optimal vs throwing everything into a single line and letting MySQL handle it. I've not found any documentation out there IE google which sheds light on this short of rolling my own code to do it.

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疧_╮線 2024-12-26 20:02:57

有两种方法。您可以根据数组的大小多次插入 (?, ?, ?) 。文本操作类似于:

my $sql_values = join( ' ', ('(?, ?, ?)') x scalar(@array) );

然后展平数组以调用 execute()。我会避免这种方式,因为需要完成棘手的字符串和数组操作。

另一种方法是开始事务,然后多次运行单个插入语句。

my $sql = 'INSERT INTO tbl (col1, col2, col3)';
$dbh->{AutoCommit} = 0;
my $sth = $dbh->prepare_cached( $sql );
$sth->execute( @$_ ) for @array;
$sth->finish;
$dbh->{AutoCommit} = 1;

这比第一种方法慢一点,但仍然避免了重新解析语句。它还避免了第一个解决方案的微妙操作,同时仍然是原子的并允许优化磁盘 I/O。

There are two approaches. You can insert (?, ?, ?) a number of times based on the size of the array. The text manipulation would be something like:

my $sql_values = join( ' ', ('(?, ?, ?)') x scalar(@array) );

Then flatten the array for calling execute(). I would avoid this way because of the thorny string and array manipulation that needs to be done.

The other way is to begin a transaction, then run a single insert statement multiple times.

my $sql = 'INSERT INTO tbl (col1, col2, col3)';
$dbh->{AutoCommit} = 0;
my $sth = $dbh->prepare_cached( $sql );
$sth->execute( @$_ ) for @array;
$sth->finish;
$dbh->{AutoCommit} = 1;

This is a bit slower than the first method, but it still avoids reparsing the statement. It also avoids the subtle manipulations of the first solution, while still being atomic and allowing disk I/O to be optimized.

夏了南城 2024-12-26 20:02:57

如果DBD::mysql支持DBI的execute_for_fetch(参见DBI的execute_array和execute_for_fetch),这是典型的使用场景,即,您现在有多行可用的插入/更新/删除,并且希望一次性(或批量)发送它们。我不知道 mysql 客户端库是否支持一次性发送多行绑定参数,但大多数其他数据库客户端库都支持并且可以利用 DBI 的execute_array/execute_for_fetch。不幸的是,很少有 DBD 实际上实现了execute_array/execute_for_fetch,并且依赖于 DBI 一次一行地实现它。

If DBD::mysql supported DBI's execute_for_fetch (see DBI's execute_array and execute_for_fetch) this is the typical usage scenario i.e., you have multiple rows of inserts/updates/deletes available now and want to send them in one go (or in batches). I've no idea if the mysql client libs support sending multiple rows of bound parameters in one go but most other database client libs do and can take advantage of DBI's execute_array/execute_for_fetch. Unfortunately few DBDs actually implement execute_array/execute_for_fetch and rely on DBI implementing it one row at a time.

物价感观 2024-12-26 20:02:57

吉姆,
弗雷泽克有它。这可能是最优化的:

my $sth = $dbh->prepare( 'INSERT INTO tbl (?, ?, ?)' );
foreach(@array) { $sth->execute( @{$_} ); }
$sth->finish;

Jim,
Frezik has it. That is probably the most optimal:

my $sth = $dbh->prepare( 'INSERT INTO tbl (?, ?, ?)' );
foreach(@array) { $sth->execute( @{$_} ); }
$sth->finish;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文