简体   繁体   English

php遍历数组并更新mysql的最快方法?

[英]Php Fastest way to iterate through an array and update mysql?

I wrote a product price/stock update script for Magento. 我为Magento编写了产品价格/库存更新脚本。 I load the csv into an array and then iterate through it. 我将csv加载到数组中,然后对其进行迭代。 The current code takes around 10 minutes to complete for 5,000 products, is there a faster way to do this? 目前的代码大约需要10分钟才能完成5,000种产品,请问有没有更快的方法? I've already bypassed Magento's API as that was extremely slow and switched to updating the database directly since its not many tables and its faster. 我已经绕过了Magento的API,因为它非常慢,由于表数量不多且速度较快,因此切换到直接更新数据库。 Using timers to record the time, it takes about 10 minutes for the foreach loop and two minutes for the reindexALL 使用计时器记录时间,foreach循环大约需要10分钟,reindexALL需要大约2分钟

$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);

If your table catalog_product_entity_decimal has index, that covers id (obviously it is) - then you have no other ways to speed it up. 如果您的表catalog_product_entity_decimal具有索引,该索引涵盖了id(显然是id)-那么您没有其他方法可以加快它的速度。 Since the slowest thing here is physical changing of the value. 由于这里最慢的是值的物理变化。 Probably you can put a WHERE clause to to avoid of updating the price to the same value. 可能您可以放置WHERE子句,以避免将价格更新为相同的值。

Other thoughts: While most people look at performance optimizations for SELECT statements, UPDATE and DELETE statements are often overlooked. 其他想法:虽然大多数人都在关注SELECT语句的性能优化,但UPDATEDELETE语句经常被忽略。 These can benefit from the principles of analyzing the Query Execution Plan (QEP). 这些可以受益于分析查询执行计划 (QEP)的原理。 You can only run an EXPLAIN on a SELECT statement, however it's possible to rewrite an UPDATE or DELETE statement to perform like a SELECT statement. 您只能在SELECT语句上运行EXPLAIN ,但是可以重写UPDATEDELETE语句以像SELECT语句一样执行。

To optimize an UPDATE , look at the WHERE clause. 要优化UPDATE ,请查看WHERE子句。 If you are using the PRIMARY KEY , no further analysis is necessary. 如果您使用的是PRIMARY KEY ,则无需进一步分析。 If you are not, it is of benefit to rewrite your UPDATE statement as a SELECT statement and obtain a QEP as previously detailed to ensure optimal indexes are used. 如果不是这样,则将UPDATE语句重写为SELECT语句并获得先前详细说明的QEP以确保使用最佳索引是有益的。 For example: 例如:

UPDATE t
SET c1 = ‘x’, c2 = ‘y’, c3 = 100
WHERE c1 = ‘x’
AND d = CURDATE()

You can rewrite this UPDATE statement as a SELECT statement for using EXPLAIN : 您可以将此UPDATE语句重写为SELECT语句,以使用EXPLAIN

EXPLAIN SELECT c1, c2, c3 FROM  t WHERE c1 = ‘x’ AND    d = CURDATE()

You should now apply the same principles as you would when optimizing SELECT statements. 现在,您应该应用与优化SELECT语句时相同的原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM