diff --git a/includes/database/mysql/schema.inc b/includes/database/mysql/schema.inc
index 949cf4e..b7bb103 100644
--- a/includes/database/mysql/schema.inc
+++ b/includes/database/mysql/schema.inc
@@ -24,6 +24,13 @@ class DatabaseSchema_mysql extends DatabaseSchema {
   const COMMENT_MAX_COLUMN = 255;
 
   /**
+   * Field types which are considered large objects.
+   *
+   * @var array
+   */
+  protected static $lobTypes = array('blob', 'text');
+
+  /**
    * Get information about the table and database name from the prefix.
    *
    * @return
@@ -80,10 +87,37 @@ class DatabaseSchema_mysql extends DatabaseSchema {
 
     // Provide defaults if needed.
     $table += array(
-      'mysql_engine' => 'InnoDB',
+      'mysql_engine' => isset($info['mysql_engine']) ? $info['mysql_engine'] : 'InnoDB',
       'mysql_character_set' => 'utf8',
     );
 
+    // Modify the table schema to comply with MEMORY storage engine restrictions.
+    if ($table['mysql_engine'] == 'MEMORY') {
+
+      // The MEMORY engine requires that all fields which participate in indexes
+      // which can be any non-LOB field must occur before any LOB fields.
+      // Re-order fields to place any LOB fields at the end of the table
+      // definition.
+      uasort($table['fields'], array('self', 'compareFieldTypes'));
+
+      // The MEMORY engine does not support the use of LOB fields in indexes.
+      // Remove indexes which utilize LOB fields.
+      foreach($table['indexes'] as $index_name => $index_fields) {
+        foreach ($index_fields as $index_field) {
+          if (is_array($index_field)) {
+            $field_name = $index_field[0];
+          }
+          else {
+            $field_name = $index_field;
+          }
+          if (in_array($table['fields'][$field_name]['type'], self::$lobTypes)) {
+            unset($table['indexes'][$index_name]);
+            break;
+          }
+        }
+      }
+    }
+
     $sql = "CREATE TABLE {" . $name . "} (\n";
 
     // Add the SQL statement for each field.
@@ -118,6 +152,29 @@ class DatabaseSchema_mysql extends DatabaseSchema {
   }
 
   /**
+   * Comparison function to sort table fields according to whether they are a
+   * BLOB/CLOB type.
+   *
+   * @param array $field1
+   *   The first field for comparison.
+   * @param array $field2
+   *   The second field for comparison.
+   * @return number
+   */
+  protected static function compareFieldTypes($field1, $field2) {
+    $field1_is_lob = in_array($field1['type'], self::$lobTypes);
+    $field2_is_lob = in_array($field2['type'], self::$lobTypes);
+
+    if (!$field1_is_lob && $field2_is_lob) {
+      return -1;
+    }
+    elseif ($field1_is_lob && !$field2_is_lob) {
+      return 1;
+    }
+    return 0;
+  }
+
+  /**
    * Create an SQL string for a field to be used in table creation or alteration.
    *
    * Before passing a field out of a schema definition into this function it has
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php
index 0853c7d..2cad9d1 100644
--- a/modules/simpletest/drupal_web_test_case.php
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -1297,9 +1297,11 @@ class DrupalWebTestCase extends DrupalTestCase {
   /**
    * Changes the database connection to the prefixed one.
    *
+   * @param $mysql_engine
+   *   The name of the mysql table engine.
    * @see DrupalWebTestCase::setUp()
    */
-  protected function changeDatabasePrefix() {
+  protected function changeDatabasePrefix($mysql_engine = NULL) {
     if (empty($this->databasePrefix)) {
       $this->prepareDatabasePrefix();
       // If $this->prepareDatabasePrefix() failed to work, return without
@@ -1317,6 +1319,9 @@ class DrupalWebTestCase extends DrupalTestCase {
       $connection_info[$target]['prefix'] = array(
         'default' => $value['prefix']['default'] . $this->databasePrefix,
       );
+      if (isset($mysql_engine)) {
+        $connection_info[$target]['mysql_engine'] = $mysql_engine;
+      }
     }
     Database::addConnectionInfo('default', 'default', $connection_info['default']);
 
@@ -1421,6 +1426,7 @@ class DrupalWebTestCase extends DrupalTestCase {
     }
 
     // Reset all statics and variables to perform tests in a clean environment.
+    $mysql_engine = $conf['simpletest_mysql_engine'];
     $conf = array();
     drupal_static_reset();
 
@@ -1428,7 +1434,7 @@ class DrupalWebTestCase extends DrupalTestCase {
     // All static variables need to be reset before the database prefix is
     // changed, since DrupalCacheArray implementations attempt to
     // write back to persistent caches when they are destructed.
-    $this->changeDatabasePrefix();
+    $this->changeDatabasePrefix($mysql_engine);
     if (!$this->setupDatabasePrefix) {
       return FALSE;
     }
diff --git a/modules/simpletest/simpletest.pages.inc b/modules/simpletest/simpletest.pages.inc
index 3127459..9cb0a4d 100644
--- a/modules/simpletest/simpletest.pages.inc
+++ b/modules/simpletest/simpletest.pages.inc
@@ -440,6 +440,15 @@ function simpletest_settings_form($form, &$form_state) {
     '#type' => 'fieldset',
     '#title' => t('General'),
   );
+  if (db_driver() == 'mysql') {
+    $form['general']['simpletest_mysql_engine'] = array(
+      '#type' => 'select',
+      '#title' => t('MySQL table engine'),
+      '#options' => array(NULL => t('- Default -')) + drupal_map_assoc(array('MEMORY', 'MyISAM', 'InnoDB')),
+      '#default_value' => variable_get('simpletest_mysql_engine', NULL),
+      '#description' => t('Forces the chosen MySQL table engine to be used when running tests, which may speed up testing. Do not use when running database tests.  The MEMORY engine will only work with MySQL versions where it supports BLOB/text columns.'),
+    );
+  }
   $form['general']['simpletest_clear_results'] = array(
     '#type' => 'checkbox',
     '#title' => t('Clear results after each complete test suite run'),
