Entity query in \Drupal\Core\Entity\ContentEntityStorageBase::getLatestRevisionId() should use a sort and limit

Created on 11 August 2025, 4 days ago

Problem/Motivation

Using \Drupal\Core\Entity\Query\QueryInterface::latestRevision() to mark a query as only for the latest revision was slow prior to πŸ› Entity queries querying the latest revision very slow with lots of revisions Needs work . The most common use-case is the query in \Drupal\Core\Entity\ContentEntityStorageBase::getLatestRevisionId(). This could be further improved by doing something like:

      $result = $this->getQuery()
        ->allRevisions()
        ->condition($this->entityType->getKey('id'), $entity_id)
        ->range(0, 1)
        ->sort($this->entityType->getKey('revision'), 'DESC')
        ->accessCheck(FALSE)
        ->execute();

However \Drupal\workspaces\EntityQuery\QueryTrait::prepare() very much treats the call to ->latestRevision() as API. We could do something like we did in πŸ› Entity queries for latest revisions should return the latest workspace-specific revisions Postponed: needs info and add metadata to the query to help workspaces.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

entity system

Created by

πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

Live updates comments and jobs are added and updated live.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024