From 602b66d986fa48ee889b0104aefae98738e0ca7d Mon Sep 17 00:00:00 2001 From: Hokuto Shinoda Date: Sat, 5 Oct 2024 17:45:40 +0900 Subject: [PATCH] =?UTF-8?q?perf:=20isu=5Fconditions=E5=8F=96=E5=BE=97?= =?UTF-8?q?=E9=83=A8=E5=88=86=E3=81=AB=E3=81=82=E3=82=8BN+1=E5=95=8F?= =?UTF-8?q?=E9=A1=8C=E3=82=92=E8=A7=A3=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- php/app/routes.php | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/php/app/routes.php b/php/app/routes.php index c2bdc06..70647e3 100644 --- a/php/app/routes.php +++ b/php/app/routes.php @@ -1508,23 +1508,28 @@ public function getTrend(Request $request, Response $response): Response /** @var array $characterCriticalIsuConditions */ $characterCriticalIsuConditions = []; - foreach ($isuList as $isu) { - try { - $stmt = $this->dbh->prepare('SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ? ORDER BY timestamp DESC'); - $stmt->execute([$isu->jiaIsuUuid]); - $rows = $stmt->fetchAll(); - } catch (PDOException $e) { - $this->logger->error('db error: ' . $e->errorInfo[2]); - - return $response->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR); - } + $isuUuids = array_map(fn($isu) => $isu->jiaIsuUuid, $isuList); + try { + $stmt = $this->dbh->prepare('SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` IN (' . implode(',', array_fill(0, count($isuList), '?')) . ') ORDER BY timestamp DESC'); + $stmt->execute($isuUuids); + $rows = $stmt->fetchAll(); + } catch (PDOException $e) { + $this->logger->error('db error: ' . $e->errorInfo[2]); - /** @var array $conditions */ - $conditions = []; - foreach ($rows as $row) { - $conditions[] = IsuCondition::fromDbRow($row); + return $response->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR); + } + $conditionsMap = []; + foreach ($rows as $row) { + $condition = IsuCondition::fromDbRow($row); + if (empty($conditionsMap[$condition->jiaIsuUuid])) { + $conditionsMap[$condition->jiaIsuUuid] = [$condition]; + } else { + $conditionsMap[$condition->jiaIsuUuid] []= $condition; } + } + foreach ($isuList as $isu) { + $conditions = $conditionsMap[$isu->jiaIsuUuid] ?? []; if (count($conditions) > 0) { $isuLastCondition = $conditions[0]; try {