我开发了一个导入程序,该程序对文件(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);
}
}
}
谢谢!
答案 0 :(得分:0)
@Rick James:
以下是一些示例数据
@RickJames 这是上述查询的一个示例:
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 ";
答案 1 :(得分:0)
我还是不明白你在做什么。但是这里有一些提示:
LOAD DATA INFILE ...
将CSV文件复制到表格中。非常快。LOAD
或UPDATEing
之前的数据(来自INSERTing
),请LOAD
放入额外的表中,然后进行批量操作。SELECT
和UPDATE
在WHERE
子句中提到的5列上需要5列索引;它们可以是任意顺序。INSERT ... ON DUPLICATE KEY UPDATE ...
以避免选择+存在检查+更新。您可能可以为额外表中的整个LOADed
数据执行一个IODKU。