BrianC has asked for the wisdom of the Perl Monks concerning the following question:
than$sth = $dbh->prepare($sql); foreach (@ValueList) { $sth->execute($_); }
So I'm trying to figure out why, in the following example, Code1, which uses the prepare strategy, benchmarks slower than Code2, which uses repeated dos? BTW, in the actual program the @Result array is different for each element of the @Took array, hence the apparent circumlocution.foreach (@ValueList) { $dbh->do("Interpolated sql statement"); }
Thanks in advance, Brian
#!/usr/bin/perl -w use strict; use Benchmark qw/:all/; my $times = shift; sub UpdateDBIar { my ($arsql) = @_; use DBI; my $dbh = DBI->connect("dbi:mysql:database=SchoolToolsDev;host=loc +alhost;user=stadmin;password=stadmin") or die "Couldn't connect to da +tabase $DBI::errstr\n"; foreach my $sql (@$arsql) { $dbh->do($sql) or die "Couldn't execute sql: $sql $dbh->errstr +"; } } sub UpdateDBIar2 { my ($arargs) = @_; use DBI; my $dbh = DBI->connect("dbi:mysql:database=SchoolToolsDev;host=loc +alhost;user=stadmin;password=stadmin") or die "Couldn't connect to da +tabase $DBI::errstr\n"; foreach my $argset (@$arargs) { my $sql = $argset->[0]; my $artemp = $argset->[1]; my $sth = $dbh->prepare($sql); foreach my $arg (@$artemp) { $sth->execute(join ',', @$arg); } } } my $crCode1 = sub { my @Result = (10734..10845); my @Took = (qw/Baseline Q1 Q2 Q3 Q4/); my $arargs; foreach my $Took (@Took) { my $sql = "UPDATE Tests SET $Took = '1' WHERE ID = ?"; my $artemp; foreach (@Result) { push @$artemp, [$_]; } push @$arargs, [$sql, $artemp]; } UpdateDBIar2($arargs); }; my $crCode2 = sub { my @Result = (10734..10845); my @Took = (qw/Baseline Q1 Q2 Q3 Q4/); my @sqls; foreach my $Took (@Took) { foreach (@Result) { push @sqls, "UPDATE Tests SET $Took = '1' WHERE ID = '$_'" +; } } UpdateDBIar(\@sqls); }; timethese ($times, { Code1 => $crCode1, Code2 => $crCode2, } );
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: DBI do vs prepare
by chromatic (Archbishop) on Aug 13, 2006 at 22:31 UTC | |
by astaines (Curate) on Aug 13, 2006 at 23:57 UTC | |
by BrianC (Acolyte) on Aug 14, 2006 at 00:16 UTC | |
by BrianC (Acolyte) on Aug 13, 2006 at 23:51 UTC | |
by chromatic (Archbishop) on Aug 14, 2006 at 00:37 UTC | |
by BrianC (Acolyte) on Aug 14, 2006 at 02:50 UTC | |
Re: DBI do vs prepare
by graff (Chancellor) on Aug 14, 2006 at 00:12 UTC | |
Re: DBI do vs prepare
by jdtoronto (Prior) on Aug 14, 2006 at 14:38 UTC | |
by BrianC (Acolyte) on Aug 15, 2006 at 15:21 UTC | |
Re: DBI do vs prepare
by jeanluca (Deacon) on Aug 14, 2006 at 11:16 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |