php-mysql更新数组非常慢

php-mysql更新数组非常慢,php,mysql,mysqli,Php,Mysql,Mysqli,Mysql库大约有13000行,更新时间大约需要40分钟。 我想 foreach ($result->GetProductBarcodeListResult->ProductBarcodeList->ProductBarcodeas $b) 每次都要建立到远程服务器的新连接,并将所有结果移动到自己的阵列中,但这并没有帮助 请帮助,更新如何更快 更新-mysqli,但它没有提供更多的速度 public function getProductBarcodes() { g

Mysql库大约有13000行,更新时间大约需要40分钟。 我想

foreach ($result->GetProductBarcodeListResult->ProductBarcodeList->ProductBarcodeas $b)
每次都要建立到远程服务器的新连接,并将所有结果移动到自己的阵列中,但这并没有帮助

请帮助,更新如何更快

更新-mysqli,但它没有提供更多的速度

public function getProductBarcodes() 
{
    global $db;
    try
    {
        $this->init();
        $request = new GetProductBarcodeListRequest();
        $params = new GetProductBarcodeList();
        $params->GetProductBarcodeListRequest = $request;
        $result = $this->soapClient->GetProductBarcodeList($params);

    if($result->GetProductBarcodeListResult->ProductBarcodeList->ProductBarcode)
    {
        $eanlist= array();

        foreach ($result->GetProductBarcodeListResult->ProductBarcodeList->ProductBarcode as $b)
        {
            $b->SupplierCode = mysqli_real_escape_string($db,"AC".$b->SupplierCode);
            $b->Barcode = mysqli_real_escape_string($db,$b->Barcode);   
            $eanlist[] = array("eancode" => $b->Barcode,"skuean" => $b->SupplierCode);
        }

        foreach ($eanlist as $eanrow) {
            mysqli_query($db, "
                    UPDATE _new_products
                    SET ean = '$eanrow[eancode]'
                    where sku = '$eanrow[skuean]'
                    ; ");  

        }

        echo "EAN UPDATE DONE!\n";
    }
    //echo "<pre>".     print_r($result,1) . '</pre>';
}
catch(SoapFault $e) {
    echo '<xmp>' . $this->soapClient->__getLastRequestHeaders()  .  $this->soapClient->__getLastRequest() .   '</xmp>';
    echo "<pre>".   print_r($e,1) . '</pre>';
}
}
公共函数getProductBarcodes()
{
全球$db;
尝试
{
$this->init();
$request=new GetProductBarcodeListRequest();
$params=new GetProductBarcodeList();
$params->GetProductBarcodeListRequest=$request;
$result=$this->soapClient->GetProductBarcodeList($params);
如果($result->GetProductBarcodeListResult->ProductBarcodeList->ProductBarcode)
{
$eanlist=数组();
foreach($result->GetProductBarcodeListResult->ProductBarcodeList->ProductBarcode作为$b)
{
$b->SupplierCode=mysqli\u real\u escape\u字符串($db,“AC”。$b->SupplierCode);
$b->Barcode=mysqli\u real\u escape\u字符串($db,$b->Barcode);
$eanlist[]=数组(“eancode”=>$b->条形码,“skuean”=>$b->供应商代码);
}
foreach($eanlist as$eanrow){
mysqli_查询($db,“
更新新产品
设置ean='$eanrow[eancode]'
其中sku='$eanrow[skuean]'
; ");  
}
回显“EAN更新完成!\n”;
}
//回显“.打印($result,1)。”;
}
捕获(SoapFault$e){
回显“”。$this->soapClient->\uuuu getLastRequestHeaders()。$this->soapClient->\uuu getLastRequest();
回音“.打印($e,1)。”;
}
}

尝试以下函数更新单个列。取自



我已将sku从longtext更改为varchar(255),并添加了索引。这就解决了问题

停止使用不推荐的
mysql.*
API。将
mysqli.*
PDO
与准备好的信息一起使用。@Jens你知道,你可以教育人们不要使用ext/mysql,而不用对他们大喊大叫;)您的表是InnoDB还是MyIsam?您设置了哪些索引(如果有的话)?索引有利于阅读,但不利于写作。如果可能的话,通常在使用索引字段更新大量行之前删除索引,这会加快更新/插入的速度,并在以后再次添加索引(与每次更改都必须重新创建/更新索引相比,“一次性”重建索引通常要快得多)请注意,如果您依赖索引来保持列值的唯一性或类似情况,这可能会导致问题both@MaxS问题是什么?您尝试过上面的代码吗?PHP解析错误:语法错误,意外的“&”,预期的“(“否,更新新产品集ean=CASE当“sku1”时,然后是“ean1”,当“sku2”时,然后是“ean2”,sku在(sku1,sku2)ean更新完成的地方结束!发生了什么?问题已解决?
<?php
function bulkUpdateSingleColumn($table, $id_column, $update_column, array &$idstovals){
    $sql = "update $table set $update_column = CASE  ";
    foreach($idstovals as $id=>$val){
        $sql .= " WHEN '$id' THEN '$val' \n";
    }
    $sql .= " END 
    WHERE $id_column in (" . implode(',', array_keys($idstovals)) . ")";
    //debugging info
    echo '<small>'.$sql.'</small>';
    $idstovals=array();
    //  db_query($sql);

}
$eanlist = array("sku1"=>"ean1","sku2"=>"ean2");
$table = '_new_products';
$id_column = 'sku';
$update_column = 'ean';
bulkUpdateSingleColumn($table, $id_column, $update_column,   $eanlist);
?>