0

我必须一次将超过 200000 条记录插入到 mysql db 表中,插入查询导致性能问题,有什么可以替代它。

下面是我正在使用的代码

$xml = simplexml_load_file("247electrical.xml");

foreach($xml->merchant as $merchant){

define('API', 'PS');
require_once('constants.inc.php');
require_once('classes/class.ClientFactory.php');
$oClient = ClientFactory::getClient(API_USERNAME, API_PASSWORD, API_USER_TYPE); $merchattrs=$merchant->attributes();
$aParams100 = array('iMerchantId' => array($merchattrs->id)); $merchantinfo= $oClient->call('getMerchant', $aParams100);

//Get Products

foreach($xml->merchant->prod as $product){

$attrs=$product->attributes();

//Insert Products into DB
mysql_query('INSERT INTO productstemp (merchant_id, merchant_name, aw_product_id, merchant_product_id, product_name, description, category_id, merchant_category, aw_deep_link, aw_image_url, search_price, delivery_cost, merchant_image_url, aw_thumb_url, brand_name, delivery_time, display_price, in_stock, merchant_thumb_url, model_number, pre_order, stock_quantity, store_price, valid_from, valid_to, web_offer, merchantimage, cleancompany) VALUES("'.$merchattrs->id.'","'.$merchattrs->name.'","'.$attrs->id.'"," ","'.$product->text->name.'","'.$product->text->desc.'","'.$product->cat->awCatId.'","'.$product->cat->mCat.'","'.$product->uri->awTrack.'","'.$product->uri->awImage.'","'.$product->price->buynow.'","'.$product->price->delivery.'","'.$product->uri->mImage.'","'.$product->uri->awThumb.'","'.$product->brand->brandName.'","'.$product->delTime.'","'.$product->price->buynow.'","'.$attrs->in_stock.'","'.$product->uri->mThumb.'","'.$product->modelNumber.'","'.$attrs->pre_order.'","'.$attrs->stock_quantity.'","'.$product->price->store.'","'.$product->valFrom.'","'.$product->valTo.'","'.$attrs->web_offer.'","'.$merchantinfo->oMerchant->sLogoUrl.'","247electrical" ) ')
or die(mysql_error());     

}
} 

谢谢

4

1 回答 1

0

我认为 INSERT 查询本身不是问题。毕竟 200.000 次插入对于 mysql 来说并不算多。

首先我猜读文件很慢。SimpleXML 很方便,但对于大文件,它会导致巨大的内存开销。想想像 PHP 的XMLReader这样的流式 XML 阅读器。

您正在向 mysql 服务器发送单个语句,这比发送一个巨大的语句要慢得多。此外,您的单个插入语句应包含在事务中。如果您处理了 10.000 条记录并插入它们,然后您的脚本死机/mysql 服务器死机等会发生什么?如何在没有手动工作的情况下安全地再次启动脚本(清除表格、查找已处理的内容等)。

除此之外,一个包含多个 VALUES 的单个 INSERT 语句应该更快。我会让你的 PHP 脚本输出查询,所以它最终看起来像这样:

INSERT INTO table(field_1, field_2, field 3)
VALUES('foo 1', 'bar 1', 'baz 1'),
VALUES('foo 2', 'bar 2', 'baz 2'),
...

然后通过以下方式导入该文件:

$ mysql ... credentials options etc ... < output.sql

如果那仍然太慢……购买更多硬件也可能会有所帮助。

于 2012-07-12T11:49:32.907 回答