我想知道是否有任何方法来修改我的php代码,使之在数据库上更轻松一点?此代码在cronjob中每分钟运行一次。
希望确保这对db的影响尽可能小。
<?php
ini_set('memory_limit', '128M');
define('MAGENTO_ROOT', getcwd());
//exit;
$time = time();
$time2 = $time - 0;
$to = date('Y-m-d H:i:s', $time2);
$lastTime = $time - 60; //orders from the past X seconds
$from = date('Y-m-d H:i:s', $lastTime);
$curDate = date('Y-m-d');
$mageFilename = '/app/Mage.php';
require_once $mageFilename;
Mage::app();
$order_collection = Mage::getResourceModel('sales/order_collection');
$order_collection->addAttributeToSelect('*')->addAttributeToFilter('updated_at', array(
'from' => $from,
'to' => $to
))->getSelect();
print count($order_collection);
foreach ($order_collection->getItems() as $order) {
$cards = Mage::getModel('giftcards/giftcards')->getCollection()->addFieldToFilter('order_id', $order->getId());
print count($cards);
foreach ($cards as $card) {
if ($card->getCardStatus() == 0) {
if ((($card->getMailDeliveryDate() == null) || ($curDate == $card->getMailDeliveryDate())) && $card->getCardType() != 'offline') {
$card->setCardStatus(1);
$card->save();
$card->send();
}
} elseif (($card->getCardStatus() == 1) && ($curDate < $card->getMailDeliveryDate())) {
$card->setCardStatus(0);
$card->save();
}
}
}
?>
1条答案
按热度按时间1zmg4dgp1#
你唯一需要马上优化的就是你的表现
giftcards
:你得到了order_collections
你迭代这些项目,得到相关的数据;这被称为n=1问题。我不知道magento到底是怎么工作的(我很肯定他们有一些
join
但您需要预先获取所需的所有内容,然后循环遍历结果并按您的需要格式化数据。