diff --git includes/database/mysql/query.inc includes/database/mysql/query.inc
index b1f248a..09f0b59 100644
--- includes/database/mysql/query.inc
+++ includes/database/mysql/query.inc
@@ -104,5 +104,57 @@ class TruncateQuery_mysql extends TruncateQuery {
 }
 
 /**
+ * MySQL-specific implementation of SelectQuery.
+ *
+ * MySQL doesn't efficiently support subqueries. The optimizer code will
+ * execute the subquery for every row of the main query, which is horribly
+ * inefficient. The most efficient solution is that one has to use a JOIN
+ * in a MySQL specific syntax instead of the subquery. This is not readily
+ * constructed automatically in the dirver, so instead we utilize a somewhat
+ * less efficient approach.  We force the engine to materialize (ie. generate a
+ * temporary table) for those subqueries by "double-wrapping" them:
+ *
+ * @code
+ *   UPDATE table SET col = 1 WHERE id = (SELECT id FROM table2);
+ * @endcode
+ *
+ * becomes:
+ *
+ * @code
+ *   UPDATE table SET col = 1 WHERE id = (SELECT * FROM (SELECT id FROM table2) alias));
+ * @endcode
+ */
+class SelectQuery_mysql extends SelectQuery {
+  function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL) {
+    if (isset($queryPlaceholder) && empty($queryPlaceholder->preventDoubleWrap)) {
+      // Disable double wrapping for subqueries of this query.
+      $queryPlaceholder->preventDoubleWrap = TRUE;
+
+      // Compile the sub-query using wrapping.
+      $this->doubleWrap = TRUE;
+      $this->wrapTableName = 'db_subquery_table_' . $queryPlaceholder->nextPlaceholder();
+      $output = parent::compile($connection, $queryPlaceholder);
+
+      // Reenable wrapping for the next subquery.
+      unset($queryPlaceholder->preventDoubleWrap);
+
+      return $output;
+    }
+    else {
+      return parent::compile($connection, $queryPlaceholder);
+    }
+  }
+
+  function __toString() {
+    if (!empty($this->doubleWrap)) {
+      return 'SELECT * FROM (' . parent::__toString() . ') ' . $this->wrapTableName;
+    }
+    else {
+      return parent::__toString();
+    }
+  }
+}
+
+/**
  * @} End of "ingroup database".
  */
