From 36baf4c4e34c060b9dfd7c63196ae7c782b143da Mon Sep 17 00:00:00 2001 From: Brezo Cordero <8002881+brezocordero@users.noreply.github.com> Date: Tue, 5 Apr 2022 17:47:19 -0500 Subject: [PATCH] Simplify the query to calculate lifetime emails Get max and count of subscribers in the query. [MAILPOET-4177] --- .../SubscribersEmailCountsController.php | 22 ++++++------------- .../tasks/phpstan/phpstan-7-baseline.neon | 5 ----- .../tasks/phpstan/phpstan-8-baseline.neon | 5 ----- 3 files changed, 7 insertions(+), 25 deletions(-) diff --git a/mailpoet/lib/Subscribers/SubscribersEmailCountsController.php b/mailpoet/lib/Subscribers/SubscribersEmailCountsController.php index 9b044bfced..f897fad5f3 100644 --- a/mailpoet/lib/Subscribers/SubscribersEmailCountsController.php +++ b/mailpoet/lib/Subscribers/SubscribersEmailCountsController.php @@ -107,10 +107,12 @@ class SubscribersEmailCountsController { private function countAndMaxOfSubscribersInRange(int $startId, int $batchSize): array { $result = $this->entityManager->getConnection()->executeQuery(" - SELECT s.id FROM {$this->subscribersTable} as s - WHERE s.id >= :startId - ORDER BY s.id - LIMIT :batchSize + SELECT COUNT(ids.id) as count, COALESCE(MAX(ids.id), 0) as max FROM ( + SELECT s.id FROM {$this->subscribersTable} as s + WHERE s.id >= :startId + ORDER BY s.id + LIMIT :batchSize + ) ids ", [ 'startId' => $startId, @@ -124,16 +126,6 @@ class SubscribersEmailCountsController { $subscribersInRange = $result->fetchAllAssociative(); - $countSubscribersInRange = count(array_map( - function ($id) { - return (int)$id['id']; - }, - $subscribersInRange - )); - - if (!$countSubscribersInRange) { - return [0,0]; - } - return [$countSubscribersInRange,$subscribersInRange[$countSubscribersInRange - 1]['id']]; + return [$subscribersInRange[0]['count'], $subscribersInRange[0]['max']]; } } diff --git a/mailpoet/tasks/phpstan/phpstan-7-baseline.neon b/mailpoet/tasks/phpstan/phpstan-7-baseline.neon index 80c3270b7f..bb6b68b4cb 100644 --- a/mailpoet/tasks/phpstan/phpstan-7-baseline.neon +++ b/mailpoet/tasks/phpstan/phpstan-7-baseline.neon @@ -1106,11 +1106,6 @@ parameters: count: 2 path: ../../lib/Subscribers/InactiveSubscribersController.php - - - message: "#^Cannot cast mixed to int\\.$#" - count: 1 - path: ../../lib/Subscribers/SubscribersEmailCountsController.php - - message: "#^Cannot cast mixed to int\\.$#" count: 3 diff --git a/mailpoet/tasks/phpstan/phpstan-8-baseline.neon b/mailpoet/tasks/phpstan/phpstan-8-baseline.neon index 7379d5f5b0..b693bcf63a 100644 --- a/mailpoet/tasks/phpstan/phpstan-8-baseline.neon +++ b/mailpoet/tasks/phpstan/phpstan-8-baseline.neon @@ -1106,11 +1106,6 @@ parameters: count: 2 path: ../../lib/Subscribers/InactiveSubscribersController.php - - - message: "#^Cannot cast mixed to int\\.$#" - count: 1 - path: ../../lib/Subscribers/SubscribersEmailCountsController.php - - message: "#^Cannot cast mixed to int\\.$#" count: 3