diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php
index 6851be0..26eca4b 100644
--- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php
+++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php
@@ -94,6 +94,11 @@ protected function preSave(EntityInterface $comment) {
         if ($comment->pid->target_id == 0) {
           // This is a comment with no parent comment (depth 0): we start
           // by retrieving the maximum thread level.
+          $result = drupal_container()->get('entity.query')->getAggregate($this->entityType)
+            ->condition('nid', $comment->nid->target_id)
+            ->aggregate('thread', 'MAX')
+            ->execute();
+          $result = reset($result);
           $max = db_query('SELECT MAX(thread) FROM {comment} WHERE nid = :nid', array(':nid' => $comment->nid->target_id))->fetchField();
           // Strip the "/" from the end of the thread.
           $max = rtrim($max, '/');
