我爲Magento寫了一個產品價格/庫存更新腳本。我將csv加載到數組中,然後遍歷它。目前的代碼需要大約10分鐘才能完成5000個產品,有沒有更快的方法來做到這一點?我已經繞過了Magento的API,因爲這非常慢,並且由於其表格更多而且速度更快,所以直接更新數據庫。使用計時器來記錄時間,大約需要10分鐘,foreach循環和兩分鐘的時間reindexALLPHP最快的方式來遍歷數組並更新mysql?
$con = mysql_connect("localhost","root","");
$selected = mysql_select_db("magento",$con);
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');
foreach($all_rows as $final)
{
$sql = mysql_query("SELECT entity_id from catalog_product_entity where sku = '".$final[ITEM]."'");
if ($row = mysql_fetch_array($sql)) {
//update price
$pricenew = $final['PRICE'] + ($final['PRICE']*.30);
mysql_query("UPDATE catalog_product_entity_decimal SET value = '$pricenew' where attribute_id = 75 AND entity_id = '".$row[entity_id]."' ");
//update retail price
$retailprice = $final['RETAIL'];
mysql_query("UPDATE catalog_product_entity_decimal SET value = '$retailprice' where attribute_id = 120 AND entity_id = '".$row[entity_id]."' ");
//update stock quantity and is in stock
$stockquantity = $final['QTY'];
$stockquantity = number_format($stockquantity, 4, '.', '');
mysql_query("UPDATE cataloginventory_stock_item SET qty = '$stockquantity', SET is_in_stock = 1 where product_id = '".$row[entity_id]."' ");
}
$processes->walk('reindexAll');
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');
mysql_close($con);
不是你的問題的直接答案,但不要使用'mysql_ *',它開始了棄用過程。看看「PDO」或「mysqli_ *」,而不是 –
你檢查過Mysql的LOAD DATA INFILE嗎? –
雖然產品已經在數據庫中,但我只需要每天從csv更新價格/庫存。價格和庫存也有兩個不同的表格。最好是使用LOAD DATA INFILE嗎? – user1155594