diff --git a/core/lib/Drupal/Core/Test/TestDatabase.php b/core/lib/Drupal/Core/Test/TestDatabase.php
index 20cc8e66870515b96650bd1735c0bf1e91a0a577..03ff3d2f626823862b159b5f505bd147c727a185 100644
--- a/core/lib/Drupal/Core/Test/TestDatabase.php
+++ b/core/lib/Drupal/Core/Test/TestDatabase.php
@@ -244,24 +244,29 @@ public static function insertAssert($test_id, $test_class, $status, $message = '
    *   The test ID to get the last test from.
    *
    * @return array
-   *   Array containing the last database prefix used and the last test class
-   *   that ran.
+   *   Associative array containing the last database prefix used and the
+   *   last test class that ran.
    *
    * @internal
    */
   public static function lastTestGet($test_id) {
     $connection = static::getConnection();
-    $last_prefix = $connection
-      ->queryRange('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', 0, 1, [
-        ':test_id' => $test_id,
-      ])
-      ->fetchField();
-    $last_test_class = $connection
-      ->queryRange('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id DESC', 0, 1, [
-        ':test_id' => $test_id,
-      ])
-      ->fetchField();
-    return [$last_prefix, $last_test_class];
+
+    // Define a subquery to identify the latest 'message_id' given the
+    // $test_id.
+    $max_message_id_subquery = $connection
+      ->select('simpletest', 'sub')
+      ->condition('test_id', $test_id);
+    $max_message_id_subquery->addExpression('MAX(message_id)', 'max_message_id');
+
+    // Run a select query to return 'last_prefix' from {simpletest_test_id} and
+    // 'test_class' from {simpletest}.
+    $select = $connection->select($max_message_id_subquery, 'st_sub');
+    $select->join('simpletest', 'st', 'st.message_id = st_sub.max_message_id');
+    $select->join('simpletest_test_id', 'sttid', 'st.test_id = sttid.test_id');
+    $select->addField('sttid', 'last_prefix');
+    $select->addField('st', 'test_class');
+    return $select->execute()->fetchAssoc();
   }
 
   /**
diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module
index fc833d1046da0929bd9a0cb9782292a6756d27f6..6f8f5b5b321e1cc1cc80920c2dad8f7d5432cced 100644
--- a/core/modules/simpletest/simpletest.module
+++ b/core/modules/simpletest/simpletest.module
@@ -407,8 +407,8 @@ function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
     // Retrieve the last database prefix used for testing and the last test
     // class that was run from. Use the information to read the lgo file
     // in case any fatal errors caused the test to crash.
-    list($last_prefix, $last_test_class) = TestDatabase::lastTestGet($test_id);
-    (new TestDatabase($last_prefix))->logRead($test_id, $last_test_class);
+    $last_test = TestDatabase::lastTestGet($test_id);
+    (new TestDatabase($last_test['last_prefix']))->logRead($test_id, $last_test['test_class']);
 
     \Drupal::messenger()->addError(t('The test run did not successfully finish.'));
     \Drupal::messenger()->addWarning(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'));
@@ -432,7 +432,7 @@ function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
  */
 function simpletest_last_test_get($test_id) {
   @trigger_error(__FUNCTION__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\TestDatabase::lastTestGet() instead. See https://www.drupal.org/node/3075252', E_USER_DEPRECATED);
-  return TestDatabase::lastTestGet($test_id);
+  return array_values(TestDatabase::lastTestGet($test_id));
 }
 
 /**
diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh
index 26763c3adbe0da0bdb5584d7355749f8d16f14ef..f1cb4c0ce45526273d6ae71a8eaa03bb942be8d7 100755
--- a/core/scripts/run-tests.sh
+++ b/core/scripts/run-tests.sh
@@ -749,7 +749,7 @@ function simpletest_script_execute_batch($test_classes) {
             ['#pass' => 0, '#fail' => 1, '#exception' => 0, '#debug' => 0]
           );
           if ($args['die-on-fail']) {
-            list($db_prefix) = TestDatabase::lastTestGet($child['test_id']);
+            $db_prefix = TestDatabase::lastTestGet($child['test_id'])['last_prefix'];
             $test_db = new TestDatabase($db_prefix);
             $test_directory = $test_db->getTestSitePath();
             echo 'Simpletest database and files kept and test exited immediately on fail so should be reproducible if you change settings.php to use the database prefix ' . $db_prefix . ' and config directories in ' . $test_directory . "\n";
@@ -899,7 +899,8 @@ function simpletest_script_cleanup($test_id, $test_class, $exitcode) {
   }
   // Retrieve the last database prefix used for testing.
   try {
-    list($db_prefix) = TestDatabase::lastTestGet($test_id);
+    $last_test = TestDatabase::lastTestGet($test_id);
+    $db_prefix = $last_test['last_prefix'];
   }
   catch (Exception $e) {
     echo (string) $e;
@@ -920,7 +921,7 @@ function simpletest_script_cleanup($test_id, $test_class, $exitcode) {
 
   // Read the log file in case any fatal errors caused the test to crash.
   try {
-    (new TestDatabase($db_prefix))->logRead($test_id, $last_test_class);
+    (new TestDatabase($db_prefix))->logRead($test_id, $last_test['test_class']);
   }
   catch (Exception $e) {
     echo (string) $e;