From a4bafeaa94541641c36f3c0e989b1af0828bbc51 Mon Sep 17 00:00:00 2001 From: Nathan ter Bogt Date: Wed, 3 May 2023 21:44:09 +1000 Subject: [PATCH] Optimise the use case where the two conditions overlap in an = --- src/Storage/DbalNestedSet.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Storage/DbalNestedSet.php b/src/Storage/DbalNestedSet.php index f5519184..0fb396eb 100644 --- a/src/Storage/DbalNestedSet.php +++ b/src/Storage/DbalNestedSet.php @@ -142,13 +142,19 @@ public function findDescendants(NodeKey $nodeKey, int $depth = 0, int $start = 1 ->orderBy('child.left_pos', 'ASC') ->setParameter('id', $nodeKey->getId()) ->setParameter('revision_id', $nodeKey->getRevisionId()); - if ($start > 0) { - $query->andWhere('child.depth >= :start_depth + parent.depth') - ->setParameter('start_depth', $start); + if ($depth > 0 && $depth == $start) { + $query->andWhere('child.depth = :depth + parent.depth') + ->setParameter('depth', $depth); } - if ($depth > 0) { - $query->andWhere('child.depth <= :depth + parent.depth') - ->setParameter('depth', $start + $depth - 1); + else { + if ($start > 0) { + $query->andWhere('child.depth >= :start_depth + parent.depth') + ->setParameter('start_depth', $start); + } + if ($depth > 0) { + $query->andWhere('child.depth <= :depth + parent.depth') + ->setParameter('depth', $start + $depth - 1); + } } $stmt = $query->executeQuery(); assert($stmt instanceof Result);