php - mysql更新数组非常慢

时间:2016-01-19 08:21:41

标签: php mysql mysqli

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>';
}
}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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