如何提高使用sqlplus的perl脚本的效率

时间:2014-12-22 06:51:41

标签: sql oracle perl sqlplus

我有这个perl脚本从sqlplus数据库中获取数据...每次特定序列号的状态值发生变化时,此数据库都会添加一个新条目。现在我们需要在每个状态更改时选择条目,并准备一个包含旧状态,新状态和其他字段的csv文件。 db table sample。

SERIALNUMBER         STATE                AT                        OPERATORID    SUBSCRIBERID    TRANSACTIONID
51223344558899       Available            20081008T10:15:47         vsuser
51223344558857       Available            20081008T10:15:49         vsowner
51223344558899       Used                 20081008T10:20:25         vsuser
51223344558860       Stolen               20081008T10:15:49         vsanyone
51223344558857       Damaged              20081008T10:50:49         vsowner
51223344558899       Damaged              20081008T10:50:25         vsuser
51343253335355       Available            20081008T11:15:47         vsindian

我的剧本:

#! /usr/bin/perl

#use warnings;
use strict;


#my $circle =
#my $schema =
my $basePath = "/scripts/Voucher-State-Change";

#my ($sec, $min, $hr, $day, $month, $years) = localtime(time);
#$years_+=1900;$mont_+=1;
#my $timestamp=sprintf("%d%02d%02d",$years,$mont,$moday);


sub getDate {
    my $daysago=shift;
    $daysago=0 unless ($daysago);
    #my @months=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time-(86400*$daysago));
    # YYYYMMDD, e.g. 20060126
    return sprintf("%d%02d%02d",$year+1900,$mon+1,$mday);
    }

my $filedate=getDate(1);
#my $startdate="${filedate}T__:__:__";
my $startdate="20081008T__:__:__";
print "$startdate\n";


##### Generating output file---
my $outputFile = "${basePath}/VoucherStateChangeReport.$filedate.csv";
open (WFH, ">", "$outputFile") or die "Can't open output file $outputFile for writing: $!\n";
print WFH "VoucherSerialNumber,Date,Time,OldState,NewState,UserId\n";



##### Generating log file---
my $logfile = "${basePath}/VoucherStateChange.$filedate.log";
open (STDOUT, ">>", "$logfile") or die "Can't open logfile $logfile for writing: $!\n";
open (STDERR, ">>", "$logfile") or die "Can't open logfile $logfile for writing: $!\n";
print "$logfile\n";

##### Now login to sqlplus-----
my $SQLPLUS='/opt/oracle/product/11g/db_1/bin/sqlplus -S system/coolman7@vsdb';
`$SQLPLUS \@${basePath}/VoucherQuery1.sql $startdate> ${basePath}/QueryResult1.txt`;


open (FH1, "${basePath}/QueryResult1.txt");

while (my $serial = <FH1>) {
     chomp ($serial);
     my $count = `$SQLPLUS \@${basePath}/VoucherQuery2.sql $serial $startdate`;
     chomp ($count);
     $count =~ s/\s+//g;
     #print "$count\n";
     next if $count == 1;

    `$SQLPLUS \@${basePath}/VoucherQuery3.sql $serial $startdate> ${basePath}/QueryResult3.txt`;

#  print "select * from sample where SERIALNUMBER = $serial----\n";
     open (FH3, "${basePath}/QueryResult3.txt");


     my ($serial_number, $state, $at, $operator_id);
     my $count1 = 0;
     my $old_state;
     while (my $data = <FH3>) {
            chomp ($data);
                    #print $data."\n";
           my @data = split (/\s+/, $data);
           my ($serial_number, $state, $at, $operator_id) = @data[0..3];
           #my $serial_number = $data[0];
           #my $state = $data[1];
           #my $at = $data[2];
           #my $operator_id = $data[3];


           $count1++;
           if ($count1 == 1) {
              $old_state = $data[1];
              next;
              }

           my ($date, $time) = split (/T/, $at);
           $date =~ s/(\d{4})(\d{2})(\d{2})/$1-$2-$3/;

           print WFH "$serial_number,$date,$time,$old_state,$state,$operator_id\n";
           $old_state = $data[1];
           }
       }
close(WFH);

在VoucherQuery1.sql中查询:

select distinct SERIALNUMBER from sample where AT like '&1';

在VoucherQuery2.sql中查询:

select count(*) from sample where SERIALNUMBER = '&1' and AT like '&2';

在VoucherQuery2.sql中查询:

select * from sample where SERIALNUMBER = '&1' and AT like '&2';

和我的示例输出:

VoucherSerialNumber,Date,Time,OldState,NewState,UserId
51223344558857,2008-10-08,10:50:49,Available,Damaged,vsowner
51223344558899,2008-10-08,10:20:25,Available,Used,vsuser
51223344558899,2008-10-08,10:50:25,Used,Damaged,vsuser

脚本运行得很好。但问题是实际的数据库表在特定日期有数百万条记录......因此它会引发性能问题......请问您如何在时间和方面提高我们的脚本效率?加载。唯一的限制是我不能使用DBI模块...... 此外,如果sql查询中出现任何错误,则错误消息将发送到QueryResult?.txt文件。我想在我的日志文件中处理和接收这些错误。如何实现这一目标?感谢

1 个答案:

答案 0 :(得分:0)

我认为您需要调整查询。一个很好的起点是使用EXPLAIN PLAN,如果它是Oracle数据库。