此脚本可以正常工作,但在更新时运行速度非常慢。问题是,对于我从源(DB2)数据库中选择的每条记录,我必须检查该记录(在我的逻辑条件下)是否已经存在,如果是,那么新记录是否与现有记录的到期窗口一致。如果是这样,它会更新。如果不是它插入。插入是非常快速的,正如预期的那样,但是因为我在while循环中执行更新查询,所以更新速度很慢。
同样,我必须检查每条记录,因为它们经常发生,因此我不能只进行批量选择然后使用逻辑进行更新,因为select可能包含记录1 - 100而我的while循环可能包含记录111,而记录110是需要更新的相同记录。那有意义吗?因此,如果没有在记录111处进行选择,它将插入而不是更新,因为它不会记录记录110,因为初始选择最多只包含100个。
这里的逻辑有点奇怪,这是有原因的,但它又有效。我只是希望能够快速优化更新。
$detailStatCheck = "
SELECT
ordrnum as order,
stat as STATUS,
cust AS customer,
build AS build,
matrl AS matrl,
pattern AS pattern ,
date AS SHIPDATE,
qty AS QUANTITY,
case when p.stores is null then 1 else p.stores end as STORES
FROM table1 g
left join table2 p on g.cust = p.cust;
";
try {
$detailCheck = $DB2conn->prepare($detailStatCheck);
$detailRslt = $detailCheck->execute();
$count2 = $detailCheck->fetch();
print_r($count2);
}catch(PDOException $ex) {
echo "QUERY ONE FAILED!: " .$ex->getMessage();
}
$validCheck = "
SELECT
sku_id,
customer_id,
expire_date
FROM customerplacetest p
INNER JOIN skus s
ON p.sku_id = s.id
WHERE p.customer_id = :customer
AND s.build = :build
AND s.matrl1 = :matrl
AND s.pattern1 = :pattern
AND p.expire_date > date_add(convert(:SHIPDATE,date), interval 7 day)
";
$updatePlacement = "
UPDATE customerplacetest d
inner join skus s
ON d.sku_id = s.id
SET expire_date = DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 127 DAY),
quantity = :QUANTITY
WHERE d.customer_id = :customer
AND s.build = :build
AND s.matrl1 = :matrl
AND s.pattern1 = :pattern
";
$checkExistingValid = $MysqlConn->prepare($validCheck);
$insert = $MysqlConn->prepare($insertPlacement);
$update = $MysqlConn->prepare($updatePlacement);
while ($row2 = $detailCheck->fetch(PDO::FETCH_ASSOC)) {
$values = [
":customer" => $row2["customer"],
":SHIPDATE" => $row2["SHIPDATE"],
":STORES" => $row2["STORES"],
":QUANTITY" => $row2["QUANTITY"],
":order" => $row2["order"],
":build" => $row2["build"],
":matrl" => $row2["matrl"],
":pattern" => $row2["pattern"],
];
$values2 = [
":customer" => $row2["customer"],
":build" => $row2["build"],
":matrl" => $row2["matrl"],
":pattern" => $row2["pattern"],
":SHIPDATE" => $row2["SHIPDATE"],
];
$values3 = [
":SHIPDATE" => $row2["SHIPDATE"],
":QUANTITY" => $row2["QUANTITY"],
":customer" => $row2["customer"],
":build" => $row2["build"],
":matrl" => $row2["matrl"],
":pattern" => $row2["pattern"],
];
try{
//Array will contain records that are valid
$existingVldRslt = $checkExistingValid->execute($values2);
$count4 = $checkExistingValid->fetch(PDO::FETCH_ASSOC);
print_r($count4);
}catch(PDOException $ex){
echo "QUERY TWO FAILED!!!: " . $ex->getMessage();
}
if(!empty($count4)){
print_r("updating");
for($i=0; $i<$row2['QUANTITY']; $i++){
try{
$updateRslt = $update->execute($values3);
}catch(PDOException $ex){
echo "UPDATE_FAILED!!!: " . $ex->getMessage();
}
}
}else{
print_r("inserting");
if($row2["QUANTITY"] >= $row2["STORES"]){
for($i=0; $i<$row2["STORES"]; $i++){
try{
$insertRslt = $insert->execute($values);
}catch(PDOException $ex){
echo "INSERT_FAILED!!!: " . $ex->getMessage();
}
}
}elseif($row2["QUANTITY"] < $row2["STORES"]){
for($i=0; $i<$row2["QUANTITY"]; $i++){
try{
$insertRslt = $insert->execute($values);
}catch(PDOException $ex){
echo "INSERT_FAILED!!!: " . $ex->getMessage();
}
}
}
}
}