Hello all, According to the documentation, it is more efficient to do (I'm paraphrasing):
$sth = $dbh->prepare($sql); foreach (@ValueList) { $sth->execute($_); }
than
foreach (@ValueList) { $dbh->do("Interpolated sql statement"); }
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.

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, } );

In reply to DBI do vs prepare by BrianC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.