23 апреля 2017
При большом количестве правил (> 20 000), получаем время выполнения запроса к БД в 4,2 секунды.
В классе Mage_SalesRule_Model_Resource_Rule смотрим функцию:
public function getActiveAttributes($websiteId, $customerGroupId)
{
$read = $this->_getReadAdapter();
$select = $read->select()
->from(array('a' => $this->getTable('salesrule/product_attribute')),
new Zend_Db_Expr('DISTINCT ea.attribute_code'))
->joinInner(array('ea' => $this->getTable('eav/attribute')), 'ea.attribute_id = a.attribute_id', array());
return $read->fetchAll($select);
}
Поменяв немного join, получаем:
public function getActiveAttributes($websiteId, $customerGroupId)
{
$read = $this->_getReadAdapter();
$subselect = $read->select()
->from(array('a' => $this->getTable('salesrule/product_attribute')),
new Zend_Db_Expr('DISTINCT a.attribute_id'));
$select = $read->select()
->from(array('ea' => $this->getTable('eav/attribute')), array('ea.attribute_code'))
->where('ea.attribute_id in (' . $subselect . ')');
return $read->fetchAll($select);
}
Время выполнения запроса в БД стало 0,23 секунды. Ускорение почти в 20 раз!
Mission complete.