open(INFILE1,"INPUT.txt");
my $modfile = 'Data.txt';
open MODIFIED,'>',$modfile or die "Could not open $modfile : $!";
for (;;) {
my $line1 = <INFILE1>;
last if not defined $line1;
my $line2 = <INFILE1>;
last if not defined $line2;
my ($tablename1, $colname1,$sql1) = split(/\t/, $line1);
my ($tablename2, $colname2,$sql2) = split(/\t/, $line2);
if ($tablename1 eq $tablename2)
{
my $sth1 = $dbh->prepare($sql1);
$sth1->execute;
my $hash_ref1 = $sth1->fetchall_hashref('KEY');
my $sth2 = $dbh->prepare($sql2);
$sth2->execute;
my $hash_ref2 = $sth2->fetchall_hashref('KEY');
my @fieldname = split(/,/, $colname1);
my $colcnt=0;
my $rowcnt=0;
foreach $key1 ( keys(%{$hash_ref1}) )
{
foreach (@fieldname)
{
$colname =$_;
my $strvalue1='';
@val1 = $hash_ref1->{$key1}->{$colname};
if (defined @val1)
{
my @filtered = grep /@val1/, @metadata;
my $strvalue1 = substr(@filtered[0],index(@filtered[0],'||') + 2);
}
my $strvalue2='';
@val2 = $hash_ref2->{$key1}->{$colname};
if (defined @val2)
{
my @filtered = grep /@val2/, @metadata2;
my $strvalue2 = substr(@filtered[0],index(@filtered[0],'||') + 2);
}
if ($strvalue1 ne $strvalue2 )
{
$colcnt = $colcnt + 1;
print MODIFIED "$tablename1\t$colname\t$strvalue1\t$strvalue2\n";
}
}
}
if ($colcnt>0)
{
print "modified count is $colcnt\n";
}
%$hash_ref1 = ();
%$hash_ref2 = ();
}
该程序是读取输入文件,其中每一行包含三个由制表符分隔的字符串。第一个是 TableName,第二个是所有列名,中间有逗号,第三个包含要运行的 sql。由于此实用程序正在比较数据,因此每个表名都有两行。每个数据库一个。因此需要从每个相应的数据库中挑选数据,然后逐列进行比较。
SQL 在结果集中作为 ID 返回,如果值来自 db,则需要通过从数组中读取将其转换为字符串(该数组包含 100K 记录,键和值由 || 分隔)
现在我运行了这个一组表,每个数据库中包含 18K 条记录。每个 sql 中从 db 中选择了 8 列。因此,对于 18K 中的每条记录,然后对于该记录中的每个字段(即 8 条),此脚本都需要大量时间。
我的问题是,是否有人可以看看它是否可以改进,以便减少时间。文件内容示例
INPUT.TXT
TABLENAME COL1,COL2 select COL1,COL2 from TABLENAME where ......
TABLENAMEB COL1,COL2 select COL1,COL2 from TABLENAMEB where ......
元数据数组包含类似这样的内容(每个 db 有两个 ie)
111||Code 1
222||Code 2
请建议