Skip to content

Instantly share code, notes, and snippets.

@ledjon
Created July 8, 2013 01:49
Show Gist options
  • Save ledjon/5945702 to your computer and use it in GitHub Desktop.
Save ledjon/5945702 to your computer and use it in GitHub Desktop.
Perl script to do a fast nextval() calls in PostgreSQL to show the cache = 1 vs. cache = 10000 and speed differences. Note: Lots of "sql injection" happening here because this is a demo. This runs so fast that the prepare time (when using proper ? syntax) was actually much more CPU than the work itself.
#!/usr/bin/perl
use strict;
use DBI;
use Parallel::ForkManager;
# seqtest=# create sequence t1 increment by 1 start with 1 cache 1;
# CREATE SEQUENCE
# seqtest=# create sequence t1000 increment by 1 start with 1 cache 10000;
# CREATE SEQUENCE
my $seq = shift or die "Please provide sequence name as arg #1\n";
my $dbh = DBI->connect("DBI:Pg:dbname=seqtest", "", "", { RaiseError => 1 }) or die("Could not connec to db\n");
my $pm = Parallel::ForkManager->new(10);
for ( my $i = 0; $i < 100; $i++ )
{
my $pid = $pm->start and next;
# now am child
my $d = $dbh->clone;
for( 1..10000 )
{
# $d->do("select nextval(?)", undef, $seq);
$d->do("select nextval('$seq')");
}
$pm->finish;
}
# we are purely parent at this point
$pm->wait_all_children;
warn("done!\n");
1;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment