From 142551a30378d8d370682fd6757f698b5a1e5f68 Mon Sep 17 00:00:00 2001
From: Denny Lubitz <lubitz@vivomedia.de>
Date: Tue, 1 Oct 2024 15:31:29 +0200
Subject: [PATCH] BUGFIX: Improve performance on buildAncestorNodesQueries

---
 .../src/Domain/Repository/ContentSubgraph.php               | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/Neos.ContentGraph.DoctrineDbalAdapter/src/Domain/Repository/ContentSubgraph.php b/Neos.ContentGraph.DoctrineDbalAdapter/src/Domain/Repository/ContentSubgraph.php
index 70fd9d3be97..c17841a428a 100644
--- a/Neos.ContentGraph.DoctrineDbalAdapter/src/Domain/Repository/ContentSubgraph.php
+++ b/Neos.ContentGraph.DoctrineDbalAdapter/src/Domain/Repository/ContentSubgraph.php
@@ -564,7 +564,7 @@ private function buildAncestorNodesQueries(NodeAggregateId $entryNodeAggregateId
         //    The parent node anchor points to the first parent node of the given node aggregate. This is also used for
         //    the recursive part, to determine its ancestors.
         $queryBuilderInitial = $this->createQueryBuilder()
-            ->select('ph.subtreetags, ph.parentnodeanchor')
+            ->select('ph.subtreetags, ph.parentnodeanchor, ph.childnodeanchor')
             ->from($this->nodeQueryBuilder->tableNames->hierarchyRelation(), 'ph')
             ->innerJoin('ph', $this->nodeQueryBuilder->tableNames->node(), 'c', 'c.relationanchorpoint = ph.childnodeanchor')
             ->andWhere('ph.contentstreamid = :contentStreamId')
@@ -575,7 +575,7 @@ private function buildAncestorNodesQueries(NodeAggregateId $entryNodeAggregateId
         // 2) Fetch the parent hierarchy recursive, starting with the anchor point of the resulting parent as child anchor
         //    point for the next iteration.
         $queryBuilderRecursive = $this->createQueryBuilder()
-            ->select('h.subtreetags, h.parentnodeanchor')
+            ->select('h.subtreetags, h.parentnodeanchor, h.childnodeanchor')
             ->from('ancestry', 'cn')
             ->innerJoin('cn', $this->nodeQueryBuilder->tableNames->hierarchyRelation(), 'h', 'h.childnodeanchor = cn.parentnodeanchor')
             ->where('h.contentstreamid = :contentStreamId')
@@ -584,7 +584,7 @@ private function buildAncestorNodesQueries(NodeAggregateId $entryNodeAggregateId
 
         $queryBuilderCte = $this->nodeQueryBuilder->buildBasicNodesCteQuery($entryNodeAggregateId, $this->contentStreamId, $this->dimensionSpacePoint, 'ancestry', 'a');
         // 3) Finally we join the node table to all collected parent node anchor
-        $queryBuilderCte->innerJoin('a', $this->nodeQueryBuilder->tableNames->node(), 'pn', 'pn.relationanchorpoint = a.parentnodeanchor');
+        $queryBuilderCte->innerJoin('a', $this->nodeQueryBuilder->tableNames->node(), 'pn', 'pn.relationanchorpoint = a.childnodeanchor AND pn.nodeaggregateid <> :entryNodeAggregateId');
 
         if ($filter->nodeTypes !== null) {
             $this->nodeQueryBuilder->addNodeTypeCriteria($queryBuilderCte, ExpandedNodeTypeCriteria::create($filter->nodeTypes, $this->nodeTypeManager), 'pn');