简体   繁体   English

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

[英]How to increase efficiency of perl script which uses sqlplus

I have this perl script which takes the data from sqlplus database... this database adds a new entry every time when there is a change in the value of state for a particular serial number. 我有这个perl脚本,它从sqlplus数据库中获取数据...每当特定序列号的state值发生变化时,该数据库都会添加一个新条目。 Now we need to pick the entries at every state change and prepare a csv file with old state, new state and other fields. 现在,我们需要在每次状态更改时选择条目,并准备一个具有旧状态,新状态和其他字段的csv文件。 db table sample. db表示例。

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

my script: 我的脚本:

#! /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);

query in VoucherQuery1.sql: 在VoucherQuery1.sql中查询:

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

query in VoucherQuery2.sql: 在VoucherQuery2.sql中查询:

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

query in VoucherQuery2.sql: 在VoucherQuery2.sql中查询:

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

and my sample output: 和我的示例输出:

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

Script is working pretty fine. 脚本运行正常。 But problem is that actual db table has millions of records for a specific day... and therefore it is raising performance issues... could you please advise how can we improve the efficiency of this script in terms of time & load. 但是问题在于实际的db表在特定的一天中有数百万条记录...因此会引发性能问题...请您告知我们如何在时间和负载方面提高此脚本的效率。 Only restriction is that I can't use DBI module for this... Also in case of any error in the sql queries, error msg is coming to QueryResult?.txt files. 唯一的限制是我不能为此使用DBI模块...另外,如果sql查询中有任何错误,则QueryResult?.txt文件中将出现错误msg。 I want to handle and receive these errors in my log file. 我想处理并在我的日志文件中收到这些错误。 how this can be accomplished? 如何做到这一点? thanks 谢谢

I think you need to tune your query. 我认为您需要调整查询。 A good starting point is to use the EXPLAIN PLAN, if it is an Oracle database. 一个良好的起点是使用EXPLAIN PLAN(如果它是Oracle数据库)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM