#!/usr/bin/perl -w # This code available under a BSD 3-clause license. # http://opensource.org/licenses/BSD-3-Clause use strict; use DBI; use Time::HiRes qw[ gettimeofday tv_interval ]; my $dbh_local = DBI->connect("dbi:SQLite:db=:memory:", "", "", { AutoCommit => 1, RaiseError => 1 } ); defined $dbh_local or die "db connect failed"; $dbh_local->begin_work(); $dbh_local->do(qq[ CREATE TABLE phrase (id INTEGER PRIMARY KEY, phrase TEXT) ]); # Insert 500 rows of a common phrase: my $phrase = "The quick red fox jumps over the lazy brown dog!"; my $insert = $dbh_local->prepare(qq[ INSERT INTO phrase (phrase) VALUES (?) ]); $insert->execute($phrase) for (1..500); $dbh_local->commit(); # Start the SELECT query from the local DB: my $select = $dbh_local->prepare(qq[ SELECT phrase FROM phrase ]); $select->execute(); # Create a fetch-subroutine that execute_for_fetch() will use. # This returns the "next" row to be inserted as an array-reference as required: my $fetch_sub = sub { return $select->fetchrow_arrayref(); }; # Connect to the remote database my $dbh_remote = DBI->connect("dbi:Pg:host=172.21.2.10;db=lab", "labUser", "labPass", { AutoCommit => 0, RaiseError => 1 } ); defined $dbh_remote or die "db connect failed"; # START TIMER, then do the inserts. my @timer = gettimeofday(); $insert = $dbh_remote->prepare(qq[ INSERT INTO phrase (phrase) VALUES (?) ]); $insert->execute_for_fetch($fetch_sub); $dbh_remote->commit(); # REPORT TIME, now that the inserts are done printf "Total time to insert was: %.2fs\n", tv_interval(\@timer);