SELECT
*
FROM
fuu AS main_table
INNER JOIN
(
SELECT
created_at AS last_order_date,
base_cost AS base_cost
FROM
foo
WHERE
product_id = main_table.product_id )LEFT JOIN
(
SELECT
retail_nxt_qty
FROM
faa
WHERE
product_id = main_table.product_id
)
LEFT JOIN
catalog_product_entity_varchar AS cpev_abc
ON (cpev_abc.entity_id = main_table.product_id
AND cpev_abc.attribute_id = 413)
LEFT JOIN
catalog_product_entity_varchar AS cpev_name
ON (cpev_name.entity_id = main_table.product_id
AND cpev_name.attribute_id = 71)
具有magento集合的旧代码导致加载时间超过300秒,从而导致504。
$collection = Mage::getModel('fuu')->getCollection();
$collection->join( array('stock_item'=> 'faa'), 'stock_item.product_id = main_table.product_id', 'retail_nxt_qty');
$collection->join( array('order_item'=> 'foo'), 'order_item.product_id = main_table.product_id', 'base_cost');
$collection->join( array('product'=> 'fii'), 'product.entity_id = main_table.product_id', null);
$collection->getSelect()
->joinLeft(
array('cpev_abc' => 'catalog_product_entity_varchar'),
'(cpev_abc.entity_id = main_table.product_id AND cpev_abc.attribute_id = '. $abcInventory. ')',
array('abc_inventory' => 'value')
);
$collection->getSelect()
->joinLeft(
array('cpev_name' => 'catalog_product_entity_varchar'),
'cpev_name.entity_id=main_table.product_id AND cpev_name.attribute_id='.$productName.'',
array('name' => 'value')
);
$collection->addFieldToSelect(new Zend_Db_Expr('MAX(order_item.created_at) AS last_order_date'));
$collection->addExpressionFieldToSelect('cost_price', 'AVG(order_item.base_cost * stock_item.retail_nxt_qty)', array('cost_price' => 'cost_price'));
$collection->getSelect()->group('order_item.product_id');
我希望获得与以前相同的结果,只是比以前的代码要快,所以我不会得到5分钟的加载时间。我知道我在新代码中缺少组函数,但是我希望它首先起作用。
这将返回以下sql:
SELECT
MAX(order_item.created_at) AS last_order_date,
`stock_item`.`retail_nxt_qty`,
`order_item`.`base_cost`,
`cpev_abc`.`value` AS `abc_inventory`,
`cpev_name`.`value` AS `name`,
AVG(order_item.base_cost * stock_item.retail_nxt_qty) AS `cost_price`
FROM
`fuu` AS `main_table`
INNER JOIN
`faa` AS `stock_item`
ON stock_item.product_id = main_table.product_id
INNER JOIN
`foo` AS `order_item`
ON order_item.product_id = main_table.product_id
INNER JOIN
`catalog_product_entity` AS `product`
ON product.entity_id = main_table.product_id
LEFT JOIN
`catalog_product_entity_varchar` AS `cpev_abc`
ON (cpev_abc.entity_id = main_table.product_id
AND cpev_abc.attribute_id = 413)
LEFT JOIN
`catalog_product_entity_varchar` AS `cpev_name`
ON cpev_name.entity_id = main_table.product_id
AND cpev_name.attribute_id = 71
GROUP BY
`order_item`.`product_id`