Pacific-Design.com

    
Home Index

1. MariaDB

2. Permutation Lenght

MariaDB / Permutation Lenght /

#!/usr/bin/perl
#------------------------------------------------------------------------------------#
use strict;
use warnings;
use BerkeleyDB;
use DBI;
use Time::HiRes qw( gettimeofday tv_interval );
$| = 1;
use vars qw( $dbh @fh $file $total );

my $MAX = 200;

#-------------------------------------------------------------------------------------------#
$dbh = DBI->connect('DBI:mysql:mysql_socket=/var/run/mysqld/mysqld.sock',
                    'user', 'password', {RaiseError => 1, AutoCommit => 0})
  || die "Database connection not made: $DBI::errstr";

#-------------------------------------------------------------------------------------------#
# Open Multiple File Handles
for (1 .. $MAX) { open($fh[$_], ">>/data2/flat/url_$_.dat"); }

for my $a ('0' .. '9', 'a' .. 'f')
{
    for my $b ('0' .. '9', 'a' .. 'f')
    {
        for my $c ('0' .. '9', 'a' .. 'f')
        {
            my $table = $a . $b . $c;
            &exec_sql('engine27', $table);
        }
    }
}

# Close Multiple File Handles
for (1 .. $MAX) { close($fh[$_]); }

#------------------------------------------------------------------------------------#
sub exec_sql
{
    my ($database, $table) = @_;

    my $sql = qq/SELECT url FROM $database.part_$table;/;
    print "----------------- [ " . $sql . " ] -----------------\n";

    my $sth = $dbh->prepare($sql);
    $sth->execute();

    my $url;

    $sth->bind_col(1, \$url);
    my $t0 = [gettimeofday];
    my $c  = 0;

    while ($sth->fetch())
    {
        $total++;
        $c++;
        print $total . "=" . $url . "\n" if (($total % 5000) == 0);
        my $len = length($url);

        if ($len > 0 && $len < $MAX)
        {
            $file = $fh[$len];
            print $file $url . "\n";
        }
    }

    my $elapsed = tv_interval($t0, [gettimeofday]);
    my $rps = int($c / $elapsed);
    print "RPS=" . $rps . " Elapsed=" . $elapsed . "\n";

    $sth->finish();
}

#------------------------------------------------------------------------------------#
__END__