create table mytable(
field1 integer,
field2 varchar(24),
field3 varchar(24),
field4 varchar(24),
field5 varchar(24)
);
####
use strict; use warnings;
use Data::GUID;
use DBD::Pg;
use SQL::Abstract;
use Tie::Cycle;
use MCE::Loop max_workers => 4;
my $sqla = SQL::Abstract->new;
my @cols = map {"field$_"} 1..5;
my $ins_sql = $sqla->insert('mytable', { map { $_ => '' } @cols });
my $sel_sql = $sqla->select('mytable', 'count(*)', { field1 => '' });
#---------------------------------------------------------------------#
mce_loop {
my ($mce, $chunk, $chunk_id) = @_;
my $dbh = get_dbh();
my $ins_sth = $dbh->prepare_cached($ins_sql);
my $sel_sth = $dbh->prepare_cached($sel_sql);
for my $record( @{$chunk} ) {
$ins_sth->execute( @{$record} );
$sel_sth->execute( 42 );
my ($count) = $sel_sth->fetchrow_array;
my $msg = sprintf 'wid %s; chnk %s; ins %s; cnt %s',
$mce->wid, $chunk_id, $record->[0], $count;
MCE->say($msg);
}
} @{ get_sample_data() };
#---------------------------------------------------------------------#
sub get_dbh {
my $dsn = 'DBI:Pg:dbname=test_db';
my $dbh = DBI->connect($dsn, $ENV{USER}, undef, {
AutoCommit => 1,
RaiseError => 1,
PrintError => 1
}) or die "Connection failed!\n" . $DBI::errstr;
return $dbh;
}
sub get_sample_data {
tie my $value1, 'Tie::Cycle', [ 40 .. 49 ];
return [ map {
[ $value1, map { Data::GUID->new->as_base64 } 0..3]
} 1..1000 ];
}
__END__
##
##
$ perl mce-pg.pl
wid 4; chnk 3; ins 40; cnt 0
wid 1; chnk 1; ins 40; cnt 0
wid 3; chnk 2; ins 40; cnt 0
wid 2; chnk 4; ins 40; cnt 0
wid 4; chnk 3; ins 41; cnt 0
wid 1; chnk 1; ins 41; cnt 0
wid 3; chnk 2; ins 41; cnt 0
wid 2; chnk 4; ins 41; cnt 1
wid 4; chnk 3; ins 42; cnt 1
wid 1; chnk 1; ins 42; cnt 2
...
wid 2; chnk 97; ins 48; cnt 100
wid 4; chnk 99; ins 48; cnt 100
wid 1; chnk 100; ins 48; cnt 100
wid 3; chnk 98; ins 49; cnt 100
wid 2; chnk 97; ins 49; cnt 100
wid 4; chnk 99; ins 49; cnt 100
wid 1; chnk 100; ins 49; cnt 100
##
##
wid 2; chnk 4; ins 41; cnt 1
wid 4; chnk 3; ins 42; cnt 1