diff --git a/core/lib/Drupal/Core/Database/Query/Select.php b/core/lib/Drupal/Core/Database/Query/Select.php
index 22aca8f..55b0414 100644
--- a/core/lib/Drupal/Core/Database/Query/Select.php
+++ b/core/lib/Drupal/Core/Database/Query/Select.php
@@ -586,28 +586,14 @@ class Select extends Query implements SelectInterface {
    * Implements SelectInterface::countQuery().
    */
   public function countQuery() {
-    $count = $this->prepareCountQuery();
-
-    $query = $this->connection->select($count);
-    $query->addExpression('COUNT(*)');
-
-    return $query;
-  }
-
-  /**
-   * Prepares a count query from the current query object.
-   *
-   * @return Drupal\Core\Database\Query\Select
-   *   A new query object ready to have COUNT(*) performed on it.
-   */
-  protected function prepareCountQuery() {
     // Create our new query object that we will mutate into a count query.
     $count = clone($this);
 
     $group_by = $count->getGroupBy();
     $having = $count->havingConditions();
+    $unions = $count->getUnion();
 
-    if (!$count->distinct && !isset($having[0])) {
+    if (empty($unions) && !$count->distinct && !isset($having[0])) {
       // When not executing a distinct query, we can zero-out existing fields
       // and expressions that are not used by a GROUP BY or HAVING. Fields
       // listed in a GROUP BY or HAVING clause need to be present in the
@@ -635,7 +621,9 @@ class Select extends Query implements SelectInterface {
 
     // If we've just removed all fields from the query, make sure there is at
     // least one so that the query still runs.
-    $count->addExpression('1');
+    if (empty($unions)) {
+      $count->addExpression('1');
+    }
 
     // Ordering a count query is a waste of cycles, and breaks on some
     // databases anyway.
@@ -648,13 +636,10 @@ class Select extends Query implements SelectInterface {
       $count->distinct = FALSE;
     }
 
-    // If there are any dependent queries to UNION, prepare each of those for
-    // the count query also.
-    foreach ($count->union as &$union) {
-      $union['query'] = $union['query']->prepareCountQuery();
-    }
+    $query = $this->connection->select($count);
+    $query->addExpression('COUNT(*)');
 
-    return $count;
+    return $query;
   }
 
   public function __toString() {
