--- includes/database/database.inc	2010-10-16 05:03:43.000000000 +1100
+++ includes/database/database-new.inc	2010-11-03 16:53:50.000000000 +1100
@@ -398,12 +398,50 @@ abstract class DatabaseConnection extend
    *   The properly-prefixed string.
    */
   public function prefixTables($sql) {
-    // Replace specific table prefixes first.
-    foreach ($this->prefixes as $key => $val) {
-      $sql = strtr($sql, array('{' . $key . '}' => $val . $key));
+    if (!empty($this->prefixes)) {
+      // Build the array of strings to find.
+      $find = $this->prefixes;
+      $find = array_keys($find);
+      array_walk($find, array($this, 'prefixTablesFind'));
+
+      // Build the array of replacement values.
+      $replace = $this->prefixes;
+      array_walk($replace, array($this, 'prefixTablesReplace'));
     }
-    // Then replace remaining tables with the default prefix.
-    return strtr($sql, array('{' => $this->defaultPrefix , '}' => ''));
+    else {
+      // Create empty arrays.
+      $find = $replace = array();
+    }
+
+    // Add the default prefix to the end of the arrays.
+    array_push($find, '{', '}');
+    array_push($replace, $this->defaultPrefix, '');
+
+    // Replace all strings at once and return modified SQL statement.
+    return str_replace($find, $replace, $sql);
+  }
+
+  /**
+   * Modifies an array value passed by DatabaseConnection::prefixTables()
+   *
+   * @param $value
+   *   A string containing the table name.
+   */
+  protected function prefixTablesFind(&$value) {
+    $value = '{' . $value . '}';
+  }
+
+  /**
+   * Modifies an array value passed by DatabaseConnection::prefixTables()
+   *
+   * @param $value
+   *   A string containing the table prefix to be appended.
+   *
+   * @param $key
+   *   A string containing the table name.
+   */
+  protected function prefixTablesReplace(&$value, $key) {
+    $value = $value . $key;
   }
 
   /**
