in reply to DBI do vs prepare
Of couse, that sort of demonstration also highlights a good reason, besides speed, for favoring the "prepare/execute" approach: using placeholders in the SQL statement tends to be better and easier than using quoted strings.use strict; use DBI; use Benchmark qw/timethese/; my $times = shift; my @vals = ( 10734..10845 ); my $sql = "UPDATE Tests SET foo = 'bar' WHERE ID = ?"; my $dbh = DBI->connect("connection_string..."); my $sth = $dbh->prepare( $sql ); sub dbi_do { for my $val ( @vals ) { (my $dosql = $sql) =~ s/\?/'$val'/; $dbh->do( $dosql ); } } sub dbi_prep { for my $val ( @vals ) { $sth->execute( $val ); } } timethese( $times, { DbiDo => \&dbi_do, PrepExec => \&dbi_prep, });
(updated to fix the "use Benchmark" line)
|
---|