Skip to content

Commit

Permalink
Merge pull request #1392 from mailchimp/Issue1391-2.3
Browse files Browse the repository at this point in the history
Optimize queries for ecommerce table Issue1391 2.3
  • Loading branch information
gonzaloebiz authored May 20, 2022
2 parents 77bbe28 + 80a97b2 commit e43efba
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Model/Api/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId)
// get only the converted quotes
$convertedCarts->addFieldToFilter('store_id', ['eq' => $magentoStoreId]);
$convertedCarts->addFieldToFilter('is_active', ['eq' => 0]);
$convertedCarts->getSelect()->reset(\Zend_Db_Select::COLUMNS)->columns(['entity_id']);

//join with mailchimp_ecommerce_sync_data table to filter by sync data.
$convertedCarts->getSelect()->joinLeft(
['m4m' => $this->_helper->getTableName('mailchimp_sync_ecommerce')],
Expand Down Expand Up @@ -223,6 +225,8 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId)
$modifiedCarts->addFieldToFilter('is_active', ['eq'=>1]);
// select carts for the current Magento store id
$modifiedCarts->addFieldToFilter('store_id', ['eq' => $magentoStoreId]);
$modifiedCarts->getSelect()->reset(\Zend_Db_Select::COLUMNS)->columns(['entity_id']);

//join with mailchimp_ecommerce_sync_data table to filter by sync data.
$modifiedCarts->getSelect()->joinLeft(
['m4m' => $this->_helper->getTableName('mailchimp_sync_ecommerce')],
Expand All @@ -237,7 +241,6 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId)
);
// limit the collection
$modifiedCarts->getSelect()->limit(self::BATCH_LIMIT);

/**
* @var $cart \Magento\Quote\Model\Quote
*/
Expand Down Expand Up @@ -356,6 +359,8 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId)
if ($this->_firstDate) {
$newCarts->addFieldToFilter('created_at', ['gt' => $this->_firstDate]);
}
$newCarts->getSelect()->reset(\Zend_Db_Select::COLUMNS)->columns(['entity_id']);

//join with mailchimp_ecommerce_sync_data table to filter by sync data.
$newCarts->getSelect()->joinLeft(
['m4m' => $this->_helper->getTableName('mailchimp_sync_ecommerce')],
Expand All @@ -365,7 +370,6 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId)
);
// be sure that the quotes are already in mailchimp and not deleted
$newCarts->getSelect()->where("m4m.mailchimp_sync_delta IS NULL");

// limit the collection
$newCarts->getSelect()->limit(self::BATCH_LIMIT);
/**
Expand Down
1 change: 1 addition & 0 deletions Model/Api/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function sendCustomers($storeId)
$listId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_LIST, $storeId);
$collection = $this->_collection->create();
$collection->addFieldToFilter('store_id', ['eq'=>$storeId]);
$collection->getSelect()->reset(\Zend_Db_Select::COLUMNS)->columns(['entity_id','store_id']);
$collection->getSelect()->joinLeft(
['m4m' => $this->_helper->getTableName('mailchimp_sync_ecommerce')],
"m4m.related_id = e.entity_id and m4m.type = '".\Ebizmarts\MailChimp\Helper\Data::IS_CUSTOMER.
Expand Down
2 changes: 2 additions & 0 deletions Model/Api/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ protected function _getModifiedOrders($magentoStoreId)
$modifiedOrders = $this->_getCollection();
// select orders for the current Magento store id
$modifiedOrders->addFieldToFilter('store_id', ['eq' => $magentoStoreId]);
$modifiedOrders->addFieldToSelect(['store_id','created_at']);
//join with mailchimp_ecommerce_sync_data table to filter by sync data.
$modifiedOrders->getSelect()->joinLeft(
['m4m' => $this->_helper->getTableName('mailchimp_sync_ecommerce')],
Expand Down Expand Up @@ -238,6 +239,7 @@ protected function _getNewOrders($magentoStoreId)
$newOrders = $this->_getCollection();
// select carts for the current Magento store id
$newOrders->addFieldToFilter('store_id', ['eq' => $magentoStoreId]);
$newOrders->addFieldToSelect(['store_id','created_at']);
// filter by first date if exists.
if ($this->_firstDate) {
$newOrders->addFieldToFilter('created_at', ['gt' => $this->_firstDate]);
Expand Down
2 changes: 2 additions & 0 deletions Model/Api/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public function _sendProducts($magentoStoreId)
$collection = $this->_getCollection();
$collection->addFieldToFilter("type_id", ["nin"=>[\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE, "grouped"]]);
$collection->addStoreFilter($magentoStoreId);
$collection->getSelect()->reset(\Zend_Db_Select::COLUMNS)->columns(['entity_id']);
$collection->getSelect()->joinLeft(
['m4m' => $this->_helper->getTableName('mailchimp_sync_ecommerce')],
"m4m.related_id = e.entity_id and m4m.type = '".\Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT.
Expand All @@ -139,6 +140,7 @@ public function _sendProducts($magentoStoreId)
$this->_helper->getMCMinSyncDateFlag().
"' and m4m.mailchimp_sync_modified = 1)");
$collection->getSelect()->limit(self::MAX);

foreach ($collection as $item) {
/**
* @var $product \Magento\Catalog\Model\Product
Expand Down
12 changes: 12 additions & 0 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,5 +546,17 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
['store_id','regtype','original_id']
);
}
if (version_compare($context->getVersion(), '102.3.45') < 0) {
$connection->addIndex(
$setup->getTable('mailchimp_sync_ecommerce'),
$connection->getIndexName($setup->getTable('mailchimp_sync_ecommerce'), 'mailchimp_sync_delta', 'index'),
'mailchimp_sync_delta'
);
$connection->addIndex(
$setup->getTable('mailchimp_sync_ecommerce'),
$connection->getIndexName($setup->getTable('mailchimp_sync_ecommerce'), 'mailchimp_sync_modified', 'index'),
'mailchimp_sync_modified'
);
}
}
}

0 comments on commit e43efba

Please sign in to comment.