magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

$productAttribute->getFrontend()->getSelectOptions() grabbing unnecessary options

Open fr0x opened this issue 13 years ago • 2 comments

This was a problem in 1.x Magento as well.

$productAttribute->getFrontend()->getSelectOptions(); basically grabs all available options, regardless if they are relevant to the product. Not a big issue if you have a small set of super attributes but if you have an amount numbering in the thousands or more, it can cause additional time on on any for loops that are using the returning data.

All credit to turnkeye (http://turnkeye.com/blog/magento-perfomance-optimization-of-configurable-products)

Implementing these changes saves us around 300ms per product in an add to cart call (we have around 2,000 super attributes) or any cart save call.

Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection protected function _loadPrices() (line 266)

Replace: $options = $productAttribute->getFrontend()->getSelectOptions();

With: $_options = array(); foreach ($_prods as $associatedProduct) { $_options[] = $associatedProduct->getData($productAttribute->getAttributeCode()); } $options = $productAttribute->getSource()->getNeededOptions($_options)


Mage_Eav_Model_Entity_Attribute_Source_Table

Add function: public function getNeededOptions($ids) { $storeId = $this->getAttribute()->getStoreId(); $collection = Mage::getResourceModel('eav/entity_attribute_option_collection') ->setPositionOrder('asc') ->setAttributeFilter($this->getAttribute()->getId()) ->addFieldToFilter('main_table.option_id', array('in' => $ids)) ->setStoreFilter($this->getAttribute()->getStoreId()) ->load(); return $collection->toOptionArray(); }

This logic can also be applied to $value->getSource()->getOptionText($attributeValue); which also calls $this->getAllOptions(false) and loads all of the options unnecessarily.

fr0x avatar May 09 '13 14:05 fr0x

Please check https://github.com/magento/magento2/issues/90 for detailed discussion regarding this issue.

magento-team avatar Jun 02 '13 15:06 magento-team

The problem that this issue fixes is the number of super attributes returned by this line: $options = $productAttribute->getFrontend()->getSelectOptions();

Issue #90 does nothing to correct that.

I'm assuming you may have run a similar test against this change (as in issue #90): "2500 configurable products with 2 simple products as options per each configurable product"

How many super attributes did this test contain? Was each simple product completely unique in this test (therefore having 5,000 super attributes)? If you only had 2 super attributes to cover the 5,000 simple products, then this test would have (incorrectly) shown no improvements.

If you have 5,000 super attributes, this call: $options = $productAttribute->getFrontend()->getSelectOptions();

is going to return 5,000 options causing the for loop (foreach ($options as $option)) to run 5,000 times.

Even worse, the nested for loop (foreach ($usedProducts as $associatedProduct)) is going to cause this to run even more than than the already unnecessary 5,000 times.

While both issues are addressing the same function and the same issue, they are addressing them in different ways (and the test case used to test issue #90 might have been completely inaccurate depending on the number of super attributes that it was tested against).

fr0x avatar Jun 03 '13 14:06 fr0x