Use WC lookup tables for improved performance

This commit is part of a task to allow multiple values in "is in country" segment. This commit replaces the queries with the more performant ones written by @lysyjan. It also creates the lookup tables on the integration database and updates the test.

[MAILPOET-3952]
This commit is contained in:
Brezo Cordero
2021-12-08 16:34:27 -06:00
committed by Veljko V
parent b04cb41921
commit 06a4be4214
2 changed files with 211 additions and 41 deletions

View File

@@ -5,6 +5,7 @@ namespace MailPoet\Segments\DynamicSegments\Filters;
use MailPoet\Entities\DynamicSegmentFilterData;
use MailPoet\Entities\DynamicSegmentFilterEntity;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\Util\DBCollationChecker;
use MailPoet\Util\Security;
use MailPoetVendor\Doctrine\DBAL\Query\QueryBuilder;
use MailPoetVendor\Doctrine\ORM\EntityManager;
@@ -15,10 +16,15 @@ class WooCommerceCountry implements Filter {
/** @var EntityManager */
private $entityManager;
/** @var DBCollationChecker */
private $collationChecker;
public function __construct(
EntityManager $entityManager
EntityManager $entityManager,
DBCollationChecker $collationChecker
) {
$this->entityManager = $entityManager;
$this->collationChecker = $collationChecker;
}
public function apply(QueryBuilder $queryBuilder, DynamicSegmentFilterEntity $filter): QueryBuilder {
@@ -34,21 +40,24 @@ class WooCommerceCountry implements Filter {
}
$countryFilterParam = 'countryCode' . $filter->getId() ?? Security::generateRandomString();
$subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName();
$collation = $this->collationChecker->getCollateIfNeeded(
$subscribersTable,
'email',
$wpdb->prefix . 'wc_customer_lookup',
'email'
);
return $queryBuilder->innerJoin(
$subscribersTable,
$wpdb->postmeta,
'postmeta',
"postmeta.meta_key = '_customer_user' AND $subscribersTable.wp_user_id=postmeta.meta_value"
$wpdb->prefix . 'wc_customer_lookup',
'customer',
"$subscribersTable.email = customer.email $collation"
)->innerJoin(
'postmeta',
$wpdb->posts,
'posts',
"postmeta.post_id = posts.id AND posts.post_status NOT IN ('wc-cancelled', 'wc-failed')"
)->innerJoin(
'postmeta',
$wpdb->postmeta,
'postmetaCountry',
"postmeta.post_id = postmetaCountry.post_id AND postmetaCountry.meta_key = '_billing_country' AND postmetaCountry.meta_value = :$countryFilterParam"
)->setParameter($countryFilterParam, $countryCode[0]);
'customer',
$wpdb->prefix . 'wc_order_stats',
'orderStats',
'customer.customer_id = orderStats.customer_id'
)->where("customer.country = :$countryFilterParam")
->andWhere('orderStats.status NOT IN ("wc-cancelled", "wc-failed")')
->setParameter($countryFilterParam, $countryCode[0]);
}
}