我在XML文件中有大约30K条记录,并且此文件一直都在更新。使用PHP将大型xml文件导入/更新到MySQL中
我想插入,如果存在更新MySQL分贝。
这是我想使用的代码,但运行速度非常缓慢,有没有人有任何想法来提高其性能?
// getting xml file
$dom = new DOMDocument();
$dom->load('products.xml');
// getting xml nodes using xpath
$xpath = new DOMXPath($dom);
$productid = $xpath->query('//NewDataSet/Product/ProductId');
$price = $xpath->query('//NewDataSet/Product/Price');
// Reading all nodes and if mach found in db update price, else insert as new record**
for($i=0;$i<$allNodes->length;$i++){
$testproductid = $productid->item($i)->nodeValue;
$testprice = $price->item($i)->nodeValue;
if(mysql_num_rows(mysql_query("Select productid from test where productid ='$testproductid'"))){
mysql_query("UPDATE test SET price = '$testprice' WHERE productid = '$testproductid'");
}else{
mysql_query("INSERT INTO test (price, productid) VALUES ('$testprice','$testproductid')");
}
}
你需要的'/ /'在'XPath'中?它也会使事情变得缓慢...... – Wrikken 2011-04-05 21:30:49
-1,SQL注入。准备好的陈述可能会使这个速度明显加快。 – 2011-04-05 22:20:17