通过perl从表中获取所有记录
我期望通过 Perl 从表中获取所有 5,812,750 条记录。目前,我遇到内存不足错误。读取所有记录的任何其他最佳替代方法。
sub sane {
my $self = shift;
my $dbh = DBI->connect(
'dbi:Oracle:usbmfs',
'US', 'states',
{ AutoCommit => 0, RaiseError => 1 }
);
my $sth = $dbh->prepare(qq{
select cpu_id, system, generation, vendor, item,
week_first_moved, week_last_moved
from us_item_tbl
});
$sth->execute();
my $rows = @{ $dbh->selectall_arrayref('
select upc_id, system, generation, vendor, item,
week_first_moved, eek_last_moved
from uk_item_tbl
') };
my %lookup;
foreach my $row (@$rows) {
my($cpu, sys, $gen, $vend, $item, $wad, $wlm) = @$rows;
my $ean = sprintf "%02s%05s%05s", $sys, $vend, $item;
$cpu = sprintf "%014s", $cpu;
$lookup{$nae}{$cpu} = [$wad, $wlm];
}
}
I am expecting to fetch all 5,812,750 records from table through Perl. Currently, I am getting an out of memory error. Any other best alternative way to read all the records.
sub sane {
my $self = shift;
my $dbh = DBI->connect(
'dbi:Oracle:usbmfs',
'US', 'states',
{ AutoCommit => 0, RaiseError => 1 }
);
my $sth = $dbh->prepare(qq{
select cpu_id, system, generation, vendor, item,
week_first_moved, week_last_moved
from us_item_tbl
});
$sth->execute();
my $rows = @{ $dbh->selectall_arrayref('
select upc_id, system, generation, vendor, item,
week_first_moved, eek_last_moved
from uk_item_tbl
') };
my %lookup;
foreach my $row (@$rows) {
my($cpu, sys, $gen, $vend, $item, $wad, $wlm) = @$rows;
my $ean = sprintf "%02s%05s%05s", $sys, $vend, $item;
$cpu = sprintf "%014s", $cpu;
$lookup{$nae}{$cpu} = [$wad, $wlm];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为使用
selectall_arrayref
是出现内存不足错误的原因。请尝试使用以下代码。我将
DBI->connect()
调用更改为使用 RowCacheSize 设置为较小的值 (20),将selectall_arrayref
更改为使用prepare()
/execute()
相反,并迭代两个查询结果,一次仅获取一行。另外,我修复了代码中的几个拼写错误:$sys
缺少美元符号,写为$ean
而不是$nae
,更改查询中的“upc_id”改为“cpu_id”,并修复了“eek_last_moved”的拼写。while条件中的
//
是定义的or运算符< /a>,而不是m//
正则表达式运算符。//
当左侧参数为undef
时计算其右侧参数。fetchrow_arrayref
当返回的行数用完时,返回 undef ;发生这种情况时,会对第二个查询结果调用fetchrow_arrayref
并将结果放入$row
中。如果 Perl 抱怨//
(可能是因为您的 Perl 早于 5.10),请尝试使用||
代替。I think using
selectall_arrayref
is why you get Out of Memory error. Try the following code instead.I changed
DBI->connect()
invocation to use RowCacheSize set to a smallish value (20), changedselectall_arrayref
to useprepare()
/execute()
instead, and iterates through both query results only fetching one row at a time. Also, I fixed a couple of typos in your code:$sys
was missing the dollar sign, wrote$ean
instead of$nae
, changed "upc_id" to "cpu_id" in the query, and fixed the spelling of "eek_last_moved".The
//
in the while condition is the defined or operator, not them//
regex operator.//
evaluates its right argument when the left one isundef
.fetchrow_arrayref
returnsundef
when it runs out of rows to return; when this happens,fetchrow_arrayref
is called on the second query result and the result is put in$row
. If Perl complains about the//
(probably because you have a perl earlier than 5.10), try using||
instead.