我有一个工作脚本,该脚本从数据库中进行选择,存储用于第二次选择查询的参数,然后将这些选择的值中的一些以及轻微的聚合插入最终的DB表中。
此脚本可以很好地工作,可以返回/插入期望的数据,但要花很长时间才能运行。
在20分钟内完成了大约300条记录。
我只是在这里完全缺少一些东西,会导致它运行得如此缓慢?
$selectPlacements = "
SELECT DISTINCT
sku_id, s.frame as frame, sg.group_code as group_code, sg.name as group_name, s.cover1 as cover, s.color1 as color, dealer_id
FROM placements p
inner join skus s
on p.sku_id = s.id
inner join sku_groups sg
on sg.id = s.sku_group_id
where p.expire_date > curdate()
;
";
try {
$placementCheck = $MysqlConn->prepare($selectPlacements);
$detailRslt = $placementCheck->execute();
while ($placementRow = $placementCheck->fetch(PDO::FETCH_ASSOC)) {
print_r($placementRow); //This prints the array I need
$salesValues = [
":dealer_id" => $placementRow["dealer_id"],
":frame" => $placementRow["frame"],
":cover" => $placementRow["cover"],
":color" => $placementRow["color"],
"group_code" => $placementRow["group_code"],
"group_name" => $placementRow["group_name"],
];
$checkSales = "
SELECT
count(*) as sales_180,
180/count(*) as velocity,
CAST(:group_code AS CHAR(10)),
CAST(:group_name AS CHAR(10)),
cr.xslsno as salesNum,
cr.slsupr as VP
FROM group g
inner join custRecords cr
on g.cstnoc = cr.xcstno
WHERE g.cstnoc = :dealer_id
AND g.framec = :frame
AND g.covr1c = :cover
AND g.colr1c = :color
AND date(substr(g.extd1d,1,4)||'-'||substr(g.EXTD1d,5,2)||'-'||substr(g.EXTD1d,7,2) ) between current_Date - 180 DAY AND current_Date
GROUP BY cr.xslsno, cr.slsupr
";
try{
$salesCheck = $DB2conn->prepare($checkSales);
$salesResult = $salesCheck->execute($salesValues);
$insertSales = "
INSERT INTO testmetrics (dealer, frame, cover, color, groupnum, groupname, last180days_sales, velocity, repnumber, vpnumber )
VALUES (
:dealer_id,
:frame,
:cover,
:color,
:group_code,
:group_name,
:SALES_180,
:VELOCITY,
:SALESNUM,
:VP
)
";
$velocityInsertPrep = $DB2conn->prepare($insertSales);
while ($salesRow = $salesCheck->fetch(PDO::FETCH_ASSOC)) {
$salesValues[":SALES_180"] = $salesRow["SALES_180"];
$salesValues[":VELOCITY"] = $salesRow["VELOCITY"];
$salesValues[":SALESNUM"] = $salesRow["SALESNUM"];
$salesValues[":VP"] = $salesRow["VP"];
print_r($salesValues);
try{
$velocityExecution = $velocityInsertPrep->execute($salesValues);
}catch(PDOException $ex){
echo "INSERT_FAILED: " .$ex->getMessage();
}
}
}catch(PDOException $ex) {
echo "QUERY TWO FAILED!: " .$ex->getMessage();
}
}
}catch(PDOException $ex) {
echo "QUERY ONE FAILED!: " .$ex->getMessage();
}