在归档日志管理中很重要的一个功能, 就是根据一个Sequence号, 来查找归档日志的路径. 普通的做法是使用find命令来实现, 如下所示:

#
# Get the full archived log name by sequence
#
sub findLogBySequence
{
    my ($logseq) = @_;
    my $logfile = "";

    if (length($logfile) == 0)
    {
        # Foloowing logic is for Oracle 9i (sid_%t_%s.arc)
        $logfile = `find /data*/arch -follow -name *_${logseq}.* | head -1`;
        chomp($logfile);
        # Foloowing logic is for Oracle 10g (sid_%t_%s_%r.arc)
        if (length($logfile) == 0)
        {
          $logfile = `find /data*/arch -follow -name *_${logseq}_* | head -1`;
          chomp($logfile);
        }
    }

    scalar $logfile;
}

    如果要管理的归档日志很多, 可能要跑成百上千次find命令, 有些资源上的浪费, 可以好好优化一下, 一般来讲同类的归档肯定是放在一起的. 因此将上面的Perl程序优化如下:

my $logfmt = "???";
#
# Get the full archived log name by sequence
#
sub findLogBySequence
{
    my ($logseq) = @_;
    my $logfile = "";

    #
    # if archive log pattern is not null
    # get the archive log file from pattern
    #
    if ($logfmt ne "???")
    {
        $logfile = $logfmt;
        $logfile =~ s/XXXXXX/${logseq}/g;
        if (! -e $logfile)
        {
          $logfile = "";
          $logfmt = "???";
        }
    }

    if (length($logfile) == 0)
    {
        # Foloowing logic is for Oracle 9i (sid_%t_%s.arc)
        $logfile = `find /data*/arch -follow -name *_${logseq}.* | head -1`;
        chomp($logfile);
        # Foloowing logic is for Oracle 10g (sid_%t_%s_%r.arc)
        if (length($logfile) == 0)
        {
          $logfile = `find /data*/arch -follow -name *_${logseq}_* | head -1`;
          chomp($logfile);
        }
    }

    # Store the archive log pattern
    if (length($logfile) and -e $logfile and $logfmt eq "???")
    {
        $logfmt = $logfile;
        $logfmt =~ s/_${logseq}\./_XXXXXX\./;
        $logfmt =~ s/_${logseq}_/_XXXXXX_/;
    }
    scalar $logfile;
}

    有了高效的查找日志路径的算法, 就可以做更多智能的功能了, 如指定保留多少GB的归档.