Hot handles
Avoid using $dbh->do(…) in a speed-critical loop
- It’s usually creating and destroying a statement handle each time
Use $sth = $dbh->prepare(…)and $sth->execute() instead
Using prepare() gets a handle on the statement in the SQL cache
- Avoids a round-trip to server for SQL cache check on each use
For example… convert looped
$dbh->do("insert … ?", undef, $id)
into $sth = $dbh->prepare("insert … ?”) before the loop
and $sth->execute($id) inside the loop
This often gives a significant performance boost
- even where placeholders are emulated, such as the current DBD::mysql
- because it avoids statement handle creation overhead