# 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 … ?”)
  plus a looped $sth->execute($id)

- ## This often gives a significant performance boost

  - even where placeholders are emulated, such as MySQL
  - because it avoids statement handle creation overhead

## .

Previous slide | Next slide | Back to first slide | View graphic version