I have only been working with Magento for a few months, wanting to get some ideas on best way to improve the below query. Thanks in advance.
So I am working on a module created a few years ago that was running on Magento 2.3.5. This site is being updated to 2.4.4 and I have an issue this query running and using up a tonne of memory, 8GB in local dev.
$connection = $this->resource->getConnection();
$select = $connection->select()
->from(
['e' => 'catalog_product_entity'],
['entity_id', 'sku']
)
->joinLeft(
['tier' => 'catalog_product_entity_tier_price'],
'e.entity_id = tier.entity_id',
['customer_group_id', 'qty', 'value as tier_price']
)
->where('(tier.all_groups IS NULL OR tier.all_groups = ?)', 0);
$this->pricesTiers = [];
$data = $connection->fetchAll($select);
foreach ($data as $item) {
if (!isset($this->pricesTiers[$item['sku']])) {
$this->pricesTiers[$item['sku']] = [
'entity_id' => $item['entity_id'],
'sku' => $item['sku'],
//'price' => (float)$item['price'],
'tiers' => [],
];
}
if ($item['customer_group_id'] !== null) {
$this->pricesTiers[$item['sku']]['tiers'][$item['customer_group_id']][(int) $item['qty']] = [
'customer_group_id' => (int) $item['customer_group_id'],
'qty' => (int) $item['qty'],
'tier_price' => (float) $item['tier_price'],
];
}
}
There are 5,487,000 + records and it chews a tonne of memory to complete. Thinking the conditionals of the foreach should be added to the query and processed inline or paged to reduce load and prevent the memory leak.
Also thinking this should probably be using the MagentoCatalogModelProduct
Model and the MagentoCatalogApiProductTierPriceManagementInterface
Interface rather than a raw query but not sure if that is feasible.
Greatly appreciate all input and ideas.