SQL 语句性能慢

问题描述 投票:0回答:2

我开发了一个导入器,它迭代一个文件(10K + 行)并将 CSV 文件写入 MySQL 数据库。不幸的是这太慢了! 您能建议我如何改进以下代码吗?

for($i=1; $i<count($data); $i++) {

    $rowData = $data[$i];
    if(isset($rowData[0])){
        $brandingPositionDataSql = 'SELECT * from branding_position WHERE sku_simple_product ="'.$rowData[0].'" AND branding_position_de ="'.$rowData[1].'" AND branding_id = "'.$rowData[8].'" AND handling_group_id = "'.$rowData[9].'" AND pre_cost_id = "'.$rowData[11].'"';
        $fetchBradingData = $connection->fetchAll($brandingPositionDataSql);
    
        if(count($fetchBradingData) >= 1){
            $updateBrandingDataSql = 'UPDATE branding_position SET sku_simple_product="'.$rowData[0].'", branding_position_de   ="'.$rowData[1].'",branding_position_en="'.$rowData[2].'",branding_position_fr  ="'.$rowData[3].'",branding_position_es="'.$rowData[4].'",branding_position_it  ="'.$rowData[5].'",branding_position_pl="'.$rowData[6].'",branding_position_nl="'.$rowData[7].'",branding_id="'.$rowData[8].'",is_branding_incl='.$rowData[10].' WHERE sku_simple_product ="'.$rowData[0].'" AND  branding_position_de ="'.$rowData[1].'" AND  branding_id = "'.$rowData[8].'" AND handling_group_id = "'.$rowData[9].'" AND pre_cost_id = "'.$rowData[11].'"';
            $connection->query($updateBrandingDataSql);
        }else{
            $sql1 = 'INSERT INTO branding_position (sku_simple_product, branding_position_de, branding_position_en, branding_position_fr, branding_position_es, branding_position_it, branding_position_pl, branding_position_nl, branding_id, handling_group_id, is_branding_incl, pre_cost_id) VALUES ("'.$rowData[0].'","'.$rowData[1].'","'.$rowData[2].'","'.$rowData[3].'","'.$rowData[4].'","'.$rowData[5].'","'.$rowData[6].'","'.$rowData[7].'","'.$rowData[8].'","'.$rowData[9].'",'.$rowData[10].',"'.$rowData[11].'")';
            $connection->query($sql1);
        }
      }
    }

以下是上述查询的一个示例:

SELECT  *
    from  branding_position
    WHERE  sku_simple_product ="01-06721001-00000"
      AND  branding_position_de ="Oberseite - 20.00 x 0.00 / 0.00"
      AND  branding_id = "01-DB1"
      AND  handling_group_id is NULL
      AND  pre_cost_id = "01-A.STK";

UPDATE  branding_position SET sku_simple_product="01-06721001-00000",
        branding_position_de ="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_en="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_fr ="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_es="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_it ="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_pl="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_nl="Oberseite - 20.00 x 0.00 / 0.00",
        branding_id="01-DB1",is_branding_incl='0'
    WHERE  sku_simple_product ="01-06721001-00000"
      AND  branding_position_de ="Oberseite - 20.00 x 0.00 / 0.00"
      AND  branding_id = "01-DB1"
      AND  handling_group_id = ""
      AND  pre_cost_id = "01-A.STK ";
mysql performance database-performance
2个回答
0
投票

我还是不明白你在做什么。 但这里有一些提示:

  • 使用
    LOAD DATA INFILE ...
    将 CSV 文件复制到表中。 速度非常快。
  • 如果数据(来自
    LOAD
    )需要操作(在
    UPDATEing
    INSERTing
    之前),将
    LOAD
    放入额外的表中,然后进行批量操作。
  • SELECT
    UPDATE
    需要在
    WHERE
    子句中提到的5列上有一个5列索引;它们可以按任何顺序排列。
  • 考虑使用
    INSERT ... ON DUPLICATE KEY UPDATE ...
    来避免选择+存在检查+更新。 您可以对额外表中的整个
    LOADed
    数据执行单个 IODKU。

-1
投票

@里克·詹姆斯:

这是一些示例数据

@里克詹姆斯 以下是上述查询的一个示例:

SELECT  *
    from  branding_position
    WHERE  sku_simple_product ="01-06721001-00000"
      AND  branding_position_de ="Oberseite - 20.00 x 0.00 / 0.00"
      AND  branding_id = "01-DB1"
      AND  handling_group_id is NULL
      AND  pre_cost_id = "01-A.STK";

UPDATE  branding_position SET sku_simple_product="01-06721001-00000",
        branding_position_de ="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_en="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_fr ="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_es="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_it ="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_pl="Oberseite - 20.00 x 0.00 / 0.00",
        branding_position_nl="Oberseite - 20.00 x 0.00 / 0.00",
        branding_id="01-DB1",is_branding_incl='0'
    WHERE  sku_simple_product ="01-06721001-00000"
      AND  branding_position_de ="Oberseite - 20.00 x 0.00 / 0.00"
      AND  branding_id = "01-DB1"
      AND  handling_group_id = ""
      AND  pre_cost_id = "01-A.STK ";
© www.soinside.com 2019 - 2024. All rights reserved.